Skip to Content

Create Zip File With CRC32 Checksum Example

  • strict warning: Non-static method view::load() should not be called statically in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_argument.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter_boolean_operator.inc on line 0.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_style_default.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.

  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. */