Use of getMethod, getSize and getTime method of ZipEntry class in java.


 

Use of getMethod, getSize and getTime method of ZipEntry class in java.

In this tutorial you will see use of getMethod, getSize and getTime method of ZipEntry class in java.

In this tutorial you will see use of getMethod, getSize and getTime method of ZipEntry class in java.

Use of getMethod, getSize and getTime method of ZipEntry class in java.

In this Example, We will discuss the use getMethod, getSize and getTime method of ZipEntry class.
The ZipEntry class is used to show the single entry in ZIP file. The ZipEntery class provides methods for
retrieving information related to entry. The ZipEntry class is available in java.util.zip package.
The getMethod method of ZipEntry class returns compression method of entry, and the
getSize method returns actual size of entry. The getTime method of ZipEntry class  returns the
modification time of entry. 

In this Example, The ZipFile class create an object of given zip file and read entries. There are two method
of ZipEntry class ZipEntry.STORED and ZipEntry.DEFLATED.The entries() method of ZipFile class 
returns enumeration of the zip file. The hasMoreElements() of method of Enumeration class checks 
availability of entry. The nextElements() of Enumeration class returns next elements of enumeration. 
The getName method of ZipEntry class return the name of associated entry. The getCompressedSize  
method of zipEntry class returns actual compressed sized of entry.

About ZIPEntry API:

The java.util.zip.ZIPEntry class extends java.io.FilterIntputStream class. It provides the following methods:

Return Type Method Description 
Boolean  getMethod() The getMethod() returns compression method of associated entry.  
String getSize() The getSize()  method returns name actual size of associated entry.
void getTime() The getTime() method returns last modification time of associated entry.

code

import java.util.*;
import java.util.zip.*;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;

public class ZipGetMethod {
  public static void main(String[] args) {
    String file = "file.zip";
    try {
      ZipFile zipfile = new ZipFile(file);
      Enumeration e = zipfile.entries();
      while (e.hasMoreElements()) {
        ZipEntry zipentry = (ZipEntrye.nextElement();
        String entryName = zipentry.getName();
        Date lastModify = new Date(zipentry.getTime());
        long uncomSize = zipentry.getSize();
        long comSize = zipentry.getCompressedSize();

        if ((zipentry.getMethod()) == ZipEntry.STORED) {
          System.out.println("The entry  Nmae " + entryName
              " was stored at " + lastModify);
          System.out.println("with a size of  " + uncomSize
              " bytes");
          System.out.println(" ");
        else if ((zipentry.getMethod()) == ZipEntry.DEFLATED) {
          System.out.println("The entry  Nmae " + entryName
              " was deflated at " + lastModify);
          System.out.println("Uncompressed size of entry : "
              + uncomSize + " bytes");
          System.out.println("Compressed size of entry : " + comSize
              " bytes");
          System.out.println(" ");
        else {
          System.out.println("The entry  Nmae " + entryName
              " was compressed, but method name not specified "
              + lastModify);
          System.out.println("Uncompressed size of entry : "
              + uncomSize + " bytes");
          System.out.println("Compressed size of entry : " + comSize
              " bytes");
        }
      }
    catch (IOException ex) {
      System.out.println("IOException is : " + ex);
    }

  }
}

Following is the output if you run the application:

C:\>java ZipGetMethod
The entry Nmae data/ was stored at Tue Jul 20 15:58:28 GMT+05:30 2010
with a size of 0 bytes
The entry Nmae data/url.txt was deflated at Tue Jul 20 15:59:36 GMT+05:30 2010
Uncompressed size of entry : 437 bytes
Compressed size of entry : 291 bytes

Download this code

Ads