【问题标题】:How to zip the content of a directory in Java如何在Java中压缩目录的内容
【发布时间】:2020-07-20 05:03:45
【问题描述】:

我在下面使用java,但是当它压缩时,它会创建一个目录并将该目录中的所有内容压缩。 例如。如果我有一个名为“目录”的文件夹,并且我想将内容压缩到一个压缩文件中,则在压缩文件中它会创建一个文件夹 testZip 并在其中包含文件。 我需要压缩文件中的所有文件,而不是父目录中的所有文件。 请帮忙。或建议是否有其他方法。

package folderZip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipFolder {
    public ZipFolder() {  
    }  


    public static void main(String[] args) throws Exception {
        ZipFolder obj = new ZipFolder();
        obj.zipFolder("C:\\Drive\\temp\\testZip","C:\\Drive\\temp\\FolderZiper.zip");       
    }


     public void zipFolder(String srcFolder,
                                 String destZipFile) throws Exception {
        ZipOutputStream zip = null;
        FileOutputStream fileWriter = null;

        fileWriter = new FileOutputStream(destZipFile);
        zip = new ZipOutputStream(fileWriter);

        addFolderToZip("", srcFolder, zip);

        zip.flush();
        zip.close();

    }

    private void addFileToZip(String path, String srcFile,
                                     ZipOutputStream zip) throws Exception {

        File folder = new File(srcFile);
        if (folder.isDirectory()) {
            addFolderToZip(path, srcFile, zip);
        } else {
            byte[] buf = new byte[1024];
            int len;
            FileInputStream in = new FileInputStream(srcFile);
            zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
            while ((len = in.read(buf)) > 0) {
                zip.write(buf, 0, len);
            }
        }
    }

    private void addFolderToZip(String path, String srcFolder,
                                       ZipOutputStream zip) throws Exception {
        File folder = new File(srcFolder);

        for (String fileName : folder.list()) {
            if (path.equals("")) {
                addFileToZip(folder.getName(), srcFolder + "/" + fileName,
                             zip);
            } else {
                addFileToZip(path + "/" + folder.getName(),
                             srcFolder + "/" + fileName, zip);
            }
        }
    }
}

【问题讨论】:

  • 基本上,如果我理解您的问题,您需要从所需的 zip 条目名称中去掉“源”文件夹名称。像this examplethis examplethis example
  • 试试这个:当前文件夹是“testing”并创建一个名为“testfolder”的子文件夹。 testfolder 有多个文件,其中一个是“user-guide.pdf”。该程序应该能够创建一个只有一个条目 user-guide.pdf 的 ZIP 文件夹 - 输出 zip 文件“MyZip”将只有一个文件 user-guide.pdf。输出 zip 文件在您选择的文件夹中创建 - testing 或 testfolder。如果您能够做到这一点,您可以将相同的逻辑应用于您正在尝试的应用程序。此外,还可以使用Files.walkFileTreeFileVisitor API 来遍历文件树并压缩内容。

标签: java zip


【解决方案1】:

所以,如果我理解正确,您想要的是,而不是出现在 Zip 文件中的目标文件夹,其中的所有文件都应该从 Zip 文件中的“/”开始。

例如,如果你有

FolderToZip/
    TestFile1.txt
    TestFile2.txt
    TestFile3.txt
    SomeSubFolder/
        TestFile4.txt
        TestFile5.txt
        TestFile6.txt

Zip 文件的内容应包含

TestFile1.txt
TestFile2.txt
TestFile3.txt
SomeSubFolder/
    TestFile4.txt
    TestFile5.txt
    TestFile6.txt

为此,您需要保留对“开始”文件夹的引用并将其路径从您添加的文件中剥离,以创建ZipEntry

为简单起见,我将您的代码更改为支持File 而不是String,这只会让整个事情变得不那么混乱。

但魔法出现在这里......

FileInputStream in = new FileInputStream(srcFile);
String name = srcFile.getPath();
name = name.replace(rootPath.getPath(), "");
System.out.println("Zip " + srcFile + "\n to " + name);
zip.putNextEntry(new ZipEntry(name));

rootPath 是初始文件夹(在您的示例中为C:\\Drive\\temp\\testZip"),srcFile 是要添加的文件。

可运行示例...

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


public class ZipFolder {

    public ZipFolder() {
    }

    public static void main(String[] args) throws Exception {
        ZipFolder obj = new ZipFolder();
        obj.zipFolder(new File("/Users/shanewhitehead/exports"),
                new File("FolderZiper.zip"));
    }

    public void zipFolder(File srcFolder, File destZipFile) throws Exception {
        try (FileOutputStream fileWriter = new FileOutputStream(destZipFile);
                ZipOutputStream zip = new ZipOutputStream(fileWriter)) {

            addFolderToZip(srcFolder, srcFolder, zip);
        }
    }

    private void addFileToZip(File rootPath, File srcFile, ZipOutputStream zip) throws Exception {

        if (srcFile.isDirectory()) {
            addFolderToZip(rootPath, srcFile, zip);
        } else {
            byte[] buf = new byte[1024];
            int len;
            try (FileInputStream in = new FileInputStream(srcFile)) {
                String name = srcFile.getPath();
                name = name.replace(rootPath.getPath(), "");
                System.out.println("Zip " + srcFile + "\n to " + name);
                zip.putNextEntry(new ZipEntry(name));
                while ((len = in.read(buf)) > 0) {
                    zip.write(buf, 0, len);
                }
            }
        }
    }

    private void addFolderToZip(File rootPath, File srcFolder, ZipOutputStream zip) throws Exception {
        for (File fileName : srcFolder.listFiles()) {
            addFileToZip(rootPath, fileName, zip);
        }
    }
}

我还清理了你的资源管理,使用try-with-resources

【讨论】:

  • 非常感谢,你拯救了我的一天 :) 我只是做了一个小的修改,在 zip 文件中,Windows 中添加了一个“\”,所以我添加了:name = name.replace(rootPath.获取路径(),“”); name = name.substring(1);删除第一个'\'。不确定在 linux 中是否已经正常工作,将测试
【解决方案2】:

如何使用这个库Zeroturnaround Zip library
然后你将你的文件夹压缩一行:

ZipUtil.pack(new File("D:\\sourceFolder\\"), new File("D:\\generatedZipFile.zip"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 2022-12-20
    • 1970-01-01
    • 2012-06-24
    • 2011-04-17
    相关资源
    最近更新 更多