亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Java 中壓縮包含所有子文件夾的完整目錄?

如何在 Java 中壓縮包含所有子文件夾的完整目錄?

臨摹微笑 2023-08-09 15:02:49
嘿嘿!在你去報告這個重復之前:我研究了幾個小時,是的,有很多關于這方面的網站、問題和視頻,但這些都不能幫助我。我已經使用了不同的技術來創建 zip 存檔,效果很好。我還可以毫無問題地從子目錄中獲取所有文件。但問題是我只得到列出的所有文件,沒有它們的目錄。如果我有/something/somethingelse/text1.txt 和 /something/somethingother/lol.txt我希望它完全像那樣顯示在 zip 文件夾中。在 zip 文件夾中應該有 2 個文件夾 someelse 和 someother,其中包含它們的文件。對于我找到的所有版本,它將所有文件和其他子文件夾中的所有文件直接放入 zip 中,因此當我單擊它時,.zip它只顯示text1.txt和lol.txt,沒有任何文件夾。有沒有一種方法可以.zip像所有眾所周知的壓縮文件程序一樣處理 a ?我只想壓縮目錄并保留以前的所有內容,只是打包到 zip 存檔中。我用我的java水平嘗試了一切,也嘗試了一些在線版本,沒有任何結果能達到我想要的結果。
查看完整描述

2 回答

?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

干得好:


private static void zipFolder(Path sourceFolderPath, Path zipPath) throws Exception {

   ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipPath.toFile()));

   Files.walkFileTree(sourceFolderPath, new SimpleFileVisitor<Path>() {

       public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

           zos.putNextEntry(new ZipEntry(sourceFolderPath.relativize(file).toString()));

           Files.copy(file, zos);

           zos.closeEntry();

           return FileVisitResult.CONTINUE;

        }

    });

    zos.close();

 }

./

https://img1.sycdn.imooc.com//64d33a450001b84c10740112.jpg

./somethigelse/

https://img1.sycdn.imooc.com//64d33a570001862610780080.jpg

./其他東西/

https://img1.sycdn.imooc.com//64d33a680001fe5110790077.jpg


查看完整回答
反對 回復 2023-08-09
?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

該解決方案保留了創建的 zip 文件內的文件夾結構的層次結構 -


import java.io.*;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;


public class ZipDirectories {


? ? public static void main(String[] args) throws IOException {

? ? ? ? zipDirectory(args[0], args[1]);

? ? }


? ? private static void zipDirectory(String zipFileName, String rootDirectoryPath) throws IOException {

? ? ? ? File directoryObject = new File(rootDirectoryPath);

? ? ? ? if (!zipFileName.endsWith(".zip")) {

? ? ? ? ? ? zipFileName = zipFileName + ".zip";

? ? ? ? }

? ? ? ? ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));

? ? ? ? System.out.println("Creating : " + zipFileName);

? ? ? ? addDirectory(directoryObject, out);

? ? ? ? out.close();

? ? }


? ? private static void addDirectory(File directoryObject, ZipOutputStream out) throws IOException {

? ? ? ? File[] files = directoryObject.listFiles();

? ? ? ? byte[] tmpBuf = new byte[1024];


? ? ? ? for (File file : files) {

? ? ? ? ? ? if (file.isDirectory()) {

? ? ? ? ? ? ? ? addDirectory(file, out);

? ? ? ? ? ? ? ? continue;

? ? ? ? ? ? }

? ? ? ? ? ? FileInputStream in = new FileInputStream(file.getAbsolutePath());

? ? ? ? ? ? System.out.println(" Adding: " + file.getAbsolutePath());

? ? ? ? ? ? out.putNextEntry(new ZipEntry(file.getAbsolutePath()));

? ? ? ? ? ? int len;

? ? ? ? ? ? while ((len = in.read(tmpBuf)) > 0) {

? ? ? ? ? ? ? ? out.write(tmpBuf, 0, len);

? ? ? ? ? ? }

? ? ? ? ? ? out.closeEntry();

? ? ? ? ? ? in.close();

? ? ? ? }

? ? }

}


查看完整回答
反對 回復 2023-08-09
  • 2 回答
  • 0 關注
  • 228 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號