I am trying to read a single file from a java.util.zip.ZipInputStream
, and copy it into a java.io.ByteArrayOutputStream
(so that I can then create a java.io.ByteArrayInputStream
and hand that to a 3rd party library that will end up closing the stream, and I don't want my ZipInputStream
getting closed).
I'm probably missing something basic here, but I never enter the while loop here:
ByteArrayOutputStream streamBuilder = new ByteArrayOutputStream();
int bytesRead;
byte[] tempBuffer = new byte[8192*2];
try {
while ((bytesRead = zipStream.read(tempBuffer)) != -1) {
streamBuilder.write(tempBuffer, 0, bytesRead);
}
} catch (IOException e) {
// ...
}
What am I missing that will allow me to copy the stream?
Edit:
I should have mentioned earlier that this ZipInputStream
is not coming from a file, so I don't think I can use a ZipFile
. It is coming from a file uploaded through a servlet.
Also, I have already called getNextEntry()
on the ZipInputStream
before getting to this snippet of code. If I don't try copying the file into another InputStream
(via the OutputStream
mentioned above), and just pass the ZipInputStream
to my 3rd party library, the library closes the stream, and I can't do anything more, like dealing with the remaining files in the stream.