I'm wanting to execute a program and as it runs read in it's output and pipe out the output into a zipped file. The output of the program can be quite large so the idea is to not hold too much in memory - just to send it to the zip as I get it.
ZipOutputStream targetStream = new ZipOutputStream(fileToSaveTo);
ZipEntry entry = new ZipEntry(nameOfFileInZipFile);
targetStream.putNextEntry(entry);
byte[] dataBlock = new byte[1024];
int count = inputStream.read(dataBlock, 0, 1024);
while (count != -1) {
targetStream.write(dataBlock, 0, count);
count = inputStream.read(dataBlock, 0, 1024);
}
In otherwords:
ZipOutputStream
, giving it the file you want to write to.You create a ZipEntry
, which constitutes a file within that zip file.
i.e. When you open myFile.zip, and there are 3 files in there, each file is a ZipEntry
.
You put that ZipEntry
into your ZipOutputStream
inputStream
into your byte buffer, and remember the count.count
is not -1
, write that byte byffer to your zipStream
.Close out your streams when you are done. Wrap it in a method as you see fit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With