Getting the size of ZipInputStream

Pradeep Vairamani picture Pradeep Vairamani · Jun 19, 2012 · Viewed 7.1k times · Source

Is there anyway to find/estimate the size of ZipInputStream before we completely read the stream?

For instance, we can get the entry's metadata with getNextEntry before we can read the userdata.

Inputstream has a method available() to return an estimate of the number of bytes that can be read from this input stream but I am not able to find a similar method for ZipInputStream.

Answer

Rahul Agrawal picture Rahul Agrawal · Jun 19, 2012

ZipInputStream has method available() but it returns 0 or 1.

To get the estimated size of any type of file, you can use FileInputStream and then to read zip file use ZipInputStream. Ex.

 public class ZipUtil {

    public static void main(String[] args) throws Exception {
        ZipInputStream zis = null;

        FileInputStream fis = new FileInputStream("C:/catalina.zip");
        int size = fis.available();
        System.out.println("size in KB : " + size/1024);
        zis = new ZipInputStream(fis);        

        ZipEntry ze;
        while ((ze = zis.getNextEntry()) != null) {
            System.out.println(ze.getName());
        }
    }
}