java 内存里压缩和解压缩zip 文件
java 内存里压缩和解压缩zip 文件
java uncompress/compress a zip file in memory
在文件系统之上的压缩和解压缩没太多说的,网络上有大量的资料
目前基于nifi做功能性的processor开发,涉及对zip文件的解压和压缩
通常解压和压缩功能依赖文件系统
为提高实时性,减少cpu压力,同时避免对磁盘的损耗,需要在内存里实现对zip文件的解压和压缩
个人从nifi源码里找到了在内存内解压缩的示例
https://github.com/apache/nifi/blob/ea9b0db2f620526c8dd0db595cf8b44c3ef835be/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/MergeContent.java
并做了示例回答
https://stackoverflow.com/questions/23612864/create-a-zip-file-in-memory
public byte[] compressZip(ByteArrayOutputStream baos,String entryName) throws IOException {
try (final ByteArrayOutputStream zipBaos = new ByteArrayOutputStream();
final java.util.zip.ZipOutputStream out = new ZipOutputStream(zipBaos)) {
final ZipEntry zipEntry = new ZipEntry(entryName);
zipEntry.setSize(baos.size());
out.putNextEntry(zipEntry);
IOUtils.copy(new ByteArrayInputStream(baos.toByteArray()), out);
out.closeEntry();
out.finish();
out.flush();
return zipBaos.toByteArray();
}
}
相关资料
http://commons.apache.org/proper/commons-compress/examples.html