zip
Get zip entry compression method
This is an example of how to get the ZipEntry Compression method. The compression method of a ZipEntry can be either STORED
for uncompressed entries, or DEFLATED
for compressed (deflated) entries. Getting the compression method of the ZipEntry implies that you should:
- Create a new ZipFile to read a zip file with the given name.
- Get the Enumeration of the ZipEntry objects of the ZipFile, with
entries()
API method of ZipFile and iterate through each one of them. - For each one of the ZipEntry objects get its Compression method, with
getMethod()
API method of ZipEntry. The method returns either the compression method or -1 if no compression method is specified.
Let’s take a look at the code snippet that follows:
package com.javacodegeeks.snippets.core; import java.io.IOException; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class GetZipEntryCompressionMethod { public static void main(String[] args) { ZipFile zipFile = null; try { // open a zip file for reading zipFile = new ZipFile("c:/archive.zip"); // get an enumeration of the ZIP file entries Enumeration<? extends ZipEntry> e = zipFile.entries(); while (e.hasMoreElements()) { ZipEntry entry = e.nextElement(); // get the compression method of the entry, or -1 if not specified int method = entry.getMethod(); if (method == ZipEntry.DEFLATED) { System.out.println(entry.getName() + " is Deflated"); } else if (method == ZipEntry.STORED) { System.out.println(entry.getName() + "is Stored"); } else if (method == -1) { System.out.println(entry.getName() + " is Not Specified"); } } } catch (IOException ioe) { System.out.println("Error opening zip file" + ioe); } finally { try { if (zipFile!=null) { zipFile.close(); } } catch (IOException ioe) { System.out.println("Error while closing zip file" + ioe); } } } }
This was an example of how to get the Compression method of a ZipEntry in Java.