欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用java API實現(xiàn)zip遞歸壓縮和解壓文件夾

 更新時間:2020年08月10日 08:55:53   作者:字母哥博客  
這篇文章主要介紹了使用java API實現(xiàn)zip遞歸壓縮文件夾及解壓,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、概述

在本篇文章中,給大家介紹一下如何將文件進行zip壓縮以及如何對zip包解壓。所有這些都是使用Java提供的核心庫java.util.zip來實現(xiàn)的。

二、壓縮文件

首先我們來學習一個簡單的例子-壓縮單個文件。將一個名為test1.txt的文件壓縮到一個名為Compressed.zip的zip文件中。

public class ZipFile {
 public static void main(String[] args) throws IOException {
  
  //輸出壓縮包
  FileOutputStream fos = new FileOutputStream("src/main/resources/compressed.zip");
  ZipOutputStream zipOut = new ZipOutputStream(fos);

  //被壓縮文件
  File fileToZip = new File("src/main/resources/test1.txt");
  FileInputStream fis = new FileInputStream(fileToZip);
  
  //向壓縮包中添加文件
  ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
  zipOut.putNextEntry(zipEntry);
  byte[] bytes = new byte[1024];
  int length;
  while((length = fis.read(bytes)) >= 0) {
   zipOut.write(bytes, 0, length);
  }
  zipOut.close();
  fis.close();
  fos.close();
 }
}

三、壓縮多個文件

接下來,我們看看如何將多個文件壓縮為一個zip文件。我們將把test1.txttest2.txt壓縮成multiCompressed.zip

public class ZipMultipleFiles {
 public static void main(String[] args) throws IOException {
  List<String> srcFiles = Arrays.asList("src/main/resources/test1.txt", "src/main/resources/test2.txt");
  FileOutputStream fos = new FileOutputStream("src/main/resources/multiCompressed.zip");
  ZipOutputStream zipOut = new ZipOutputStream(fos);
  //向壓縮包中添加多個文件
  for (String srcFile : srcFiles) {
   File fileToZip = new File(srcFile);
   FileInputStream fis = new FileInputStream(fileToZip);
   ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
   zipOut.putNextEntry(zipEntry);
 
   byte[] bytes = new byte[1024];
   int length;
   while((length = fis.read(bytes)) >= 0) {
    zipOut.write(bytes, 0, length);
   }
   fis.close();
  }
  zipOut.close();
  fos.close();
 }
}

四、壓縮目錄

下面的例子,我們將zipTest目錄及該目錄下的遞歸子目錄文件,全都壓縮到dirCompressed.zip中

public class ZipDirectory {
 public static void main(String[] args) throws IOException, FileNotFoundException {
  //被壓縮的文件夾
  String sourceFile = "src/main/resources/zipTest"; 
  //壓縮結(jié)果輸出,即壓縮包
  FileOutputStream fos = new FileOutputStream("src/main/resources/dirCompressed.zip");
  ZipOutputStream zipOut = new ZipOutputStream(fos);
  File fileToZip = new File(sourceFile);
  //遞歸壓縮文件夾
  zipFile(fileToZip, fileToZip.getName(), zipOut);
  //關(guān)閉輸出流
  zipOut.close();
  fos.close();
 }
  

 /**
  * 將fileToZip文件夾及其子目錄文件遞歸壓縮到zip文件中
  * @param fileToZip 遞歸當前處理對象,可能是文件夾,也可能是文件
  * @param fileName fileToZip文件或文件夾名稱
  * @param zipOut 壓縮文件輸出流
  * @throws IOException
  */
 private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException {
  //不壓縮隱藏文件夾
  if (fileToZip.isHidden()) {
   return;
  }
  //判斷壓縮對象如果是一個文件夾
  if (fileToZip.isDirectory()) {
   if (fileName.endsWith("/")) {
    //如果文件夾是以“/”結(jié)尾,將文件夾作為壓縮箱放入zipOut壓縮輸出流
    zipOut.putNextEntry(new ZipEntry(fileName));
    zipOut.closeEntry();
   } else {
    //如果文件夾不是以“/”結(jié)尾,將文件夾結(jié)尾加上“/”之后作為壓縮箱放入zipOut壓縮輸出流
    zipOut.putNextEntry(new ZipEntry(fileName + "/"));
    zipOut.closeEntry();
   }
   //遍歷文件夾子目錄,進行遞歸的zipFile
   File[] children = fileToZip.listFiles();
   for (File childFile : children) {
    zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);
   }
   //如果當前遞歸對象是文件夾,加入ZipEntry之后就返回
   return;
  }
  //如果當前的fileToZip不是一個文件夾,是一個文件,將其以字節(jié)碼形式壓縮到壓縮包里面
  FileInputStream fis = new FileInputStream(fileToZip);
  ZipEntry zipEntry = new ZipEntry(fileName);
  zipOut.putNextEntry(zipEntry);
  byte[] bytes = new byte[1024];
  int length;
  while ((length = fis.read(bytes)) >= 0) {
   zipOut.write(bytes, 0, length);
  }
  fis.close();
 }
}
  • 要壓縮子目錄及其子目錄文件,所以需要遞歸遍歷
  • 每次遍歷找到的是目錄時,我們都將其名稱附加“/”,并將其以ZipEntry保存到壓縮包中,從而保持壓縮的目錄結(jié)構(gòu)。
  • 每次遍歷找到的是文件時,將其以字節(jié)碼形式壓縮到壓縮包里面

