使用 JAVA 将包含文件的文件夹添加到 zip 文件中

发布于 2024-08-20 03:18:50 字数 418 浏览 6 评论 0原文

我正在使用 java 的 java.util.zip api 将文件和文件夹添加到 zip 文件中,但是当我将多个文件添加到同一文件夹中时,它删除了旧内容。有什么方法可以将文件添加到 zip 文件中而不修改文件夹中的现有内容? 请帮忙,这很重要!

这是我的示例代码:

ZipOutputStream zip = null;
FileOutputStream fileWriter = null;
fileWriter = new FileOutputStream(destZipFile);
zip = new ZipOutputStream(fileWriter);
zip.putNextEntry(new ZipEntry(destFilePath));
zip.write(content);
zip.flush();
zip.close();

I am using java's java.util.zip api to add files and folders to a zip file, but when i add multiple files into the same folder, it deleted the old contents. Is there any way i can add files into the zip file without modifying existing contents in the folder?.
Kindly help, its important!

This is my sample code:

ZipOutputStream zip = null;
FileOutputStream fileWriter = null;
fileWriter = new FileOutputStream(destZipFile);
zip = new ZipOutputStream(fileWriter);
zip.putNextEntry(new ZipEntry(destFilePath));
zip.write(content);
zip.flush();
zip.close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遮了一弯 2024-08-27 03:18:50

如果您想将新文件添加到现有 zip 文件中,则必须首先解压缩所有内容,然后添加所有文件并再次压缩。

有关示例,请参阅此链接

If you want to add a new file to an existing zip file, you must first unzip everything, then add all the files and zip again.

See this link for samples.

喵星人汪星人 2024-08-27 03:18:50

我发现过一次...它会创建一个临时文件,并在添加额外文件之前将现有 zip 中的所有文件添加到“新”zip 中。如果两个文件具有相同的名称,它只会添加“最新”的文件。

public static void addFilesToExistingZip(File zipFile,
         File[] files) throws IOException {
            // get a temp file
    File tempFile = File.createTempFile(zipFile.getName(), null);
            // delete it, otherwise you cannot rename your existing zip to it.
    tempFile.delete();

    boolean renameOk=zipFile.renameTo(tempFile);
    if (!renameOk)
    {
        throw new RuntimeException("could not rename the file "+zipFile.getAbsolutePath()+" to "+tempFile.getAbsolutePath());
    }
    byte[] buf = new byte[1024];

    ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

    ZipEntry entry = zin.getNextEntry();
    while (entry != null) {
        String name = entry.getName();
        boolean notInFiles = true;
        for (File f : files) {
            if (f.getName().equals(name)) {
                notInFiles = false;
                break;
            }
        }
        if (notInFiles) {
            // Add ZIP entry to output stream.
            out.putNextEntry(new ZipEntry(name));
            // Transfer bytes from the ZIP file to the output file
            int len;
            while ((len = zin.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
        entry = zin.getNextEntry();
    }
    // Close the streams        
    zin.close();
    // Compress the files
    for (int i = 0; i < files.length; i++) {
        InputStream in = new FileInputStream(files[i]);
        // Add ZIP entry to output stream.
        out.putNextEntry(new ZipEntry(files[i].getName()));
        // Transfer bytes from the file to the ZIP file
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        // Complete the entry
        out.closeEntry();
        in.close();
    }
    // Complete the ZIP file
    out.close();
    tempFile.delete();
}

编辑:
我认为这已经有两年多了,所以可能有些东西已经不再是最新的了。

I found this once... It creates a temporary file and adds all files from the existing zip to the 'new' zip before adding the extra files. If two files have the same name, it only adds the 'newest' one.

public static void addFilesToExistingZip(File zipFile,
         File[] files) throws IOException {
            // get a temp file
    File tempFile = File.createTempFile(zipFile.getName(), null);
            // delete it, otherwise you cannot rename your existing zip to it.
    tempFile.delete();

    boolean renameOk=zipFile.renameTo(tempFile);
    if (!renameOk)
    {
        throw new RuntimeException("could not rename the file "+zipFile.getAbsolutePath()+" to "+tempFile.getAbsolutePath());
    }
    byte[] buf = new byte[1024];

    ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

    ZipEntry entry = zin.getNextEntry();
    while (entry != null) {
        String name = entry.getName();
        boolean notInFiles = true;
        for (File f : files) {
            if (f.getName().equals(name)) {
                notInFiles = false;
                break;
            }
        }
        if (notInFiles) {
            // Add ZIP entry to output stream.
            out.putNextEntry(new ZipEntry(name));
            // Transfer bytes from the ZIP file to the output file
            int len;
            while ((len = zin.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
        entry = zin.getNextEntry();
    }
    // Close the streams        
    zin.close();
    // Compress the files
    for (int i = 0; i < files.length; i++) {
        InputStream in = new FileInputStream(files[i]);
        // Add ZIP entry to output stream.
        out.putNextEntry(new ZipEntry(files[i].getName()));
        // Transfer bytes from the file to the ZIP file
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        // Complete the entry
        out.closeEntry();
        in.close();
    }
    // Complete the ZIP file
    out.close();
    tempFile.delete();
}

EDIT:
I think this is more than 2 years old, so possibly some things are not really up to date anymore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文