Extract File With CRC32 Checksum Example

  1. /*
  2. Extract File With CRC32 Checksum Example
  3. This Java example shows how to extract zip file with CRC32 Checksum
  4. using CheckedInputStream and CRC32 classes.
  5. */
  6.  
  7. import java.io.FileInputStream;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.io.OutputStream;
  11. import java.util.zip.CRC32;
  12. import java.util.zip.CheckedInputStream;
  13. import java.util.zip.ZipEntry;
  14. import java.util.zip.ZipInputStream;
  15.  
  16. public class ExtractZipWithCRC32Checksum {
  17.  
  18. public static void main(String args[])
  19. {
  20.  
  21. String sourceZipFile = "C:/FileIO/sourceFile.zip";
  22.  
  23. try
  24. {
  25. //create FileInputStream from the source zip file
  26. FileInputStream fin = new FileInputStream(sourceZipFile);
  27.  
  28. /*
  29. * To extract zip file with CRC32 checksum, use
  30. *
  31. * CheckedInputStream(InputStream in, Checksum checksum)
  32. *
  33. * Constructor.
  34. */
  35.  
  36. //create CheckedInputStream object.
  37. CheckedInputStream checksum = new CheckedInputStream(fin,new CRC32());
  38.  
  39. //create ZipInputStream from CheckedInputStream object
  40. ZipInputStream zin = new ZipInputStream(checksum);
  41.  
  42. //get the first entry from the source zip file
  43. ZipEntry entry = zin.getNextEntry();
  44.  
  45. //crate OutputStream to extract the entry from zip file
  46. OutputStream os = new FileOutputStream("c:/extractedFile.css");
  47.  
  48.  
  49. byte[] buffer = new byte[1024];
  50. int length;
  51.  
  52. //read the entry from zip file and extract it to disk
  53. while( (length = zin.read(buffer)) > 0)
  54. {
  55. os.write(buffer, 0, length);
  56. }
  57.  
  58. //close the streams
  59. os.close();
  60.  
  61. //close the zip file
  62. zin.close();
  63.  
  64. System.out.println("File Extracted from zip file");
  65.  
  66. /*
  67. * To get the CRC32 checksum of the extracted file, use
  68. *
  69. * Checksum getChecksum()
  70. *
  71. * Method of CheckedInputStream class.
  72. */
  73.  
  74. System.out.println("CRC32 checksum is: " + checksum.getChecksum().getValue());
  75.  
  76. }
  77. catch(IOException e)
  78. {
  79. System.out.println("IOException :" + e);
  80. }
  81.  
  82. }
  83.  
  84. }
  85.  
  86. /*
  87. Output of this example would be
  88. File Extracted from zip file
  89. CRC32 checksum is: 2920622546
  90. */

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!