Create Zip File With CRC32 Checksum Example

  1. /*
  2. Create Zip File With CRC32 Checksum Example
  3. This Java example shows how to create a zip file and generate the
  4. checksum value using Java CheckedOutputStream and CRC32 classes.
  5. */
  6.  
  7. import java.io.FileInputStream;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.util.zip.CRC32;
  11. import java.util.zip.CheckedOutputStream;
  12. import java.util.zip.ZipEntry;
  13. import java.util.zip.ZipOutputStream;
  14.  
  15. public class CompressFileWithCRC32Checksum {
  16.  
  17. public static void main(String args[])
  18. {
  19. String zipFile = "C:/FileIO/zipdemo.zip";
  20. String sourceFile = "C:/FileIO/sourcefile.doc";
  21.  
  22. // create byte buffer
  23. byte[] buffer = new byte[1024];
  24.  
  25. try
  26. {
  27. //create object of FileOutputStream
  28. FileOutputStream fout = new FileOutputStream(zipFile);
  29.  
  30. /*
  31. * Create an object of CheckedOutputStream using,
  32. *
  33. * CheckedOutputStream(OutputStream out, Checksum checksum)
  34. *
  35. * constructor.
  36. *
  37. * The second argument is the Checksum algorithm we want to use.
  38. * It can be either CRC32 or Adler32.
  39. *
  40. */
  41.  
  42. CheckedOutputStream checksum = new CheckedOutputStream(fout, new CRC32());
  43.  
  44. //create an object of ZipOutputStream
  45. ZipOutputStream zout = new ZipOutputStream(checksum);
  46.  
  47. //create object of FileInputStream for source file
  48. FileInputStream fin = new FileInputStream(sourceFile);
  49.  
  50. zout.putNextEntry(new ZipEntry(sourceFile));
  51.  
  52. int length;
  53.  
  54. while((length = fin.read(buffer)) > 0)
  55. {
  56. zout.write(buffer, 0, length);
  57. }
  58.  
  59. zout.closeEntry();
  60.  
  61. //close the InputStream
  62. fin.close();
  63.  
  64. //close the ZipOutputStream
  65. zout.close();
  66.  
  67. System.out.println("Zip file has been created!");
  68.  
  69. /*
  70. * Get the generated checksum value using
  71. *
  72. * Checksum getChecksum()
  73. *
  74. * method.
  75. *
  76. * This method returns the checksum for the output stream.
  77. */
  78. System.out.println("CRC32 Checksum is : " + checksum.getChecksum().getValue());
  79. }
  80. catch(IOException ioe)
  81. {
  82. System.out.println("IOException : " + ioe);
  83. }
  84.  
  85.  
  86. }
  87.  
  88. }
  89.  
  90. /*
  91. Output of this example would be
  92. Zip file has been created!
  93. CRC32 Checksum is : 1481449702
  94. */

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!