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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用java压缩文件成zip——三种方式压缩文件速度对比

發布時間:2023/12/18 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用java压缩文件成zip——三种方式压缩文件速度对比 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 寫在前面
  • 使用Buffered緩沖流壓縮
  • 使用nio的FileChannel壓縮
  • 使用apache.commons包下的并行壓縮方式壓縮

寫在前面

對于大文件批量壓縮的問題,實際上是非常消耗時間的,怎么能提高壓縮速度呢?
在這里提供了三種方式壓縮文件,咱們對比一下哪一個壓縮速度更快。

在這里提供了三個文件,壓縮后的大小大概是1.1GB左右,咱們測試一下每一種壓縮方式的壓縮時間。

使用Buffered緩沖流壓縮

/*** zip文件壓縮* @param inputFile 待壓縮文件夾/文件名* @param outputFile 生成的壓縮包名字*/public static void ZipCompress(String inputFile, String outputFile) throws Exception {//創建zip輸出流ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputFile));//創建緩沖輸出流BufferedOutputStream bos = new BufferedOutputStream(out);File input = new File(inputFile);compress(out, bos, input,null);bos.close();out.close();}/*** @param name 壓縮文件名,可以寫為null保持默認*///遞歸壓縮public static void compress(ZipOutputStream out, BufferedOutputStream bos, File input, String name) throws IOException {if (name == null) {name = input.getName();}//如果路徑為目錄(文件夾)if (input.isDirectory()) {//取出文件夾中的文件(或子文件夾)File[] flist = input.listFiles();if (flist.length == 0)//如果文件夾為空,則只需在目的地zip文件中寫入一個目錄進入{out.putNextEntry(new ZipEntry(name + "/"));} else//如果文件夾不為空,則遞歸調用compress,文件夾中的每一個文件(或文件夾)進行壓縮{for (int i = 0; i < flist.length; i++) {compress(out, bos, flist[i], name + "/" + flist[i].getName());}}} else//如果不是目錄(文件夾),即為文件,則先寫入目錄進入點,之后將文件寫入zip文件中{out.putNextEntry(new ZipEntry(name));FileInputStream fos = new FileInputStream(input);BufferedInputStream bis = new BufferedInputStream(fos);int len;//將源文件寫入到zip文件中byte[] buf = new byte[1024];while ((len = bis.read(buf)) != -1) {bos.write(buf,0,len);}bis.close();fos.close();}}public static void main(String[] args) {try {//壓縮System.out.println("開始壓縮");long date = new Date().getTime();ZipCompress("D:\\fileTest", "D:\\TestbyYTT.zip");System.out.println("壓縮結束用時" + (new Date().getTime() - date));} catch (Exception e) {e.printStackTrace();}} 執行了三次,結果如下: 開始壓縮 壓縮結束用時46835開始壓縮 壓縮結束用時48016開始壓縮 壓縮結束用時47280

結論:平均就是47秒左右。

使用nio的FileChannel壓縮

/*** NIO方式壓縮*/public static File fileToZip(String sourceFilePath, String zipFilePath, String fileName) throws IOException {File sourceFile = new File(sourceFilePath);FileInputStream fis = null;BufferedInputStream bis = null;FileOutputStream fos = null;ZipOutputStream zos = null;WritableByteChannel writableByteChannel = null;File zipFile = new File(zipFilePath + "/" + fileName);if (!sourceFile.exists()) {System.out.println("待壓縮的文件目錄:" + sourceFilePath + "不存在.");} else {try {if (zipFile.exists()) {Files.delete(zipFile.toPath());}File[] sourceFiles = sourceFile.listFiles();if (null == sourceFiles || sourceFiles.length < 1) {System.out.println("待壓縮的文件目錄:" + sourceFilePath + "里面不存在文件,無需壓縮.");} else {fos = new FileOutputStream(zipFile);zos = new ZipOutputStream(new BufferedOutputStream(fos));writableByteChannel = Channels.newChannel(zos) ;for (File file : sourceFiles) {//創建ZIP實體,并添加進壓縮包ZipEntry zipEntry = new ZipEntry(file.getName());zos.putNextEntry(zipEntry);//讀取待壓縮的文件并寫進壓縮包里fis = new FileInputStream(file);FileChannel fileChannel = fis.getChannel();fileChannel.transferTo(0, file.length() -1,writableByteChannel);}}} finally {//關閉流if (null != bis) {bis.close();}if (fis != null) {fis.close();}if (zos != null) {zos.close();}if (fos != null) {fos.close();}if (null != writableByteChannel) {writableByteChannel.close();}}}return zipFile;} 執行了三次,結果如下: 開始壓縮 壓縮結束用時36929開始壓縮 壓縮結束用時37783開始壓縮 壓縮結束用時36246

結論:平均36秒左右。
注意!transferTo方法最大能讀取2G的文件!

使用apache.commons包下的并行壓縮方式壓縮

/*** @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();ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("compressFileList-pool-").build();ExecutorService executor = new ThreadPoolExecutor(5, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(20), factory);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()));} 執行了三次,結果如下: 開始壓縮 ParallelCompressUtil->ParallelCompressUtil-> info:{}{"compressionElapsed":22642,"mergingElapsed":3802} 壓縮結束用時27261開始壓縮 ParallelCompressUtil->ParallelCompressUtil-> info:{}{"compressionElapsed":21958,"mergingElapsed":3406} 壓縮結束用時25524開始壓縮 ParallelCompressUtil->ParallelCompressUtil-> info:{}{"compressionElapsed":22076,"mergingElapsed":3668} 壓縮結束用時25977

結論:平均26秒左右。

總結

以上是生活随笔為你收集整理的使用java压缩文件成zip——三种方式压缩文件速度对比的全部內容,希望文章能夠幫你解決所遇到的問題。

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