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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
/* Create Zip File With Adler32 Checksum Example This Java example shows how to create a zip file and generate the checksum value using Java CheckedOutputStream and Adler32 classes. */ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.Adler32; import java.util.zip.CheckedOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class CompressFileWithAdler32Checksum { public static void main(String args[]) { String zipFile = "C:/FileIO/zipdemo.zip"; String sourceFile = "C:/FileIO/sourcefile.doc"; // create byte buffer byte[] buffer = new byte[1024]; try { //create object of FileOutputStream FileOutputStream fout = new FileOutputStream(zipFile); /* * Create an object of CheckedOutputStream using, * * CheckedOutputStream(OutputStream out, Checksum checksum) * * constructor. * * The second argument is the Checksum algorithm we want to use. * It can be either CRC32 or Adler32. * */ CheckedOutputStream checksum = new CheckedOutputStream(fout, new Adler32()); //create an object of ZipOutputStream ZipOutputStream zout = new ZipOutputStream(checksum); //create object of FileInputStream for source file 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(); //close the InputStream fin.close(); //close the ZipOutputStream zout.close(); System.out.println("Zip file has been created!"); /* * Get the generated checksum value using * * Checksum getChecksum() * * method. * * This method returns the checksum for the output stream. */ System.out.println("Adler32 Checksum is : " + checksum.getChecksum().getValue()); } catch(IOException ioe) { System.out.println("IOException : " + ioe); } } } /* Output of this example would be Zip file has been created! Adler32 Checksum is : 2587378163 */ |
If you run it again and again you will get different numbers. Should it be like that?
I thought checksum of the same file should be the same.