I'm using the Apache library commons-io in verion 2.6 to parse a zip stream and get its bytes. But when I call IOUtils.toByteArray(zipStreamObject)
I get a java.io.EOFException: Unexpected end of ZLIB input stream.
Here's my code:
public static void descompactarArquivoZip(Path arquivoZip) throws IOException {
InputStream is = new FileInputStream(arquivoZip.toFile());
try(ZipInputStream zis = new ZipInputStream(is)) {
unzipStream(zis);
}
}
private static void unzipStream(ZipInputStream zis) throws IOException {
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
byte[] bytes = IOUtils.toByteArray(zis); // the error occurs here
zis.closeEntry();
}
}
And here's the stacktrace:
java.io.EOFException: Unexpected end of ZLIB input stream
at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
at java.util.zip.ZipInputStream.read(ZipInputStream.java:194)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2314)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:2270)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2291)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:2246)
at org.apache.commons.io.IOUtils.toByteArray(IOUtils.java:765)
BTW, I know that I can just adopt this solution: Exception: Unexpected end of ZLIB input stream but I don't want to implement this in my code and, actually, I have a Non GZIP Stream error when I tried to do this.
Can someone, please, help me?