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è)壓縮包(通過瀏覽器下載)
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è)文件壓縮成壓縮包并下載工具類
*
* @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將多個(gè)文件夾進(jìn)行壓縮的兩種方法(瀏覽器下載和另存為)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot文件夾壓縮的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Kafka日志清理實(shí)現(xiàn)詳細(xì)過程講解
這篇文章主要為大家介紹了Kafka日志清理實(shí)現(xiàn)詳細(xì)過程講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
SpringBoot+Vue跨域配置(CORS)問題得解決過程
在使用 Spring Boot 和 Vue 開發(fā)前后端分離的項(xiàng)目時(shí),跨域資源共享(CORS)問題是一個(gè)常見的挑戰(zhàn),接下來,我將分享我是如何一步步解決這個(gè)問題的,包括中間的一些試錯(cuò)過程,希望能夠幫助到正在經(jīng)歷類似問題的你2024-08-08
SpringBoot整合Ldap的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot整合Ldap的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
MybatisPlus EntityWrapper如何自定義SQL
這篇文章主要介紹了MybatisPlus EntityWrapper如何自定義SQL,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
一文詳解Redisson分布式鎖底層實(shí)現(xiàn)原理
這篇文章主要詳細(xì)介紹了Redisson分布式鎖底層實(shí)現(xiàn)原理,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
JavaWeb實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(2)
這篇文章主要介紹了JavaWeb實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)的第二篇,實(shí)現(xiàn)學(xué)生管理系統(tǒng)的查找和添加功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Java的MyBatis框架中關(guān)鍵的XML字段映射的配置參數(shù)詳解
將XML文件的schema字段映射到數(shù)據(jù)庫(kù)的schema是我們操作數(shù)據(jù)庫(kù)的常用手段,這里我們就來整理一些Java的MyBatis框架中關(guān)鍵的XML字段映射的配置參數(shù)詳解,需要的朋友可以參考下2016-06-06

