Create Zip File With CRC32 Checksum - Java File Path IO

Java examples for File Path IO:Zip File

Description

Create Zip File With CRC32 Checksum

Demo Code


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
public class Main {
       //from  ww  w  .j av  a  2 s.c o m
        public static void main(String args[])
        {              
                String zipFile = "C:/Folder/zipdemo.zip";
                String sourceFile = "C:/Folder/sourcefile.doc";
               
                byte[] buffer = new byte[1024];
               
                try
                {
                        //create object of FileOutputStream
                        FileOutputStream fout = new FileOutputStream(zipFile);
                        CheckedOutputStream checksum = new CheckedOutputStream(fout, new CRC32());
                        //create an object of ZipOutputStream
                        ZipOutputStream zout = new ZipOutputStream(checksum);
                        FileInputStream fin = new FileInputStream(sourceFile);
                        zout.putNextEntry(new ZipEntry(sourceFile));
                        int length;
                        while((length = fin.read(buffer)) > 0)
                        {
                           zout.write(buffer, 0, length);
                        }
                        zout.closeEntry();
                        fin.close();
                        zout.close();
                        System.out.println("Zip file has been created!");
                        System.out.println("CRC32 Checksum is : " + checksum.getChecksum().getValue());
                }
                catch(IOException ioe)
                {
                        System.out.println("IOException : " + ioe);
                }
        }
}
 

Result


Related Tutorials