Extract File From Zip File Using Command Line Arguments Example

  1. /*
  2. Extract File From Zip File Using Command Line Arguments Example.
  3. This Java example shows how to extract file from zip file using
  4. Java command line arguments.
  5. */
  6.  
  7. import java.io.FileInputStream;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.io.OutputStream;
  11. import java.util.zip.ZipEntry;
  12. import java.util.zip.ZipInputStream;
  13.  
  14. public class ExtractFileUsingCommandLine {
  15.  
  16. public static void main(String args[])
  17. {
  18.  
  19. /*
  20. * This program expects two command line arguments
  21. *
  22. * 1. Path of zip file
  23. * 2. Destination path for extracted file
  24. */
  25.  
  26. //fetch command line arguments
  27. String strZipFile = args[0];
  28. String strDestinationPath = args[1];
  29.  
  30. if(strZipFile == null || strZipFile.equals(""))
  31. {
  32. System.out.println("Invalid source file");
  33. System.exit(0);
  34. }
  35.  
  36. if(strDestinationPath == null || strDestinationPath.equals(""))
  37. {
  38. System.out.println("Invalid destination path");
  39. System.exit(0);
  40. }
  41.  
  42. try
  43. {
  44. //create FileInputStream from the source zip file
  45. FileInputStream fin = new FileInputStream(strZipFile);
  46.  
  47. //create ZipInputStream from FileInputStream object
  48. ZipInputStream zin = new ZipInputStream(fin);
  49.  
  50. //get the first entry from the source zip file
  51. ZipEntry entry = zin.getNextEntry();
  52.  
  53. //crate OutputStream to extract the entry from zip file
  54. OutputStream os = new FileOutputStream(strDestinationPath + "/" + entry.getName());
  55.  
  56.  
  57. byte[] buffer = new byte[1024];
  58. int length;
  59.  
  60. //read the entry from zip file and extract it to disk
  61. while( (length = zin.read(buffer)) > 0)
  62. {
  63. os.write(buffer, 0, length);
  64. }
  65.  
  66. //close the streams
  67. os.close();
  68.  
  69. //close the zip file
  70. zin.close();
  71.  
  72. System.out.println("File Extracted from zip file");
  73. }
  74. catch(IOException e)
  75. {
  76. System.out.println("IOException :" + e);
  77. }
  78.  
  79.  
  80. }
  81.  
  82. }
  83.  
  84. /*
  85. Sample usage of this prgram
  86.  
  87. java ExtractFileUsingCommandLine c:/sampleDoc.zip c:
  88.  
  89. Output of this program would be
  90. File Extracted from zip file
  91.  
  92. This program will extract file from C:/sampleDoc.zip to c:/
  93. */

Post new comment

To combat spam, please enter the code in the image.


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!