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();
}
now baos
contains your zip file as a stream
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…