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.
You probably tried reading from a
FileInputStream
like this:This won’t work since a zip archive can contain multiple files and you need to specify which file to read.
You could use java.util.zip.ZipFile and a library such as IOUtils from Apache Commons IO or ByteStreams from Guava that assist you in copying the stream.
Example:
You're missing call
ZipEntry entry = (ZipEntry) zipStream.getNextEntry();
to position the first byte decompressed of the first entry.
I would call getNextEntry() on the ZipInputStream until it is at the entry you want (use ZipEntry.getName() etc.). Calling getNextEntry() will advance the "cursor" to the beginning of the entry that it returns. Then, use ZipEntry.getSize() to determine how many bytes you should read using zipInputStream.read().
Please try code bellow