亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java ZipFile如何將多級(jí)目錄壓縮

 更新時(shí)間:2024年08月26日 09:18:29   作者:愛穿襯衫的張某某  
這篇文章主要介紹了java ZipFile如何將多級(jí)目錄壓縮問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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)文章

最新評(píng)論