Create Zip File From Multiple Files using ZipOutputStream Example

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

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




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!