一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 使用java API實現zip遞歸壓縮和解壓文件夾

使用java API實現zip遞歸壓縮和解壓文件夾

2020-08-10 15:48字母哥博客 Java教程

這篇文章主要介紹了使用java API實現zip遞歸壓縮文件夾及解壓,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、概述

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

二、壓縮文件

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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中

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class ZipDirectory {
 public static void main(String[] args) throws IOException, FileNotFoundException {
  //被壓縮的文件夾
  String sourceFile = "src/main/resources/zipTest";
  //壓縮結果輸出,即壓縮包
  FileOutputStream fos = new FileOutputStream("src/main/resources/dirCompressed.zip");
  ZipOutputStream zipOut = new ZipOutputStream(fos);
  File fileToZip = new File(sourceFile);
  //遞歸壓縮文件夾
  zipFile(fileToZip, fileToZip.getName(), zipOut);
  //關閉輸出流
  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("/")) {
    //如果文件夾是以“/”結尾,將文件夾作為壓縮箱放入zipOut壓縮輸出流
    zipOut.putNextEntry(new ZipEntry(fileName));
    zipOut.closeEntry();
   } else {
    //如果文件夾不是以“/”結尾,將文件夾結尾加上“/”之后作為壓縮箱放入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不是一個文件夾,是一個文件,將其以字節碼形式壓縮到壓縮包里面
  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保存到壓縮包中,從而保持壓縮的目錄結構。
  • 每次遍歷找到的是文件時,將其以字節碼形式壓縮到壓縮包里面

五、解壓縮zip壓縮包

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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;
 }
}

總結

到此這篇關于使用java API實現zip遞歸壓縮文件夾及解壓的文章就介紹到這了,更多相關java API實現zip遞歸壓縮文件夾及解壓內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://www.cnblogs.com/zimug/p/13467639.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91普通话国产对白在线 | 狠狠香蕉 | 成人亚洲精品一区 | 亚洲精品一区二区三区在线看 | 精品91| 国产亚洲一区二区三区 | 被巨大黑人的翻白眼 | 欧美3p大片在线观看完整版 | 国产一卡2卡3卡四卡国色天香 | 娇妻终于接受了3p的调教 | 成人综合网址 | 亚洲一区二区精品推荐 | 国产精品视频久久久 | 农村老妇1乱69系列小说 | 亚洲色图亚洲色图 | 深夜在线看 | 久久er国产精品免费观看2 | 国产98在线| 男人捅女人漫画 | 男人叼女人的痛爽视频免费 | 荡女人人爱全文免费阅读 | 日韩一级片免费观看 | chinesemature老女人 | 97秋霞| 99热这里只有精品免费 | jiizz亚洲护士厕所 | 香蕉视频在线观看网站 | 欧美人做人爱a全程免费 | 亚州精品视频 | 国产欧美精品一区二区三区–老狼 | 国产三级精品91三级在专区 | 小早川怜子息梦精在线播放 | 美女禁区视频无遮挡免费看 | 视频在线免费看 | 女女性恋爱视频入口 | 日本不卡免费新一二三区 | 40岁女人三级全黄 | 欧美日本一本线在线观看 | 加勒比伊人 | 毛片免费在线视频 | 欧美成人禁片在线观看俄罗斯 |