Zipentry example in java


 

Zipentry example in java

In this tutorial you will see the use of Zipentry class.

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

Ads