java create NESTED zip recursively in memory -
this how create simple zip archive 3 files
fileoutputstream fos = new fileoutputstream(new file("out.zip")); bytearrayoutputstream baos = new bytearrayoutputstream(); try (zipoutputstream zos = new zipoutputstream(baos)) { (int = 0; < 3; i++) { string s = "hello world " + i; zipentry entry = new zipentry("text" + + ".txt"); zos.putnextentry(entry); zos.write(s.getbytes()); zos.closeentry(); } } baos.writeto(fos);
how can put zip inside zip recursively in 1 turn on fly? there way put zipoutputstream
or zipentry
inside each other?
edit:
solution mark suggested:
fileoutputstream fos = new fileoutputstream(new file("out.zip")); bytearrayoutputstream resultbytes = new bytearrayoutputstream(); bytearrayoutputstream zipoutstream = new bytearrayoutputstream(); try (zipoutputstream zos2 = new zipoutputstream(zipoutstream)) { string s = "hello world "; zipentry entry = new zipentry("text.txt"); zos2.putnextentry(entry); zos2.write(s.getbytes()); zos2.closeentry(); zos2.close(); } bytearrayoutputstream baos = new bytearrayoutputstream(); try (zipoutputstream zos = new zipoutputstream(baos)) { zipentry entry = new zipentry("text.zip"); zos.putnextentry(entry); zos.write(zipoutstream.tobytearray()); zos.closeentry(); } baos.writeto(resultbytes); resultbytes.writeto(fos);
for "inner" .zip file, use bytearrayoutputstream
instead of fileoutputstream
; zip built data in ram; can use resulting byte array data zipentry
in outer .zip file.
Comments
Post a Comment