Extract File With CRC32 Checksum - Java Security

Java examples for Security:CRC

Description

Extract File With CRC32 Checksum

Demo Code


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
 
public class Main {
 
        public static void main(String args[])
        {//from  w  w w.ja  v a2s  .  c  o  m
                String sourceZipFile = "C:/Folder/sourceFile.zip";
                try
                {
                        //create FileInputStream from the source zip file
                        FileInputStream fin = new FileInputStream(sourceZipFile);
                       
                        CheckedInputStream checksum = new CheckedInputStream(fin,new CRC32());
                         
                        ZipInputStream zin = new ZipInputStream(checksum);
                       
                        ZipEntry entry = zin.getNextEntry();
                       
                        OutputStream os = new FileOutputStream("c:/extractedFile.css");
                       
                        byte[] buffer = new byte[1024];
                        int length;
                       
                        //read the entry from zip file and extract it to disk
                        while( (length = zin.read(buffer)) > 0)
                        {
                                os.write(buffer, 0, length);
                        }
                        os.close();
                        zin.close();
                        System.out.println("File Extracted from zip file");
                        System.out.println("CRC32 checksum is: " + checksum.getChecksum().getValue());
                }
                catch(IOException e)
                {
                        System.out.println("IOException :" + e);
                }
               
        }
 
}

Result


Related Tutorials