Reputation: 386
I want to check the size of future uncompressed files before doing the unzip. Can someone confirm that the library does not start by uncompressing file in memory to get its final uncompressed size?
My code:
import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
public class ZipGetMethod {
public static void main(String[] args) {
String file = "file.zip";
try {
ZipFile zipfile = new ZipFile(file);
Enumeration e = zipfile.entries();
while (e.hasMoreElements()) {
ZipEntry zipentry = (ZipEntry) e.nextElement();
String entryName = zipentry.getName();
Date lastModify = new Date(zipentry.getTime());
long uncomSize = zipentry.getSize();
long comSize = zipentry.getCompressedSize();
} catch (IOException ex) {
System.out.println("IOException is : " + ex);
}
}
}
Upvotes: 0
Views: 588
Reputation: 26934
ZIP format stores uncompressed file size as part of the central directory (http://en.wikipedia.org/wiki/Zip_%28file_format%29#Structure), it is unnecessary to process the actual compressed data to determine file size.
Now, regarding the Java implementation you are using, depends on the platform but very likely it won't process file data nor the local header, and will refer to the central directory (or the various central directories if the zip file had been appended data).
A quick look at OpenJDK implementation (http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/zip/ZipEntry.java#ZipEntry.getSize%28%29) suggests that the ZipEntry is loaded with file size data from the ZIP directory, and getSize()
simply returns that value without further inspection of the compressed data.
Upvotes: 2
Reputation: 20174
No. The uncompressed size of each file is stored in the Zip index, so decompressing is not necessary to find the uncompressed size of a given file.
Upvotes: 1