Java多线程Zip压缩
生活随笔
收集整理的這篇文章主要介紹了
Java多线程Zip压缩
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java多線程Zip壓縮
- Zip壓縮
- 多線程壓縮
- 線程池
依賴 maven坐標
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-compress</artifactId><version>1.20</version> </dependency>壓縮工具包代碼 ZipCompressUtils.java
package com.test.utils;import org.apache.commons.compress.archivers.zip.ParallelScatterZipCreator; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.compress.parallel.InputStreamSupplier; import org.apache.commons.io.input.NullInputStream;import java.io.*; import java.util.concurrent.*; import java.util.zip.Deflater;/*** @author youlingdada youlingdada@163.com* @version 1.0* @createDate 2022/1/30 11:15*/ public class ZipCompressUtils {/*** 壓縮文件夾** @param zipOutName zip輸出路徑* @param paths 將要壓縮的路徑* @throws IOException* @throws ExecutionException* @throws InterruptedException*/public static void compressFiles(String zipOutName, String... paths) throws IOException, ExecutionException, InterruptedException { // 創建一個線程池對象ExecutorService executor = new ThreadPoolExecutor(5, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(20), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy()); // 壓縮等級默認為速度優先compressFiles(zipOutName, executor, Deflater.BEST_SPEED, paths);}/*** 自定義線程池** @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 parallelScatterZipCreator = new ParallelScatterZipCreator(executorService); // 輸出文件流OutputStream outputStream = new FileOutputStream(zipOutName); // 輸出Zip文件流ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(outputStream); // 設置壓縮等級zipArchiveOutputStream.setLevel(level); // 設置壓縮的字符編碼zipArchiveOutputStream.setEncoding("UTF-8"); // 循環壓縮各個路徑的文件for (String path : paths) {File temp = new File(path);compress(parallelScatterZipCreator, temp, temp.getName());} // 將數據寫入zip輸出流parallelScatterZipCreator.writeTo(zipArchiveOutputStream); // 相關流的關閉zipArchiveOutputStream.close();outputStream.close();}/*** 自定義線程創建工廠** @param zipOutName* @param factory 線程創建工廠* @param level 壓縮等級* @param paths* @throws IOException* @throws ExecutionException* @throws InterruptedException*/public static void compressFiles(String zipOutName, ThreadFactory factory, int level, String... paths) throws IOException, ExecutionException, InterruptedException {ExecutorService executor = new ThreadPoolExecutor(5, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(20), factory, new ThreadPoolExecutor.CallerRunsPolicy());compressFiles(zipOutName, executor, level, paths);}/*** 遍歷壓縮** @param parallelScatterZipCreator 線程池壓縮對象* @param inputFile 將要壓縮的文件路徑,絕對路徑* @param relativePath 相對與壓縮包內的路徑* @throws IOException* @throws ExecutionException* @throws InterruptedException*/protected static void compress(ParallelScatterZipCreator parallelScatterZipCreator, File inputFile, String relativePath) throws IOException, ExecutionException, InterruptedException { // 文件流為空,返回if (inputFile == null) {return;} // 文件為文件夾,遞歸遍歷文件if (inputFile.isDirectory()) { // 獲取文件內的所有文件File[] files = inputFile.listFiles();if (files == null) {return;} // 遍歷處理文件for (File file : files) {if (file.isDirectory()) {compress(parallelScatterZipCreator, new File(inputFile.getAbsolutePath() + "/" + file.getName()), relativePath + "/" + file.getName());} else { // 轉化為InputStreamSupplier對象final InputStreamSupplier inputStreamSupplier = () -> {try {return new FileInputStream(file);} catch (FileNotFoundException e) {e.printStackTrace();return new NullInputStream(0);}}; // 添加ZipArchiveEntity對象,這里的構造函數的值,name屬性,是相對于zip文件內的路徑ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(relativePath + "/" + file.getName()); // 設置壓縮算法zipArchiveEntry.setMethod(ZipArchiveEntry.DEFLATED); // 設置未壓縮文件的大小zipArchiveEntry.setSize(file.length()); // 添加添加ZipArchiveEntity對象到多線程壓縮中parallelScatterZipCreator.addArchiveEntry(zipArchiveEntry, inputStreamSupplier);}}} else { // 當是文件時,直接處理final InputStreamSupplier inputStreamSupplier = () -> {try {return new FileInputStream(inputFile);} catch (FileNotFoundException e) {e.printStackTrace();return new NullInputStream(0);}};ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(relativePath + "/" + inputFile.getName());zipArchiveEntry.setMethod(ZipArchiveEntry.DEFLATED);zipArchiveEntry.setSize(inputFile.length());parallelScatterZipCreator.addArchiveEntry(zipArchiveEntry, inputStreamSupplier);}} }總結
以上是生活随笔為你收集整理的Java多线程Zip压缩的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3D MAX入门训练(1)放样制作胡萝卜
- 下一篇: Java8 Zip 压缩与解压缩