List the Zip method in Java

Description

The following code shows how to list the Zip method.

Example


/*from  ww  w.  j  a  va2 s . com*/
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {

  public static void main(String[] args) throws Exception {
    ZipFile zf = new ZipFile("a.zip");
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
      ZipEntry ze = (ZipEntry) e.nextElement();
      String name = ze.getName();
      
      long uncompressedSize = ze.getSize();
      long compressedSize = ze.getCompressedSize();
      long crc = ze.getCrc();
      int method = ze.getMethod();
      String comment = ze.getComment();

      System.out.println(name + " was stored at " + new Date(ze.getTime()));
      if (method == ZipEntry.STORED) {
        System.out.println("with a size of  " + uncompressedSize + " bytes");
      } else if (method == ZipEntry.DEFLATED) {
        System.out.println("from " + uncompressedSize + " bytes to " + compressedSize);
      } else {
        System.out.println("from " + uncompressedSize + " bytes to " + compressedSize);
      }
      System.out.println("Its CRC is " + crc);
      if (comment != null && !comment.equals("")) {
        System.out.println(comment);
      }
      if (ze.isDirectory()) {
        System.out.println(name + " is a directory");
      }
    }
  }
}




















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip