简体   繁体   English

Java:zip中的zip变得不可读

[英]Java: A zip in a zip becomes unreadable

For a project I need to create a zip that contains two zip files. 对于项目,我需要创建一个包含两个zip文件的zip文件。 I created some methods to do that, however, the resulting zip is not correct. 我创建了一些方法来执行此操作,但是,生成的zip文件不正确。

Consider the following test class: 考虑以下测试类:

package nl.test;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.junit.Test;

public class ZipTest {

    @Test
    public void test() throws IOException {
        Path p = Paths.get("input.txt");
        try (BufferedWriter writer = Files.newBufferedWriter(p)) {
            writer.write("text to compress");
        }
        Path p1 = createZipFile("p.zip", p);
        createZipFile("p1.zip", p1);
    }

    private Path createZipFile(String zipName, Path p) {
        try {
            OutputStream fos = Files.newOutputStream(Paths.get(zipName));
            ZipOutputStream zos = new ZipOutputStream(fos);
            OutputStream bos = new BufferedOutputStream(zos);
            try (Writer writer = new OutputStreamWriter(bos)) {
                zos.putNextEntry(new ZipEntry(p.toString()));
                writer.write(new String(Files.readAllBytes(p)));
                writer.flush();
            }
            return Paths.get(zipName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

When executing this, the file p1.zip does indeed contain a p.zip, however, that p.zip is unreadable. 执行此操作时,文件p1.zip确实包含一个p.zip,但是该p.zip不可读。 Is there a way to fix this? 有没有办法解决这个问题? Or is there another way to place a zip in a zip? 还是有其他方法可以将拉链放在拉链中?

Here's a simpler way: 这是一种更简单的方法:

  1. right click on the file that you want to add in "p.zip" 右键单击要添加到“ p.zip”的文件
  2. click add to archive and edit the settings if you want to 单击添加以存档并编辑设置(如果要)
  3. right click anywhere in the folder where p.zip exists and hover your mouse over new and select "Compressed Zipped Folder" 右键单击存在p.zip的文件夹中的任何位置,然后将鼠标悬停在新建的位置,然后选择“压缩的压缩文件夹”
  4. after it is created just drag the p.zip into the zipped folder just created now. 创建后,只需将p.zip拖到刚才创建的压缩文件夹中即可。
  5. voilà! 瞧!

Until I find out why this is not working, it is solved using zip4j: 直到我弄清为什么它不起作用,才使用zip4j解决:

package nl.test;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.Test;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

public class ZipTest2 {

    @Test
    public void test() throws IOException {
        Path p = Paths.get("input.txt");
        try (BufferedWriter writer = Files.newBufferedWriter(p)) {
            writer.write("text to compress");
        }
        Path p1 = createZipFile("p1.zip", p);
        createZipFile("p2.zip", p1);
    }

    private Path createZipFile(String compressedFile, Path inputPath) {
        try {
            ZipFile zipFile = new ZipFile(compressedFile);
            ZipParameters parameters = new ZipParameters();

            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_FASTEST);
            zipFile.addFile(inputPath.toFile(), parameters);

            return Paths.get(compressedFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

This works when adding a resulting zip to another zip. 将生成的zip添加到另一个zip时可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM