Home Tutorial Java Corejava Zip Zipentry example in java

 
 

Share on Google+Share on Google+
Zipentry example in java
Posted on: July 14, 2010 at 12:00 AM
Advertisement
In this tutorial you will see the use of Zipentry class.

Zipentry example in java

In this Example, we will discuss the use of ZipEntry class The ZipEntry class is used to show the entry in ZIP file and also property Name and size of data etc. The ZipEntry class is available in java.util.zip package.

In this Example, we will discuss about the ZIP file entry. The ZipEntry class create a instance of zip entry which represent Zip file entry. . The getNextEntry method of ZipInputStream class is used to read next ZIP file entry and position  at the beginning of the entry data, and the getName method of ZipEntry class return the name of associated entry. And  the getCompressSize methed of ZipEntry class return compress size of entry. The getSize methed of ZipEntry class return actual size of entry.

About ZIPEntry API:

Return Type Method Description 
long getCompressSize() The method getCompressedSize() return compressed Size of entry.
String getName() The method getName() returns name of the associated entry.
long getSize() The method getSize() returns the actual size of current entry.

Code

import java.io.*;
import java.util.zip.*;
import java.io.FileInputStream.*;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

class Demo {
  public static void doEntry(String filethrows IOException {
    FileInputStream inStream = new FileInputStream(file);
    ZipInputStream zis = new ZipInputStream(inStream);
    ZipEntry entry;
    while ((entry = zis.getNextEntry()) != null) {
      System.out.println("Name of Entry in Given Zip file : "
          + entry.getName());
      System.out.println("Size of Entry in Given Zip file : "
          + entry.getCompressedSize()+" byte");
      System.out.println("Actual Size of Entry : "
          + entry.getSize()+" byte");
    }
  }
}

public class ZipEntryDemo {
  public static void main(String[] argsthrows IOException {
    Demo.doEntry(args[0]);
  }
}

Following is the output if you run the application:

C:\>java ZipEntryDemo C:\Work\Bharat\Roseindi_Tutorial\Zip_api\zipnextentryeee\bharat.zip
Name of Entry in Given Zip file : testfile2.txt
Size of Entry in Given Zip file is : 83 byte
Actual Size of Entry is : 180 byte
Name of Entry in Given Zip file : testfile1.txt
Size of Entry in Given Zip file is : 32 byte
Actual Size of Entry is : 34 byte

Download this code

Advertisement

Related Tags for Zipentry example in java:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 14, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com