In this example we are going to see how Create Zip File With CRC32 Checksum In Java.
Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
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 JavaCreateZipFileWithCRC32 { public static void main(String args[]) { String zipFile = "C:/File/Demozip.zip"; String sourceFile = "C:/File/file1.doc"; byte[] buffer = new byte[1024]; try { FileOutputStream fout = new FileOutputStream(zipFile); CheckedOutputStream checksum = new CheckedOutputStream(fout, new CRC32()); 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); } } } |
Create Zip File With CRC32 Checksum, Java