Search code examples
javazip4j

zip4j ZipFile getInputStream always returns byte array with size 1


The following code always results to a byte array with size 1, any ideas ?

FileHeader fh = (FileHeader) packageFile.getFileHeaders().get(1);
InputStream inputStream = packageFile.getInputStream(fh);
byte[] bytes=new byte[inputStream.available()];

the zip file is fine!


Solution

  • What do you expect? Perhaps your understanding of available() isn't correct. The method returns an estimate but is dependent on implementation.

    From the docs;

    Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

    It looks like you are defining a buffer bytes to read the data into.