Create Zip File From Directory using ZipOutputStream Example

  1. /*
  2. Create Zip File From Directory using ZipOutputStream Example
  3. This Java example shows how create zip file from directory
  4. using Java ZipOutputStream class.
  5. */
  6.  
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.util.zip.ZipEntry;
  12. import java.util.zip.ZipException;
  13. import java.util.zip.ZipOutputStream;
  14.  
  15.  
  16. public class CreateZipFileDirectory {
  17.  
  18. public static void main(String args[])
  19. {
  20. try
  21. {
  22. String zipFile = "C:/FileIO/zipdemo.zip";
  23. String sourceDirectory = "C:/examples";
  24.  
  25. //create byte buffer
  26. byte[] buffer = new byte[1024];
  27. /*
  28. * To create a zip file, use
  29. *
  30. * ZipOutputStream(OutputStream out)
  31. * constructor of ZipOutputStream class.
  32. *
  33. */
  34.  
  35. //create object of FileOutputStream
  36. FileOutputStream fout = new FileOutputStream(zipFile);
  37.  
  38. //create object of ZipOutputStream from FileOutputStream
  39. ZipOutputStream zout = new ZipOutputStream(fout);
  40.  
  41. //create File object from directory name
  42. File dir = new File(sourceDirectory);
  43.  
  44. //check to see if this directory exists
  45. if(!dir.isDirectory())
  46. {
  47. System.out.println(sourceDirectory + " is not a directory");
  48. }
  49. else
  50. {
  51. File[] files = dir.listFiles();
  52.  
  53. for(int i=0; i < files.length ; i++)
  54. {
  55. System.out.println("Adding " + files[i].getName());
  56.  
  57. //create object of FileInputStream for source file
  58. FileInputStream fin = new FileInputStream(files[i]);
  59.  
  60. /*
  61. * To begin writing ZipEntry in the zip file, use
  62. *
  63. * void putNextEntry(ZipEntry entry)
  64. * method of ZipOutputStream class.
  65. *
  66. * This method begins writing a new Zip entry to
  67. * the zip file and positions the stream to the start
  68. * of the entry data.
  69. */
  70.  
  71. zout.putNextEntry(new ZipEntry(files[i].getName()));
  72.  
  73. /*
  74. * After creating entry in the zip file, actually
  75. * write the file.
  76. */
  77. int length;
  78.  
  79. while((length = fin.read(buffer)) > 0)
  80. {
  81. zout.write(buffer, 0, length);
  82. }
  83.  
  84. /*
  85. * After writing the file to ZipOutputStream, use
  86. *
  87. * void closeEntry() method of ZipOutputStream class to
  88. * close the current entry and position the stream to
  89. * write the next entry.
  90. */
  91.  
  92. zout.closeEntry();
  93.  
  94. //close the InputStream
  95. fin.close();
  96. }
  97. }
  98.  
  99. //close the ZipOutputStream
  100. zout.close();
  101.  
  102. System.out.println("Zip file has been created!");
  103.  
  104. }
  105. catch(IOException ioe)
  106. {
  107. System.out.println("IOException :" + ioe);
  108. }
  109.  
  110. }
  111. }
  112.  
  113. /*
  114. Output of this program would be
  115. Adding nonav.log
  116. Adding ospreg.exe
  117. Adding servers.ini
  118. Adding setupisam.log
  119. Adding sourceFile1.doc
  120. Adding sourceFile2.doc
  121. Zip file has been created!
  122. */

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!