在内存中创建一个zip文件

 

Use ByteArrayOutputStream with ZipOutputStream to accomplish the task.

you can use ZipEntry to specify the files to be included into the zip file.

Here is an example of using the above classes,

String s = "hello world";

ByteArrayOutputStream baos = new ByteArrayOutputStream();
try(ZipOutputStream zos = new ZipOutputStream(baos)) {

  /* File is not on the disk, test.txt indicates
     only the file name to be put into the zip */
  ZipEntry entry = new ZipEntry("test.txt"); 

  zos.putNextEntry(entry);
  zos.write(s.getBytes());
  zos.closeEntry();

  /* use more Entries to add more files
     and use closeEntry() to close each file entry */

  } catch(IOException ioe) {
    ioe.printStackTrace();
  }

  

links:

https://stackoverflow.com/questions/23612864/create-a-zip-file-in-memory

https://stackoverflow.com/questions/12030703/uncompressing-a-zip-file-in-memory-in-java

https://stackoverflow.com/questions/25974354/creating-zip-file-in-memory-out-of-byte-zip-file-is-allways-corrupted

https://stackoverflow.com/questions/45354097/creating-zip-file-in-memory

https://stackoverflow.com/questions/18406148/how-can-i-generate-zip-file-without-saving-to-the-disk-with-java

posted @ 2020-09-01 14:15  ls1519🎈  阅读(388)  评论(0编辑  收藏  举报