如何创建多部分zip文件并将其读回?
2022-09-03 14:46:04
我如何正确地将字节压缩到a,然后使用?我有以下方法:ByteArrayOutputStream
ByteArrayInputStream
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