我正在使用 aZipOutputStream
压缩一堆文件,这些文件混合了已经压缩的格式以及许多大型高度可压缩格式,如纯文本。
大多数已经压缩的格式都是大文件,花费 cpu 和内存重新压缩它们是没有意义的,因为它们永远不会变小,有时在极少数情况下会稍微变大。
我在检测到预压缩文件时尝试使用.setMethod(ZipEntry.STORED)
,但它抱怨我需要size, compressedSize and crc
为这些文件提供。
我可以使用以下方法使其工作,但这需要我读取文件两次。一次计算CRC32
然后再次实际将文件复制到ZipOutputStream
.
// code that determines the value of method omitted for brevity
if (STORED == method)
{
fze.setMethod(STORED);
fze.setCompressedSize(fe.attributes.size());
final HashingInputStream his = new HashingInputStream(Hashing.crc32(), fis);
ByteStreams.copy(his,ByteStreams.nullOutputStream());
fze.setCrc(his.hash().padToLong());
}
else
{
fze.setMethod(DEFLATED);
}
zos.putNextEntry(fze);
ByteStreams.copy(new FileInputStream(fe.path.toFile()), zos);
zos.closeEntry();