小白程序员为了这事折腾了一天,试过换压缩格式,换压缩方式,使用多线程去压缩,都没有解决这个问题,最后公司的大佬帮忙解决了一下 给了下面一个使用多线程进行文件压缩的解决方案,具体逻辑暂时还没有理解,先搬运过来,以便于参考理解
/**
* 压缩文件夹
*
* @param zipOutName zip输出路径
* @param paths 将要压缩的路径
* @throws IOException
* @throws ExecutionException
* @throws InterruptedException
*/
public static void compressFiles(String zipOutName, String... paths) throws IOException, ExecutionException, InterruptedException {
long start = System.currentTimeMillis();
// 创建一个线程池对象
ExecutorService executor = new ThreadPoolExecutor(10, 20, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(20), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());
// 压缩等级默认为速度优先
compressFiles(zipOutName, executor, Deflater.BEST_SPEED, paths);
long end = System.currentTimeMillis();
log.info("压缩完成,耗时:" + (end - start) + " ms");
}
/**
* 自定义线程池
*
* @param zipOutName
* @param executorService 线程池实现对象
* @param paths
* @throws IOException
* @throws ExecutionException
* @throws InterruptedException
*/
public static void compressFiles(String zipOutName, ExecutorService executorService, int level, String... paths) throws IOException, ExecutionException, InterruptedException {
// 创建用于多线程压缩文件的对象
ParallelScatterZipCreator parallelScatterZip