Java I/O How to - Get zip method: ZipEntry.STORED, ZipEntry.DEFLATED








Question

We would like to know how to get zip method: ZipEntry.STORED, ZipEntry.DEFLATED.

Answer

 /*from w w w  .  jav a2s  . co  m*/

import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class MainClass {

  public static void main(String[] args) {

    try {
      ZipFile zf = new ZipFile("your.zip");
      Enumeration e = zf.entries();
      while (e.hasMoreElements()) {
        ZipEntry ze = (ZipEntry) e.nextElement();
        String name = ze.getName();
        
        Date lastModified = new Date(ze.getTime());
        long uncompressedSize = ze.getSize();
        long compressedSize = ze.getCompressedSize();
        
        int method = ze.getMethod();

        if (method == ZipEntry.STORED) {
          System.out.println(name + " was stored at " + lastModified);
          System.out.println("with a size of  " + uncompressedSize + " bytes");
        } else if (method == ZipEntry.DEFLATED) {
          System.out.println(name + " was deflated at " + lastModified);
          System.out
              .println("from  " + uncompressedSize + " bytes to " + compressedSize
                  + " bytes, a savings of " + (100.0 - 100.0 * compressedSize / uncompressedSize)
                  + "%");
        } else {
          System.out.println(name + " was compressed using an unrecognized method at "
              + lastModified);
          System.out
              .println("from  " + uncompressedSize + " bytes to " + compressedSize
                  + " bytes, a savings of " + (100.0 - 100.0 * compressedSize / uncompressedSize)
                  + "%");
        }        
      }
    } catch (IOException ex) {
      System.err.println(ex);
    }
  }
}

The code above generates the following result.