上周做了一個需求,要求將數據庫保存的 html 界面取出后將服務器下的css和js文件一起打包壓縮為ZIP文件,返回給前臺;在數據庫中保存的是html標簽,查出后,我把這些內容寫入css和js等其他文件所在目錄的一個文件內,然后將這整個文件夾壓縮打包下載,解決過程中遇到了下載出來后并沒有保存層級目錄,在查了好久方法后完成了如下版本,已經可以正常下載并保留層級目錄。
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
|
//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); } } |
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
60
61
62
|
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 的文件 層級目錄都有保留
總結
到此這篇關于Java將文件夾保留目錄打包為 ZIP 壓縮包并下載的教程詳解的文章就介紹到這了,更多相關java將文件打包為zip壓縮包下載內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/u010356768/article/details/90756742