Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the size of ZipInputStream

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.

like image 959
Pradeep Vairamani Avatar asked Jun 19 '12 09:06

Pradeep Vairamani


People also ask

How to find Size of Zip file in Java?

ZipFile size() function in Java with examples util. zip package. The function returns the number of entries of the zip file. Example 1: Create a file named zip_file and get the number of entries using size() function.”file.

How do I read a Zipinputstream file?

ZipEntry getNextEntry() : Reads the next ZIP file entry and positions the stream at the beginning of the entry data. int read(byte[] b, int off, int len) : Reads from the current ZIP entry into an array of bytes.

What is Zipinputstream in Java?

This class implements an input stream filter for reading files in the ZIP file format. Includes support for both compressed and uncompressed entries.


3 Answers

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());
        }
    }
}
like image 84
Rahul Agrawal Avatar answered Oct 09 '22 15:10

Rahul Agrawal


If you have just the stream, then I don't think so.

The zip file format has just a sequence of entries, and then a global directory (which has a table of all files and their sizes) at the end. So without access to the file, you won't get to that information.

like image 39
Thilo Avatar answered Oct 09 '22 13:10

Thilo


Inputstream has a method available() to return an estimate of the number of bytes that can be read from this input stream

without blocking. Not the same thing. There is a specific warning in the Javadoc about not treating this value as a total file size.

but I am not able to find a similar method for ZipInputStream.

That's strange because it's there. However it returns zero, which is the best estimate it can make of how much can be read without blocking.

Why? Because (a) it's a stream, and (ii) it's a zipped stream. There is no way of knowing how much there is without reading it all; and there is no way of knowing how much of that can be read without blocking,

like image 26
user207421 Avatar answered Oct 09 '22 14:10

user207421