日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java多线程Zip压缩

發布時間:2023/12/8 java 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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压缩的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。