Objective of this program is how to get compression method used to compress the entry using getMethod 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 GetCompressionMethod {
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("D:/WebFiles.zip");
/* Get list of zip entries using entries method of ZipFile class */
Enumeration e = zipFile.entries();
System.out.println("File Name\t\t\t\tCompression Method");
System.out.println("---------------------------------");
while(e.hasMoreElements())
{
ZipEntry entry = (ZipEntry)e.nextElement();
/* To get compression method used to compress entry, use int getMethod() method of ZipEntry class.This method returns the
method of compression, or -1 if not specified.Compression method can be either STORED or DEFLATED for any zip entry.*/
int method = entry.getMethod();
if(method == ZipEntry.DEFLATED)
System.out.println(entry.getName() + "\t\t\t\tDeflated");
else if(method == ZipEntry.STORED)
System.out.println(entry.getName() + "\t\t\t\tStored");
else if(method == -1)
System.out.println(entry.getName() + "\t\t\t\tNot Specified");
}
/* close the opened zip file using,void close() method.*/
zipFile.close();
}
catch(IOException ioe)
{
System.out.println("Error opening zip file" + ioe);
}
}
}