多线程压缩
/**
* @param sourceFilePath 需要压缩的目录
* @param zipOutName 压缩后的文件名称
**/
public static void compressFileList(String sourceFilePath, String zipOutName) throws IOException, ExecutionException, InterruptedException {
File sourceFile = new File(sourceFilePath);
File[] sourceFiles = sourceFile.listFiles();
ExecutorService executor = new ThreadPoolExecutor(5, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(20));
ParallelScatterZipCreator parallelScatterZipCreator = new ParallelScatterZipCreator(executor);
OutputStream outputStream = new FileOutputStream(zipOutName);
ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(outputStream);
zipArchiveOutputStream.setEncoding("UTF-8");
for (File inFile : sourceFiles) {
final InputStreamSupplier inputStreamSupplier = () -> {
try {
return new FileInputStream(inFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
return new NullInputStream(0);
}
};
ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(inFile.getName());
zipArchiveEntry.setMethod(ZipArchiveEntry.DEFLATED);
zipArchiveEntry.setSize(inFile.length());
zipArchiveEntry.setUnixMode(UnixStat.FILE_FLAG | 436);
parallelScatterZipCreator.addArchiveEntry(zipArchiveEntry, inputStreamSupplier);
}
parallelScatterZipCreator.writeTo(zipArchiveOutputStream);
zipArchiveOutputStream.close();
outputStream.close();
System.out.println("ParallelCompressUtil->ParallelCompressUtil-> info:{}"+ JSONObject.toJSONString(parallelScatterZipCreator.getStatisticsMessage()));
}
压缩时间结果
![](https://img-blog.csdnimg.cn/img_convert/bfbfa61a7b2da485e7bb2c97ca661f8f.png)
hutool压缩工具
ZipUtil.zip("C:\\Users\\root\\Desktop\\zip包\\C20230013+赤石镇冰深村石龙庵后山等3处边坡治理工程","E:\\C20230013+赤石镇冰深村石龙庵后山等3处边坡治理工程.zip");
压缩时间
![](https://img-blog.csdnimg.cn/img_convert/e5d2d8cf9ac3dcdb481abfa29876f5bb.png)
一个时间短一个简单,看你咋个选择