Extract Zip File With Subdirectories Using Command Line Argument Example

  1. /*
  2. Extract Zip File With Subdirectories Using Command Line Argument Example.
  3. This Java example shows how to extract a zip file and create required
  4. sub-directories using Java ZipInputStream class.
  5. */
  6.  
  7. import java.io.BufferedInputStream;
  8. import java.io.BufferedOutputStream;
  9. import java.io.File;
  10. import java.io.FileInputStream;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.io.OutputStream;
  15. import java.util.Enumeration;
  16. import java.util.zip.ZipEntry;
  17. import java.util.zip.ZipFile;
  18. import java.util.zip.ZipInputStream;
  19.  
  20. public class ExtractFileSubDirectories {
  21.  
  22. public static void main(String args[])
  23. {
  24.  
  25. /*
  26. * This program expects one command line argument
  27. *
  28. * 1. Path of zip file
  29. */
  30.  
  31. //fetch command line argument
  32. String strZipFile = args[0];
  33.  
  34. if(strZipFile == null || strZipFile.equals(""))
  35. {
  36. System.out.println("Invalid source file");
  37. System.exit(0);
  38. }
  39.  
  40. //call method to unzip/extract files from zip file
  41. unzip(strZipFile);
  42.  
  43. System.out.println("Zip file extracted!");
  44. }
  45.  
  46. private static void unzip(String strZipFile) {
  47.  
  48. try
  49. {
  50. /*
  51. * STEP 1 : Create directory with the name of the zip file
  52. *
  53. * For e.g. if we are going to extract c:/demo.zip create c:/demo
  54. * directory where we can extract all the zip entries
  55. *
  56. */
  57. File fSourceZip = new File(strZipFile);
  58. String zipPath = strZipFile.substring(0, strZipFile.length()-4);
  59. File temp = new File(zipPath);
  60. temp.mkdir();
  61. System.out.println(zipPath + " created");
  62.  
  63. /*
  64. * STEP 2 : Extract entries while creating required
  65. * sub-directories
  66. *
  67. */
  68. ZipFile zipFile = new ZipFile(fSourceZip);
  69. Enumeration e = zipFile.entries();
  70.  
  71. while(e.hasMoreElements())
  72. {
  73. ZipEntry entry = (ZipEntry)e.nextElement();
  74. File destinationFilePath = new File(zipPath,entry.getName());
  75.  
  76. //create directories if required.
  77. destinationFilePath.getParentFile().mkdirs();
  78.  
  79. //if the entry is directory, leave it. Otherwise extract it.
  80. if(entry.isDirectory())
  81. {
  82. continue;
  83. }
  84. else
  85. {
  86. System.out.println("Extracting " + destinationFilePath);
  87.  
  88. /*
  89. * Get the InputStream for current entry
  90. * of the zip file using
  91. *
  92. * InputStream getInputStream(Entry entry) method.
  93. */
  94. BufferedInputStream bis = new BufferedInputStream(zipFile
  95. .getInputStream(entry));
  96.  
  97. int b;
  98. byte buffer[] = new byte[1024];
  99.  
  100. /*
  101. * read the current entry from the zip file, extract it
  102. * and write the extracted file.
  103. */
  104. FileOutputStream fos = new FileOutputStream(destinationFilePath);
  105. BufferedOutputStream bos = new BufferedOutputStream(fos,
  106. 1024);
  107.  
  108. while ((b = bis.read(buffer, 0, 1024)) != -1) {
  109. bos.write(buffer, 0, b);
  110. }
  111.  
  112. //flush the output stream and close it.
  113. bos.flush();
  114. bos.close();
  115.  
  116. //close the input stream.
  117. bis.close();
  118. }
  119. }
  120. }
  121. catch(IOException ioe)
  122. {
  123. System.out.println("IOError :" + ioe);
  124. }
  125.  
  126. }
  127. }
  128.  
  129. /*
  130. Sample usage of this prgram
  131.  
  132. java ExtractFileSubDirectories c:/sampleDoc.zip
  133.  
  134. Output of this program would be
  135. Zip file extracted!
  136.  
  137. This program will extract file from C:/sampleDoc.zip to
  138. c:/sampleDoc directory with all required sub-directories.
  139.  
  140. */

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!