java ZipFile如何將多級(jí)目錄壓縮
java ZipFile將多級(jí)目錄壓縮
在 Java 中,可以使用 java.util.zip 包中的 ZipOutputStream 和 ZipEntry 類來壓縮多級(jí)目錄。
一個(gè)例子
展示如何遞歸地將一個(gè)目錄及其所有子目錄和文件壓縮到一個(gè) ZIP 文件中:
import java.io.*;
import java.nio.file.*;
import java.util.zip.*;
public class ZipDirectory {
public static void main(String[] args) throws IOException {
String sourceDirPath = "path/to/source/directory";
String zipFilePath = "path/to/output.zip";
try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFilePath))) {
Path sourceDir = Paths.get(sourceDirPath);
Files.walk(sourceDir)
.filter(path -> !Files.isDirectory(path))
.forEach(path -> {
ZipEntry zipEntry = new ZipEntry(sourceDir.relativize(path).toString());
try {
zipOut.putNextEntry(zipEntry);
Files.copy(path, zipOut);
zipOut.closeEntry();
} catch (IOException e) {
System.err.println("Failed to add file to ZIP: " + path);
e.printStackTrace();
}
});
}
}
}這個(gè)程序?qū)⒈闅v指定目錄(包括子目錄)的所有文件,并將它們添加到 ZIP 文件中。
步驟解析
- 使用 Files.walk 方法遞歸遍歷源目錄。
- 對(duì)于每個(gè)非目錄文件,創(chuàng)建一個(gè)相對(duì)路徑的 ZipEntry。
- 將文件內(nèi)容復(fù)制到 ZipOutputStream。
請(qǐng)根據(jù)實(shí)際情況替換 sourceDirPath 和 zipFilePath。此外,請(qǐng)確保適當(dāng)處理異常并關(guān)閉資源以避免資源泄漏。
Java多級(jí)目錄導(dǎo)出文件壓縮包
//創(chuàng)建臨時(shí)文件
File zipFile = File.createTempFile("test", ".zip");
Path temp = Files.createTempDirectory(null);
String srcImgPath = temp.toString();
for( 循環(huán) ){
String outImgPath = srcImgPath + "/aa/bb/cc";
File outImgFile = new File(outImgPath);
//如果文件夾不存在則創(chuàng)建
if (!outImgFile.exists() && !outImgFile.isDirectory()) {
outImgFile.mkdirs();
}
FileOutputStream outImgStream = new FileOutputStream(outImgPath + "/" + name);
ImageIO.write(bufImg, imgSuffix, outImgStream);
outImgStream.flush();
outImgStream.close();
}
ZipUtil.zip(srcImgPath, zipFile.getAbsolutePath(), true);
String header = request.getHeader("User-Agent").toUpperCase();
String fileName = "附件信息" + System.currentTimeMillis() + ".zip";
if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {
fileName = URLEncoder.encode(fileName, "utf-8");
//IE下載文件名空格變+號(hào)問題
fileName = fileName.replace("+", "%20");
} else {
fileName = new String(fileName.getBytes(), "ISO8859-1");
}
response.reset();
response.setContentType("text/plain");
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader("Location", fileName);
response.setHeader("Cache-Control", "max-age=0");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
FileInputStream fis = new FileInputStream(zipFile);
BufferedInputStream buff = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
byte[] car = new byte[1024];
int l = 0;
while (l < zipFile.length()) {
int j = buff.read(car, 0, 1024);
l += j;
out.write(car, 0, j);
}
// 關(guān)閉流
fis.close();
buff.close();
out.close();
// 刪除臨時(shí)文件
zipFile.delete();
FileUtil.deleteDir(srcImgPath);總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于注解的springboot+mybatis的多數(shù)據(jù)源組件的實(shí)現(xiàn)代碼
這篇文章主要介紹了基于注解的springboot+mybatis的多數(shù)據(jù)源組件的實(shí)現(xiàn),會(huì)使用到多個(gè)數(shù)據(jù)源,文中通過代碼講解的非常詳細(xì),需要的朋友可以參考下2021-04-04
關(guān)于Java中阻塞隊(duì)列BlockingQueue的詳解
這篇文章主要介紹了關(guān)于Java中阻塞隊(duì)列BlockingQueue的詳解,BlockingQueue是為了解決多線程中數(shù)據(jù)高效安全傳輸而提出的,從阻塞這個(gè)詞可以看出,在某些情況下對(duì)阻塞隊(duì)列的訪問可能會(huì)造成阻塞,需要的朋友可以參考下2023-05-05
Spring Boot Admin的使用詳解(Actuator監(jiān)控接口)
這篇文章主要介紹了Spring Boot Admin的使用詳解(Actuator監(jiān)控接口),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Java中調(diào)用DLL動(dòng)態(tài)庫(kù)的操作方法
在Java編程中,有時(shí)我們需要調(diào)用本地代碼庫(kù),特別是Windows平臺(tái)上的DLL(動(dòng)態(tài)鏈接庫(kù)),本文中,我們將詳細(xì)討論如何在Java中加載和調(diào)用DLL動(dòng)態(tài)庫(kù),并通過具體示例來展示這個(gè)過程,感興趣的朋友跟隨小編一起看看吧2024-03-03
Java判斷一個(gè)字符串是不是一個(gè)數(shù)字的解決思路
這篇文章主要給大家介紹了關(guān)于Java判斷一個(gè)字符串是不是一個(gè)數(shù)字的解決思路,判斷一個(gè)字符串是否為數(shù)字是Java開發(fā)中很常見的業(yè)務(wù)需求,實(shí)現(xiàn)這個(gè)判斷有很多種方式,需要的朋友可以參考下2023-08-08

