In this example we are going to see Create Checksum For Zip File 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 | import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.Adler32; import java.util.zip.CheckedOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class JavaCheckSumZipFile { public static void main(String[] args) throws Exception { CheckedOutputStream checksum = new CheckedOutputStream(new FileOutputStream("data.zip"), new Adler32()); ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(checksum)); int size = 0; byte[] buffer = new byte[1024]; File dir = new File("."); String[] files = dir.list(); for (int i = 0; i < files.length; i++) { System.out.println("Compressing: " + files[i]); FileInputStream fis = new FileInputStream(files[i]); ZipEntry zipEntry = new ZipEntry(files[i]); zos.putNextEntry(zipEntry); while ((size = fis.read(buffer, 0, buffer.length)) > 0) { zos.write(buffer, 0, size); } zos.closeEntry(); fis.close(); } zos.close(); System.out.println("Checksum : " + checksum.getChecksum().getValue()); } } |
A checksum is a small-size datum from a block of digital data for the purpose of detecting errors which may have been introduced during its transmission or storage.
Checksum is not computed from the complete zip file, but from the user’s data only.