Search code examples
javazipinputstream

Why zipInputStream is empty though entry exists?


I am trying to unzip archive, zis.getNextEntry() gives me nextEntry, I can see the correct name of the entry, but zip input stream itself is empty. Why?

byte[] htmlFile = new byte[]{};           
        ByteArrayInputStream bais =  new ByteArrayInputStream(Base64.decodeBase64(template.getKey().getFileBase64()));
        zis = new ZipInputStream(bais);
        ZipEntry ze = null;
        try {
             while ((ze = zis.getNextEntry()) != null) {
                if (!ze.isDirectory()) {
                    byte[] tempEntry = new byte[]{};
                    try {
                        zis.read(tempEntry);
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }                        
                }
            }
            try {
                zis.closeEntry();
                zis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

Below is some debug info, where you can see - the entry exists, but nothing is read from the stream:

enter image description here


Solution

  • As per JavaDoc

    public int read(byte[] b)
             throws IOException
    

    Reads up to byte.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available

    This method simply performs the call read(b, 0, b.length) and returns the result.

    Since length of tempEntry is 0 hence nothing gets read from the Stream