Create Zip File using ZipOutputStream Example

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

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!