SpringBoot將多個(gè)文件夾進(jìn)行壓縮的兩種方法(瀏覽器下載和另存為)
1、將多個(gè)文件夾壓縮成一個(gè)壓縮包(壓縮到固定目錄)
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、將多個(gè)文件夾壓縮成一個(gè)壓縮包(通過(guò)瀏覽器下載)
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é)生資料");
/**
* 多個(gè)文件壓縮成壓縮包并下載工具類(lèi)
*
* @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)容的類(lèi)型
response.setContentType("application/octet-stream");
// 將壓縮文件寫(xiě)入輸出流
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將多個(gè)文件夾進(jìn)行壓縮的兩種方法(瀏覽器下載和另存為)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot文件夾壓縮的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
MyBatis 參數(shù)綁定的具體實(shí)現(xiàn)
本文主要介紹了MyBatis 參數(shù)綁定的具體實(shí)現(xiàn),包括默認(rèn)參數(shù)名、@Param注解和POJO/DTO對(duì)象三種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2026-01-01
mybatis利用association或collection傳遞多參數(shù)子查詢
今天小編就為大家分享一篇關(guān)于mybatis利用association或collection傳遞多參數(shù)子查詢,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
Java中無(wú)參構(gòu)造方法的一些核心作用總結(jié)
構(gòu)造方法是一個(gè)特殊的方法,用于創(chuàng)建類(lèi)的實(shí)例,構(gòu)造方法的名稱必須與類(lèi)名相同,并且沒(méi)有返回類(lèi)型,這篇文章主要介紹了Java中無(wú)參構(gòu)造方法的一些核心作用,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2026-06-06
SpringBoot整合Mybatis-plus實(shí)現(xiàn)多級(jí)評(píng)論功能
本文介紹了如何使用SpringBoot整合Mybatis-plus實(shí)現(xiàn)多級(jí)評(píng)論功能,同時(shí)提供了數(shù)據(jù)庫(kù)的設(shè)計(jì)和詳細(xì)的后端代碼,前端界面使用的Vue2,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-05-05
SpringBoot?整合mybatis+mybatis-plus的詳細(xì)步驟
這篇文章主要介紹了SpringBoot?整合mybatis+mybatis-plus的步驟,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
在SSM中配置了事務(wù)控制但沒(méi)生效的問(wèn)題
這篇文章主要介紹了在SSM中配置了事務(wù)控制但沒(méi)生效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
springboot基于注解實(shí)現(xiàn)去重表消息防止重復(fù)消費(fèi)
本文主要介紹了springboot基于注解實(shí)現(xiàn)去重表消息防止重復(fù)消費(fèi),通過(guò)記錄消息ID、使用分布式鎖和設(shè)置過(guò)期時(shí)間,可以確保消息只會(huì)被處理一次,具有一定的參考價(jià)值,感興趣的可以了解一下2025-05-05
spring boot 配置Filter過(guò)濾器的方法
本篇文章主要介紹了spring boot 配置Filter過(guò)濾器的方法,實(shí)例分析了spring boot 配置Filter過(guò)濾器的技巧,有興趣的可以了解一下。2017-03-03