五、解壓縮zip壓縮包

下面為大家舉例講解解壓縮zip壓縮包。在此示例中,我們將compressed.zip解壓縮到名為unzipTest的新文件夾中。

public class UnzipFile {
 public static void main(String[] args) throws IOException {
  //被解壓的壓縮文件
  String fileZip = "src/main/resources/unzipTest/compressed.zip";
  //解壓的目標目錄
  File destDir = new File("src/main/resources/unzipTest");

  byte[] buffer = new byte[1024];
  ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));
  //獲取壓縮包中的entry,并將其解壓
  ZipEntry zipEntry = zis.getNextEntry();
  while (zipEntry != null) {
   File newFile = newFile(destDir, zipEntry);
   FileOutputStream fos = new FileOutputStream(newFile);
   int len;
   while ((len = zis.read(buffer)) > 0) {
    fos.write(buffer, 0, len);
   }
   fos.close();
   //解壓完成一個entry,再解壓下一個
   zipEntry = zis.getNextEntry();
  }
  zis.closeEntry();
  zis.close();
 }
 //在解壓目標文件夾,新建一個文件
 public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
  File destFile = new File(destinationDir, zipEntry.getName());

  String destDirPath = destinationDir.getCanonicalPath();
  String destFilePath = destFile.getCanonicalPath();

  if (!destFilePath.startsWith(destDirPath + File.separator)) {
   throw new IOException("該解壓項在目標文件夾之外: " + zipEntry.getName());
  }

  return destFile;
 }
}

總結(jié)

到此這篇關(guān)于使用java API實現(xiàn)zip遞歸壓縮文件夾及解壓的文章就介紹到這了,更多相關(guān)java API實現(xiàn)zip遞歸壓縮文件夾及解壓內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot整合Web開發(fā)之文件上傳與@ControllerAdvice

    SpringBoot整合Web開發(fā)之文件上傳與@ControllerAdvice

    @ControllerAdvice注解是Spring3.2中新增的注解,學名是Controller增強器,作用是給Controller控制器添加統(tǒng)一的操作或處理。對于@ControllerAdvice,我們比較熟知的用法是結(jié)合@ExceptionHandler用于全局異常的處理,但其作用不止于此
    2022-08-08
  • idea新建mapper.xml文件詳細步驟如:mybatis-config

    idea新建mapper.xml文件詳細步驟如:mybatis-config

    這篇文章主要介紹了idea新建xml模板設(shè)置,例如:mybatis-config,本文分步驟通過圖文并茂的形式給大家介紹的非常詳細,需要的朋友可以參考下
    2023-07-07
  • Java在利用反射條件下替換英文字母中的值

    Java在利用反射條件下替換英文字母中的值

    今天小編就為大家分享一篇關(guān)于Java在利用反射條件下替換英文字母中的值,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Java?List排序4種寫法整理

    Java?List排序4種寫法整理

    這篇文章主要給大家介紹了關(guān)于Java?List排序4種寫法整理的相關(guān)資料,在有的時候我們會需要對List進行排序,在Java中如何實現(xiàn)呢,本文記錄一下Java中對List的幾種排序方式,需要的朋友可以參考下
    2023-08-08
  • 詳解使用Spring Boot的AOP處理自定義注解

    詳解使用Spring Boot的AOP處理自定義注解

    本篇文章主要介紹了詳解使用Spring Boot的AOP處理自定義注解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • JWT Token實現(xiàn)方法及步驟詳解

    JWT Token實現(xiàn)方法及步驟詳解

    這篇文章主要介紹了JWT Token實現(xiàn)方法及步驟詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • Java9新特性中的模塊化詳解

    Java9新特性中的模塊化詳解

    今天介紹一個Java?9的功能,模塊化(Modular),這可能使Java有史以來最大的Feature,對Java9模塊化相關(guān)知識感興趣的朋友一起看看吧
    2022-03-03
  • Java垃圾回收機制簡述

    Java垃圾回收機制簡述

    這篇文章主要為大家詳細介紹了Java垃圾回收機制的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Spring?Boot?利用?XML?方式整合?MyBatis

    Spring?Boot?利用?XML?方式整合?MyBatis

    這篇文章主要介紹了Spring?Boot?利用?XML?方式整合?MyBatis,文章圍繞主題的相關(guān)資料展開詳細的內(nèi)容介紹,具有一定的參考價值,組要的小伙伴可以參考一下
    2022-05-05
  • Sublime Text 打開Java文檔中文亂碼的解決方案

    Sublime Text 打開Java文檔中文亂碼的解決方案

    這篇文章主要介紹了Sublime Text 中文亂碼的解決方案,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-12-12

最新評論