9

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?

yottabrain
  • 2,387
  • 5
  • 23
  • 37
  • FWIW if you call `getSize` after reading the stream for that ZipEntry, then it will return the right size. Weird. – rogerdpack Mar 02 '18 at 17:55

3 Answers3

9

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

elxala
  • 291
  • 3
  • 5
1

you'll get a return of -1 if that is what is in the ZipFile entry table. This is quite simply just an aspect of the definition of the zip file format.

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
0

This code will return the correct size of zipEntry:

ZipInputStream( BufferedInputStream( cr.openInputStream(zipUri) ) ).use { srcZipStream ->
        var zipEntry: ZipEntry?
        while (srcZipStream.nextEntry.also { zipEntry = it} != null) {
            srcZipStream.closeEntry()
            allZipEntryList.add(zipEntry!!.name)
            Log.e("wkExtract",zipEntry!!.name+" size= "+zipEntry!!.size+" compressed size= "+zipEntry!!.compressedSize)
        }
}