SpringBoot將多個文件夾進行壓縮的兩種方法(瀏覽器下載和另存為)
更新時間:2024年07月26日 09:57:40 作者:一紙紅塵輕似夢
Spring Boot項目通常不會自動對文件夾進行壓縮,不過,在打包應(yīng)用時,如果你使用了Maven或Gradle這樣的構(gòu)建工具,并且配置了相應(yīng)的插件,可以在打成jar或war包的時候?qū)⒁蕾嚨膸煳募喜⒉嚎s,本文介紹了SpringBoot將多個文件夾進行壓縮的兩種方法
1、將多個文件夾壓縮成一個壓縮包(壓縮到固定目錄)
import java.io.*; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class FileZipper { public static void main(String[] args) { // 示例使用 String zipFilePath = "C:\\Users\\guohu\\Desktop\\archive.zip"; List<File> fileList = List.of( new File("C:\\Users\\guohu\\Desktop\\新建文件夾 (8)\\1657269583419039746"), new File("C:\\Users\\guohu\\Desktop\\新建文件夾 (8)\\1657269583419039747"), new File("C:\\Users\\guohu\\Desktop\\新建文件夾 (8)\\1657269583419039748") ); // 將文件列表壓縮成壓縮包 boolean result = zipFiles(fileList, zipFilePath); if (result) { System.out.println("文件壓縮成功: " + zipFilePath); } else { System.out.println("壓縮文件失敗"); } } public static boolean zipFiles(List<File> fileList, String zipFilePath) { try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilePath))) { for (File file : fileList) { if (file.exists()) { compress(file, zos, file.getName(), true); } } return true; } catch (IOException e) { e.printStackTrace(); return false; } } private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure) throws IOException { byte[] buffer = new byte[4096]; if (sourceFile.isFile()) { try (FileInputStream fis = new FileInputStream(sourceFile)) { ZipEntry zipEntry; if (KeepDirStructure) { zipEntry = new ZipEntry(name); } else { zipEntry = new ZipEntry(sourceFile.getName()); } zos.putNextEntry(zipEntry); int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } zos.closeEntry(); } } else if (sourceFile.isDirectory()) { File[] files = sourceFile.listFiles(); if (files != null) { for (File file : files) { if (KeepDirStructure) { compress(file, zos, name + File.separator + file.getName(), KeepDirStructure); } else { compress(file, zos, file.getName(), KeepDirStructure); } } } } } }
2、將多個文件夾壓縮成一個壓縮包(通過瀏覽器下載)
List<File> fileList = Arrays.asList( new File("C:\\Users\\guohu\\Desktop\\新建文件夾 (8)\\1657269583419039746"), new File("C:\\Users\\guohu\\Desktop\\新建文件夾 (8)\\1657269583419039747"), new File("C:\\Users\\guohu\\Desktop\\新建文件夾 (8)\\1657269583419039748") ); zipFiles(fileList, response,"學(xué)生資料"); /** * 多個文件壓縮成壓縮包并下載工具類 * * @param fileList * @param */ public static void zipFiles(List<File> fileList, HttpServletResponse response, String zipFileName) { try { // 設(shè)置響應(yīng)的頭部信息 response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipFileName+".zip", "utf-8")); // 設(shè)置響應(yīng)內(nèi)容的類型 response.setContentType("application/octet-stream"); // 將壓縮文件寫入輸出流 try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) { for (File file : fileList) { if (file.exists()) { compress(file, zos, file.getName(), true); } } } response.flushBuffer(); response.getOutputStream().close(); } catch (IOException e) { e.printStackTrace(); } } private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean keepDirStructure) throws IOException { byte[] buffer = new byte[4096]; if (sourceFile.isFile()) { try (FileInputStream fis = new FileInputStream(sourceFile)) { ZipEntry zipEntry; if (keepDirStructure) { zipEntry = new ZipEntry(name); } else { zipEntry = new ZipEntry(sourceFile.getName()); } zos.putNextEntry(zipEntry); int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } zos.closeEntry(); } } else if (sourceFile.isDirectory()) { File[] files = sourceFile.listFiles(); if (files != null) { for (File file : files) { if (keepDirStructure) { compress(file, zos, name + File.separator + file.getName(), keepDirStructure); } else { compress(file, zos, file.getName(), keepDirStructure); } } } } }
以上就是SpringBoot將多個文件夾進行壓縮的兩種方法(瀏覽器下載和另存為)的詳細內(nèi)容,更多關(guān)于SpringBoot文件夾壓縮的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot+Vue跨域配置(CORS)問題得解決過程
在使用 Spring Boot 和 Vue 開發(fā)前后端分離的項目時,跨域資源共享(CORS)問題是一個常見的挑戰(zhàn),接下來,我將分享我是如何一步步解決這個問題的,包括中間的一些試錯過程,希望能夠幫助到正在經(jīng)歷類似問題的你2024-08-08MybatisPlus EntityWrapper如何自定義SQL
這篇文章主要介紹了MybatisPlus EntityWrapper如何自定義SQL,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03JavaWeb實現(xiàn)學(xué)生信息管理系統(tǒng)(2)
這篇文章主要介紹了JavaWeb實現(xiàn)學(xué)生信息管理系統(tǒng)的第二篇,實現(xiàn)學(xué)生管理系統(tǒng)的查找和添加功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08Java的MyBatis框架中關(guān)鍵的XML字段映射的配置參數(shù)詳解
將XML文件的schema字段映射到數(shù)據(jù)庫的schema是我們操作數(shù)據(jù)庫的常用手段,這里我們就來整理一些Java的MyBatis框架中關(guān)鍵的XML字段映射的配置參數(shù)詳解,需要的朋友可以參考下2016-06-06