/*
Get Uncompressed Size of Zip Entry Example
This Java example shows how to get uncompressed size of particular entry
(i.e. file or directory) using getSize method of
Java ZipEntry class.
*/
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class GetUncompressedSize {
public static void main(String args[])
{
try
{
/*
* To Open a zip file, use
*
* ZipFile(String fileName)
* constructor of the ZipFile class.
*
* This constructor throws IOException for any I/O error.
*/
ZipFile zipFile = new ZipFile("c:/FileIO/WebFiles.zip");
/*
* Get list of zip entries using entries method of
* ZipFile class.
*/
Enumeration e = zipFile.entries();
System.out.print("File Name");
System.out.print("\t\tUncompressed Size");
System.out.println("\t\tCompressed Size");
System.out.println("---------------------------------");
while(e.hasMoreElements())
{
ZipEntry entry = (ZipEntry)e.nextElement();
/*
* To get uncompressed size of zip entry, use
*
* long getSize()
* method of ZipEntry class.
*
* This method returns the size of uncompressed entry or -1
* if the size is not known.
*
* PLEASE NOTE that uncompressed size and compressed size
* of the zip entries created using ZipEntry.STORED method
* will be same.
*
* However, It differes for zip entries created using
* ZipEntry.DEFLATED entries.
*/
String entryName = entry.getName();
long originalSize = entry.getSize();
long compressedSize = entry.getCompressedSize();
System.out.print(entryName);
System.out.print("\t\t" + originalSize);
System.out.print("\t\t" + compressedSize);
System.out.print("\n");
}
/*
* close the opened zip file using,
* void close()
* method.
*/
zipFile.close();
}
catch(IOException ioe)
{
System.out.println("Error opening zip file" + ioe);
}
}
}
/*
Output of this program would be
File Name Uncompressed Size Compressed Size
---------------------------------
css/datagrid.css 26939 3255
css/graph.css 2412 416
jsps/Masthead.jspf 1200 536
jsps/Welcome.jsp 3062 1248
*/
Post new comment