Skip to Content

Create Zip File using ZipOutputStream 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 using ZipOutputStream Example
  3.         This Java example shows how create zip file containing one file
  4.         using Java ZipOutputStream class.
  5. */
  6.  
  7. import java.io.FileInputStream;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.util.zip.ZipEntry;
  11. import java.util.zip.ZipException;
  12. import java.util.zip.ZipOutputStream;
  13.  
  14.  
  15. public class CreateZipFile {
  16.        
  17.         public static void main(String args[])
  18.         {                
  19.                 try
  20.                 {
  21.                         String zipFile = "C:/FileIO/zipdemo.zip";
  22.                         String sourceFile = "C:/FileIO/sourcefile.doc";
  23.                        
  24.                         //create byte buffer
  25.                         byte[] buffer = new byte[1024];
  26.                         /*
  27.                          * To create a zip file, use
  28.                          *
  29.                          * ZipOutputStream(OutputStream out)
  30.                          * constructor of ZipOutputStream class.
  31.                          *  
  32.                          */
  33.                          
  34.                          //create object of FileOutputStream
  35.                          FileOutputStream fout = new FileOutputStream(zipFile);
  36.                          
  37.                          //create object of ZipOutputStream from FileOutputStream
  38.                          ZipOutputStream zout = new ZipOutputStream(fout);
  39.                          
  40.                          //create object of FileInputStream for source file
  41.                          FileInputStream fin = new FileInputStream(sourceFile);
  42.                          
  43.                          /*
  44.                           * To begin writing ZipEntry in the zip file, use
  45.                           *
  46.                           * void putNextEntry(ZipEntry entry)
  47.                           * method of ZipOutputStream class.
  48.                           *
  49.                           * This method begins writing a new Zip entry to
  50.                           * the zip file and positions the stream to the start
  51.                           * of the entry data.
  52.                           */
  53.                          
  54.                          zout.putNextEntry(new ZipEntry(sourceFile));
  55.                          
  56.                          /*
  57.                           * After creating entry in the zip file, actually
  58.                           * write the file.
  59.                           */
  60.                          int length;
  61.                          
  62.                          while((length = fin.read(buffer)) > 0)
  63.                          {
  64.                                 zout.write(buffer, 0, length);
  65.                          }
  66.                          
  67.                          /*
  68.                           * After writing the file to ZipOutputStream, use
  69.                           *
  70.                           * void closeEntry() method of ZipOutputStream class to
  71.                           * close the current entry and position the stream to
  72.                           * write the next entry.
  73.                           */
  74.                          
  75.                           zout.closeEntry();
  76.                          
  77.                           //close the InputStream
  78.                           fin.close();
  79.                          
  80.                           //close the ZipOutputStream
  81.                           zout.close();
  82.                          
  83.                           System.out.println("Zip file has been created!");
  84.                
  85.                 }
  86.                 catch(IOException ioe)
  87.                 {
  88.                         System.out.println("IOException :" + ioe);
  89.                 }
  90.                
  91.         }
  92. }
  93.  
  94. /*
  95. Output of this program would be
  96. Zip file has been created!
  97. */