一、概述
在本篇文章中,給大家介紹一下如何將檔案進行zip壓縮以及如何對zip包解壓,所有這些都是使用Java提供的核心庫java.util.zip來實作的,
二、壓縮檔案
首先我們來學習一個簡單的例子-壓縮單個檔案,將一個名為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.txt和test2.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";
//壓縮結果輸出,即壓縮包
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的新檔案夾中,
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;
}
}
歡迎關注我的博客,里面有很多精品合集
- 本文轉載注明出處(必須帶連接,不能只轉文字):字母哥博客,
覺得對您有幫助的話,幫我點贊、分享!您的支持是我不竭的創作動力! ,另外,筆者最近一段時間輸出了如下的精品內容,期待您的關注,
- 《手摸手教你學Spring Boot2.0》
- 《Spring Security-JWT-OAuth2一本通》
- 《實戰前后端分離RBAC權限管理系統》
- 《實戰SpringCloud微服務從青銅到王者》
- 《VUE深入淺出系列》
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/89886.html
標籤:Java
上一篇:Simple zip compressor to instead WinRAR.
下一篇:ssm框架ajax發送資料報錯Content type 'application/json' not supported
