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