如何创建多部分zip文件并将其读回?

我如何正确地将字节压缩到a,然后使用?我有以下方法:ByteArrayOutputStreamByteArrayInputStream

private byte[] getZippedBytes(final String fileName, final byte[] input) throws Exception {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ZipOutputStream zipOut = new ZipOutputStream(bos);
    ZipEntry entry = new ZipEntry(fileName);
    entry.setSize(input.length);
    zipOut.putNextEntry(entry);
    zipOut.write(input, 0, input.length);
    zipOut.closeEntry();
    zipOut.close();

    //Turn right around and unzip what we just zipped
    ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(bos.toByteArray()));

    while((entry = zipIn.getNextEntry()) != null) {
        assert entry.getSize() >= 0;
    }

    return bos.toByteArray();
}

当我执行此代码时,底部的断言失败,因为是 。我不明白为什么提取的实体与压缩的实体不匹配。entry.size-1


答案 1

为什么大小为 -1?

ZipInputStream 中调用 getNextEntry 只需将读取光标放在要读取的条目的开头即可。

大小(以及其他元数据)存储在实际数据的末尾,因此当游标位于起点时,大小不易获得。

只有在您读取整个条目数据或仅转到下一个条目后,这些信息才可用。

例如,转到下一个条目:

// position at the start of the first entry
entry = zipIn.getNextEntry();
ZipEntry firstEntry = entry;    
// size is not yet available
System.out.println("before " + firstEntry.getSize()); // prints -1

// position at the start of the second entry
entry = zipIn.getNextEntry();
// size is now available
System.out.println("after " + firstEntry.getSize()); // prints the size

或读取整个输入数据:

// position at the start of the first entry
entry = zipIn.getNextEntry();
// size is not yet available
System.out.println("before " + entry.getSize()); // prints -1

// read the whole entry data
while(zipIn.read() != -1);

// size is now available
System.out.println("after " + entry.getSize()); // prints the size

您的误解很常见,并且有许多关于此问题的错误报告(以“不是问题”的形式关闭),例如JDK-4079029JDK-4113731JDK-6491622

正如错误报告中提到的,您可以使用ZipFile而不是ZipInputStream,这将允许在访问条目数据之前访问大小信息;但是要创建ZipFile,您需要一个File(请参阅构造函数)而不是字节数组。

例如:

File file = new File( "test.zip" );
ZipFile zipFile = new ZipFile(file);

Enumeration enumeration = zipFile.entries();
while (enumeration.hasMoreElements()) {
    ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
    System.out.println(zipEntry.getSize()); // prints the size
}

如何从输入流中获取数据?

如果要检查解压缩的数据是否等于原始输入数据,可以从输入流中读取,如下所示:

byte[] output = new byte[input.length];
entry = zipIn.getNextEntry();
zipIn.read(output);

System.out.println("Are they equal? " + Arrays.equals(input, output));

// and if we want the size
zipIn.getNextEntry(); // or zipIn.read();
System.out.println("and the size is " + entry.getSize());

现在应该具有与 相同的内容。outputinput


答案 2

如何将其拉回拉链并解压缩回去?byte[]

我经常使用以下方法来放气/充气(zip/解压缩)小(即当它适合内存时)。它基于Defllater javadoc中给出的示例,并使用Defllater类来压缩数据,并使用 Inflater类将其解压缩回来:byte[]

public static byte[] compress(byte[] source, int level) {
    Deflater compresser = new Deflater(level);
    compresser.setInput(source);
    compresser.finish();
    byte[] buf = new byte[1024];
    ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
    int n;
    while ((n = compresser.deflate(buf)) > 0)
        bos.write(buf, 0, n);
    compresser.end();
    return bos.toByteArray(); // You could as well return "bos" directly
}

public static byte[] uncompress(byte[] source) {
    Inflater decompresser = new Inflater();
    decompresser.setInput(source);
    byte[] buf = new byte[1024];
    ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
    try {
        int n;
        while ((n = decompresser.inflate(buf)) > 0)
            bos.write(buf, 0, n);
        return bos.toByteArray();
    } catch (DataFormatException e) {
        return null;
    } finally {
        decompresser.end();
    }
}

不需要 ,但是如果您真的想要,可以使用 InflaterInputStream 包装它(但直接使用更容易)。ByteArrayInputStreamInflater


推荐