java压缩工具类

44 阅读1分钟

` import org.apache.commons.compress.archivers.ArchiveException; import org.apache.commons.compress.archivers.ArchiveOutputStream; import org.apache.commons.compress.archivers.ArchiveStreamFactory; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;

import java.io.; import java.util.ArrayList; import java.util.List; import java.util.concurrent.;

public class ZipCompressorUtil { private static final int THREAD_POOL_SIZE = 5; // 定义线程池大小

/**
 * 多线程压缩
 *
 * @param sourceFolderPath
 * @param outputZipFilePath
 */
public static void compressToZip(String sourceFolderPath, String outputZipFilePath) {
    try {
        File sourceFolder = new File(sourceFolderPath);
        File outputZipFile = new File(outputZipFilePath);

        FileOutputStream fos = new FileOutputStream(outputZipFile);
        ArchiveOutputStream aos = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, fos);

        ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
        List<Future<Void>> futures = new ArrayList<>();
        Object lock = new Object(); // 用于同步操作的锁对象

        compressFolder(sourceFolder, sourceFolder.getName(), aos, executorService, futures, lock);

        for (Future<Void> future : futures) {
            try {
                future.get(); // 等待所有压缩任务完成
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }

        executorService.shutdown();
        executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);

        aos.finish();
        aos.close();
        fos.close();
    } catch (IOException | ArchiveException | InterruptedException e) {
        e.printStackTrace();
    }
}

private static void compressFolder(File folder, String parentFolder, ArchiveOutputStream aos, ExecutorService executorService, List<Future<Void>> futures, Object lock) throws IOException {
    File[] files = folder.listFiles();
    if (files != null) {
        for (File file : files) {
            if (file.isDirectory()) {
                if (file.listFiles().length > 0) {
                    compressFolder(file, parentFolder + "/" + file.getName(), aos, executorService, futures, lock);
                } else {
                    // 如果文件夹是空的,创建一个空的Entry,并写入压缩流
                    ZipArchiveEntry entry = new ZipArchiveEntry(parentFolder + "/" + file.getName() + "/");
                    synchronized (lock) {
                        aos.putArchiveEntry(entry);
                        aos.closeArchiveEntry();
                    }
                }
            } else {
                Callable<Void> compressTask = () -> {
                    compressFile(file, parentFolder, aos, lock);
                    return null;
                };
                futures.add(executorService.submit(compressTask));
            }
        }
    }
}

private static void compressFile(File file, String parentFolder, ArchiveOutputStream aos, Object lock) throws IOException {
    ZipArchiveEntry entry = new ZipArchiveEntry(parentFolder + "/" + file.getName());
    synchronized (lock) {
        aos.putArchiveEntry(entry);

        try (InputStream fis = new FileInputStream(file);
             BufferedInputStream bis = new BufferedInputStream(fis)) {
            byte[] buffer = new byte[1024];
            int length;
            while ((length = bis.read(buffer)) != -1) {
                aos.write(buffer, 0, length);
            }
            aos.closeArchiveEntry();
        }
    }
}

}`

导入的pom net.sf.sevenzipjbinding sevenzipjbinding 16.02-2.01 net.sf.sevenzipjbinding sevenzipjbinding-all-platforms 16.02-2.01