Skip to Content

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