Apache commons-compress ZIP打包

ZipArchiveOutputStream zipOutput = null;
		try {
			String folderPath = "d:\\測試文件夾"; 
			File zipFile = new File("d:\\demo.zip");
			zipOutput = (ZipArchiveOutputStream) new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, new FileOutputStream(zipFile));
			zipOutput.setEncoding("UTF-8");
			zipOutput.setUseZip64(Zip64Mode.AsNeeded);
			File[] files = new File(folderPath).listFiles();
			for(File file : files){
				InputStream in = null;
				try {
					in = new FileInputStream(file);
					ZipArchiveEntry entry = new ZipArchiveEntry(file, file.getName());//zipOutput.createArchiveEntry(logFile, logFile.getName());
					zipOutput.putArchiveEntry(entry);
					IOUtils.copy(in, zipOutput);
					zipOutput.closeArchiveEntry();
				}finally{
					if(in != null){
						try {
							in.close();
						} catch (Exception e) { }
					}
				}
			}
			zipOutput.finish();
			zipOutput.close();
		}catch(Exception e){
			if(zipOutput != null){
				try {
					zipOutput.close();
				} catch (Exception e1) { }
			}
			throw e;
		}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章