Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java zipentry getsize returns -1

Java zipEntry.getSize() returns the size of the actual file and some times it returns -1 (though the file size is greater than 0).

Java API document says "Returns the uncompressed size of the entry data, or -1 if not known."

Not sure on what situation it will return -1 i.e. on what situation it will be unknown?

like image 464
yottabrain Avatar asked Mar 16 '16 19:03

yottabrain


Video Answer


1 Answers

Surprisely using ZipFile instead of ZipInputStream for getting the entries makes getSize and getCompressedSize to return the right values.

     ZipFile zipfile = new ZipFile("myFile.zip"); 
     java.util.Enumeration zipEnum = zipfile.entries();
     while (zipEnum.hasMoreElements ()) 
     { 
        ZipEntry entry = (ZipEntry) zipEnum.nextElement(); 
        if (! entry.isDirectory ())
        {
            // entry.getName()
            // entry.getSize ()
            // entry.getCompressedSize ()
         }
     }

trick found at http://vimalathithen.blogspot.de/2006/06/using-zipentrygetsize.html

like image 89
elxala Avatar answered Oct 24 '22 12:10

elxala