1. 程式人生 > 實用技巧 >JAVA 實現將多目錄多層級檔案打成ZIP包後保留層級目錄下載 ZIP壓縮 下載

JAVA 實現將多目錄多層級檔案打成ZIP包後保留層級目錄下載 ZIP壓縮 下載

將資料夾保留目錄打包為 ZIP 壓縮包並下載

上週做了一個需求,要求將資料庫儲存的 html 介面取出後將伺服器下的css和js檔案一起打包壓縮為ZIP檔案,返回給前臺;在資料庫中儲存的是html標籤,查出後,我把這些內容寫入css和js等其他檔案所在目錄的一個檔案內,然後將這整個資料夾壓縮打包下載,解決過程中遇到了下載出來後並沒有儲存層級目錄,在查了好久方法後完成了如下版本,已經可以正常下載並保留層級目錄。

話不多說,直接上程式碼,有不足的地方希望大哥們提出來一起探討
     //ZIP檔案包壓縮下載
@Override
public void downloadZip(String id,HttpServletResponse response) {
String zipPath = "你的路徑";
File file = new File(zipPath,"index.html");//建立指定目錄和檔名稱的檔案物件
BufferedWriter bw = null;//建立緩衝流
try {
//校驗檔案目錄是否存在,檔案是否存在
chenkFile(file,zipPath);
//這一步是我將指定內容從資料庫寫入檔案
ModuleInfo moduleInfo = moduleDao.getByModId(id); bw = new BufferedWriter(new FileWriter(file));
//把內容寫入臨時檔案中
bw.write(moduleInfo.getContent());
//此處不能刪除,要關閉一次 不關閉無法寫入內容 導致壓縮包內檔案無內容
bw.flush();
bw.close();
//將目標檔案壓縮為ZIP並下載
ZipUtil.zip(zipPath,response);
//刪除檔案(防止下一次壓縮時有重複檔名)
file.delete();
} catch (Exception e) {
log.error("html壓縮"+e.getMessage(),e);
}finally {
//這是我寫的IO流關閉工具類 如需要可以看我關於IO流關閉的文章
IOCloseUtils.ioClose(bw);
}
} //判斷檔案目錄和檔案是否存在 如否則新建
public void chenkFile(File file,String path){
try {
if (file.exists()){//如果目錄存在
if (!file.isDirectory()){//如果檔案不存在
file.createNewFile();//建立檔案
}
}else {//如果目錄不存在
File file1 = new File(path);//建立指定目錄檔案物件
file1.mkdirs();//建立目錄
file.createNewFile();//建立檔案
}
} catch (IOException e) {
log.error(e.getMessage(),e);
}
}
    public static void zip(String sourceFileName, HttpServletResponse response){
ZipOutputStream out = null;
BufferedOutputStream bos = null;
try {
//將zip以流的形式輸出到前臺
response.setHeader("content-type", "application/octet-stream");
response.setCharacterEncoding("utf-8");
// 設定瀏覽器響應頭對應的Content-disposition
//引數中 testZip 為壓縮包檔名,尾部的.zip 為檔案字尾
response.setHeader("Content-disposition",
"attachment;filename=" + new String("testZip".getBytes("gbk"), "iso8859-1")+".zip");
//建立zip輸出流
out = new ZipOutputStream(response.getOutputStream());
//建立緩衝輸出流
bos = new BufferedOutputStream(out);
File sourceFile = new File(sourceFileName);
//呼叫壓縮函式
compress(out, bos, sourceFile, sourceFile.getName());
out.flush();
log.info("壓縮完成");
} catch (Exception e) {
log.error("ZIP壓縮異常:"+e.getMessage(),e);
} finally {
IOCloseUtils.ioClose(bos,out);
}
} public static void compress(ZipOutputStream out, BufferedOutputStream bos, File sourceFile, String base){
FileInputStream fos = null;
BufferedInputStream bis = null;
try {
//如果路徑為目錄(資料夾)
if (sourceFile.isDirectory()) {
//取出資料夾中的檔案(或子資料夾)
File[] flist = sourceFile.listFiles();
if (flist.length == 0) {//如果資料夾為空,則只需在目的地zip檔案中寫入一個目錄進入點
out.putNextEntry(new ZipEntry(base + "/"));
} else {//如果資料夾不為空,則遞迴呼叫compress,資料夾中的每一個檔案(或資料夾)進行壓縮
for (int i = 0; i < flist.length; i++) {
compress(out, bos, flist[i], base + "/" + flist[i].getName());
}
}
} else {//如果不是目錄(資料夾),即為檔案,則先寫入目錄進入點,之後將檔案寫入zip檔案中
out.putNextEntry(new ZipEntry(base));
fos = new FileInputStream(sourceFile);
bis = new BufferedInputStream(fos); int tag;
//將原始檔寫入到zip檔案中
while ((tag = bis.read()) != -1) {
out.write(tag);
} bis.close();
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
IOCloseUtils.ioClose(bis,fos);
}
}

如圖下載後如圖所示,名為 testZip.zip 的檔案 層級目錄都有保留

發文不易,各位看官關注下公眾號,謝謝!!!

關注公眾號回覆 ”jar包“ 獲取 IDEA2020最新版破解jar包

教程在這 點選這裡跳轉教程

求個關注不過分叭~~~