Home Tutorial Java Corejava Zip Use of setCrc, setSize and getCrc method of ZipEntry class.

 
 

Share on Google+Share on Google+
Use of setCrc, setSize and getCrc method of ZipEntry class.
Posted on: July 21, 2010 at 12:00 AM
Advertisement
In this tutorial you will see the use of setCrc, setSize and getCrc method of ZipEntry class.

Use of setCrc, setSize and getCrc method of ZipEntry class in java.

In this tutorial, We will discuss the use of setCrc, setSize and getCrc method of ZipEntry class.The
ZipEntry class is used to show the single entry of ZIP file. The ZipEntery class provides methods
for retrieving information related to entry. The ZipEntry class is available in java.util.zip package.

          In this example, we will set the size and set CRC( Cyclic Redundancy Check ) of entry.  The
setCrc method of ZipEntry class set crc_32 checksum of uncompressed entry data, and the  getCrc
method of ZipEntry class returns crc_32 checksum of uncompressed entry data. The  setSize method
set the uncompressed size of this entry. The getSize method of ZipEntry class returns uncompressed
size of entry data.

About ZIPEntry API:

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

Return Type Method Description 
void setCrc() The setCrc() method set CRC-32 checksum of uncompressed associated entry data.  
void setSize() The setSize() method set the size of uncompressed associated entry data.
long getCrc() The getCrc() method returns crc_32 checksum of uncompressed associated entry data.
long getSize() The getSize() method uncompressed size of associated entry data.

code

import java.io.*;
import java.util.zip.*;
import java.util.zip.ZipEntry;
import java.io.FileOutputStream;
import java.util.zip.ZipOutputStream;

class ZipEntrySetCrc {
  public static void main(String[] args) {
    try {
      String file = "bharat.txt";
      String zipfile = "bharat.zip";
      ZipEntrySetCrc list = new ZipEntrySetCrc();
      list.doZip(file, zipfile);
    catch (Exception e) {
      System.out.println("IOException :" + e);
    }
  }

  public void doZip(String file, String zipfile) {
    try {
      byte[] b = new byte[1024];
      FileInputStream finStream = new FileInputStream(file);
      finStream.read(b, 0, b.length);
      CRC32 crc32 = new CRC32();
      ZipOutputStream zipOutStream = new ZipOutputStream(
          (OutputStreamnew FileOutputStream(zipfile));
      zipOutStream.setLevel(6);
      ZipEntry zipentry = new ZipEntry(file);
      zipentry.setSize((longb.length);
      System.out.println("Size of entry after setSize method : "
          + zipentry.getSize());
      crc32.reset();
      crc32.update(b);
      zipentry.setCrc(crc32.getValue());
      System.out.println("Value of crc after setCRC method : "
          + zipentry.getCrc());
      zipOutStream.putNextEntry(zipentry);
      zipOutStream.write(b, 0, b.length);
      zipOutStream.finish();
      zipOutStream.close();
      System.out.println("File successfully compressed");
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Following is the output if you run the application:

C:\>java ZipEntrySetCrc
Size of entry after setSize method : 1024
Value of crc after setCRC method : 2785745967
File successfully compressed

Download this code

Advertisement

Related Tags for Use of setCrc, setSize and getCrc method of ZipEntry class.:


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 21, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com