專案場景:
Java將檔案或者檔案夾壓縮成zip(修復檔案夾中存在多個檔案報Stream Closed錯誤問題)
問題描述:
最近的專案需要將多級檔案夾壓縮成zip,網上找了幾個工具類,都會報錯,所以找個其中這一個文章中的工具類改進了后,已解決檔案夾中存在多個檔案報Stream Closed錯誤問題,
ps:java新手,不知道這樣改有沒有問題,希望大神們幫忙研究下
原工具類文章鏈接:
原因分析:
zos.close();
改為在呼叫最外層try-catch-finally里只關閉一次,避免閉關多次關閉后,再次執行報錯,
原有的邏輯沒有洗掉,改為注釋掉了
解決方案:
package utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// 壓縮zip方法
public class ZipUtil {
private static final int BUFFER_SIZE = 2 * 1024;
private static Logger logger = LoggerFactory.getLogger(ZipUtil.class);
/**
* 壓縮成ZIP 方法1
*
* @param srcDir
* 壓縮檔案夾路徑
* @param out
* 壓縮檔案輸出流
* @param KeepDirStructure
* 是否保留原來的目錄結構,true:保留目錄結構;
* false:所有檔案跑到壓縮包根目錄下(注意:不保留目錄結構可能會出現同名檔案,會壓縮失敗)
* @throws RuntimeException
* 壓縮失敗會拋出運行時例外
*/
public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure){
long start = System.currentTimeMillis();
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(out);
File sourceFile = new File(srcDir);
compress(sourceFile, zos, sourceFile.getName(), KeepDirStructure);
long end = System.currentTimeMillis();
logger.debug("壓縮完成,耗時:" + (end - start) + " ms");
} catch (Exception e) {
e.printStackTrace();
logger.error("系統例外:",e);
} finally {
//zos != null zos.close(); 并不是判斷zos是否被關閉 而是這個物件是否創建成功
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
logger.error("系統例外:",e);
}
}
}
}
/**
* 壓縮成ZIP 方法2
*
* @param srcFiles
* 需要壓縮的檔案串列
* @param out
* 壓縮檔案輸出流
* @throws RuntimeException
* 壓縮失敗會拋出運行時例外
*/
public static void toZip(List<File> srcFiles, OutputStream out){
long start = System.currentTimeMillis();
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(out);
for (File srcFile : srcFiles) {
byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(srcFile.getName()));
int len;
FileInputStream in = new FileInputStream(srcFile);
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
long end = System.currentTimeMillis();
logger.debug("壓縮完成,耗時:" + (end - start) + " ms");
} catch (Exception e) {
e.printStackTrace();
logger.error("系統例外:",e);
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
logger.error("系統例外:",e);
}
}
}
}
/**
* 壓縮成ZIP 方法2
*
* @param srcFiles
* 需要壓縮的檔案串列
* @param out
* 壓縮檔案輸出流
* @throws RuntimeException
* 壓縮失敗會拋出運行時例外
*/
public static void toZip(File[] srcFiles, OutputStream out){
long start = System.currentTimeMillis();
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(out);
for (File srcFile : srcFiles) {
byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(srcFile.getName()));
int len;
FileInputStream in = new FileInputStream(srcFile);
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
long end = System.currentTimeMillis();
logger.debug("壓縮完成,耗時:" + (end - start) + " ms");
} catch (Exception e) {
e.printStackTrace();
logger.error("系統例外:",e);
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
logger.error("系統例外:",e);
}
}
}
}
/**
*
* 遞回壓縮方法
*
* @param sourceFile
* 源檔案
*
* @param zos
* zip輸出流
*
* @param name
* 壓縮后的名稱
*
* @param KeepDirStructure
* 是否保留原來的目錄結構,true:保留目錄結構;
*
* false:所有檔案跑到壓縮包根目錄下(注意:不保留目錄結構可能會出現同名檔案,會壓縮失敗)
*
* @throws Exception
*
*/
//工具類有邏輯問題 已更改添加注釋 便于理解
private static void compress(File sourceFile, ZipOutputStream zos, String name,
boolean KeepDirStructure) throws IOException {
byte[] buf = new byte[BUFFER_SIZE];
//例外改為向上層拋出 避免 zos.close(); 沒有執行
// try {
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);
}
}
}
}
// }catch(Exception e){
// e.printStackTrace();
// logger.error("系統例外:",e);
// } finally {
// 遞回時,不需要把zip輸出流關閉,zip輸出流的關閉應該是在呼叫完遞回方法后面關閉 這里不再關閉 在最外層關閉
// if (zos != null) {
// try {
// zos.close();
// } catch (IOException e) {
// e.printStackTrace();
// logger.error("系統例外:",e);
// }
//
// }
// }
}
public static void main(String[] args) throws Exception {
/** 測驗壓縮方法1 */
FileOutputStream fos1 = new FileOutputStream(new File("F:/11.zip"));
ZipUtil.toZip("F:/driverDailyInspection", fos1, true);
/** 測驗壓縮方法2 */
// List<File> fileList = new ArrayList<>();
// fileList.add(new File("E:/emailTemplate.html"));
// fileList.add(new File("E:/logobottom.jpg"));
// FileOutputStream fos2 = new FileOutputStream(new File("E:/mytest02.zip"));
// ZipUtil.toZip(fileList, fos2);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/131742.html
標籤:AI
