Java實現(xiàn)文件壓縮為zip和解壓zip壓縮包
更新時間:2022年06月21日 17:04:25 作者:共飲一杯無
這篇文章主要為大家介紹了Java如何實現(xiàn)將文件壓縮為zip以及解壓zip壓縮包,文中的示例代碼講解詳細,感興趣的小伙伴可以動手嘗試一下
壓縮成.zip
代碼如下:
/**
* 壓縮成ZIP
*
* @param srcDir 壓縮文件夾路徑
* @param out 壓縮文件輸出流
* @throws RuntimeException 壓縮失敗會拋出運行時異常
*/
public static void toZip(String srcDir, OutputStream out) throws RuntimeException {
long start = System.currentTimeMillis();
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(out);
File sourceFile = new File(srcDir);
compress(sourceFile, zos, sourceFile.getName(), false);
long end = System.currentTimeMillis();
System.out.println("壓縮完成,耗時:" + (end - start) + " ms");
} catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils", e);
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 遞歸壓縮方法
*
* @param sourceFile 源文件
* @param zos zip輸出流
* @param name 壓縮后的名稱
* @param KeepDirStructure 是否保留原來的目錄結構,true:保留目錄結構;
* <p>
* false:所有文件跑到壓縮包根目錄下(注意:不保留目錄結構可能會出現(xiàn)同名文件,會壓縮失敗)
* @throws Exception
*/
private static void compress(File sourceFile, ZipOutputStream zos, String name,
boolean KeepDirStructure) throws Exception {
byte[] buf = new byte[BUFFER_SIZE];
if (sourceFile.isFile()) {
// 向zip輸出流中添加一個zip實體,構造器中name為zip實體的文件的名字
zos.putNextEntry(new ZipEntry(name));
// copy文件到zip輸出流中
int len;
FileInputStream in = new FileInputStream(sourceFile);
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
// Complete the entry
zos.closeEntry();
in.close();
} else {
File[] listFiles = sourceFile.listFiles();
if (listFiles == null || listFiles.length == 0) {
// 需要保留原來的文件結構時,需要對空文件夾進行處理
if (KeepDirStructure) {
// 空文件夾的處理
zos.putNextEntry(new ZipEntry(name + "/"));
// 沒有文件,不需要文件的copy
zos.closeEntry();
}
} else {
for (File file : listFiles) {
// 判斷是否需要保留原來的文件結構
if (KeepDirStructure) {
// 注意:file.getName()前面需要帶上父文件夾的名字加一斜杠,
// 不然最后壓縮包中就不能保留原來的文件結構,即:所有文件都跑到壓縮包根目錄下了
compress(file, zos, name + "/" + file.getName(), KeepDirStructure);
} else {
compress(file, zos, file.getName(), KeepDirStructure);
}
}
}
}
}
測試驗證代碼:
/**
* 測試打包本地的Navicat,輸出為zip文件
* @throws Exception
*/
@Test
public void test() throws Exception {
//Navicat路徑
String inDir = "E:\\developer\\Navicat";
//打包后輸出路徑
String outDir = "E:\\developer\\NavicatZip\\Navicat.zip";
OutputStream fileOutputStream = new FileOutputStream(new File(outDir));
ZipUtils.toZip(inDir, fileOutputStream);
}
打包前后的文件如下:


解壓.zip
代碼如下:
/**
* 解壓zip文件到指定目錄
* @param fileZip
* @param path_to_dest
* @throws IOException
*/
public static void readZip(String fileZip,String path_to_dest) throws IOException {
try (FileInputStream fis = new FileInputStream(fileZip);
ZipInputStream zis =
new ZipInputStream(new BufferedInputStream(fis))) {
ZipEntry entry;
// 從ZipInputStream讀取每個條目,直到沒有
// 發(fā)現(xiàn)更多條目,返回值為空
// getNextEntry()方法。
while ((entry = zis.getNextEntry()) != null) {
System.out.println("Unzipping: " + entry.getName());
int size;
byte[] buffer = new byte[2048];
File fileOut = new File(path_to_dest+"\\"+entry.getName());
try (FileOutputStream fos =
new FileOutputStream(fileOut);
BufferedOutputStream bos =
new BufferedOutputStream(fos, buffer.length)) {
while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, size);
}
bos.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
測試驗證代碼:
/**
* 測試解壓本地zip文件
* @throws Exception
*/
@Test
public void readZip() throws Exception {
//解壓后路徑
String path_to_dest = "E:\\developer\\NavicatUnzip";
//zip文件路徑
String fileZip = "E:\\developer\\NavicatZip\\Navicat.zip";
ZipUtils.readZip(fileZip, path_to_dest);
}
解壓前后的文件如下:


以上就是Java實現(xiàn)文件壓縮為zip和解壓zip壓縮包的詳細內容,更多關于Java文件壓縮為zip的資料請關注腳本之家其它相關文章!
相關文章
Java利用happen-before規(guī)則如何實現(xiàn)共享變量的同步操作詳解
這篇文章主要給大家介紹了關于Java利用happen-before規(guī)則實現(xiàn)共享變量的同步操作的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用java具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-06-06
idea maven 項目src下的配置文件沒有同步至target的解決操作
這篇文章主要介紹了idea maven 項目src下的配置文件沒有同步至target的解決操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Java方法遞歸的形式和常見遞歸算法(方法遞歸結合File類查找文件)
方法遞歸方法直接調用自己或者間接調用自己的形式稱為方法遞歸( recursion),遞歸做為一種算法在程序設計語言中廣泛應用,這篇文章主要介紹了Java方法遞歸的形式和常見遞歸算法-方法遞歸結合File類查找文件,需要的朋友可以參考下2023-02-02
Java Spring循環(huán)依賴原理與bean的生命周期圖文案例詳解
這篇文章主要介紹了Spring循環(huán)依賴原理與bean的生命周期圖文案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-09-09

