Extract First File From Zip File Example

  1. /*
  2. Extract First File From Zip File Example.
  3. This Java example shows how to extract one file from zip file
  4. using ZipInputStream class.
  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 ExtractFileFromZipFile {
  15.  
  16. public static void main(String args[])
  17. {
  18.  
  19. String sourceZipFile = "C:/FileIO/sourceFile.zip";
  20.  
  21. try
  22. {
  23. //create FileInputStream from the source zip file
  24. FileInputStream fin = new FileInputStream(sourceZipFile);
  25.  
  26. //create ZipInputStream from FileInputStream object
  27. ZipInputStream zin = new ZipInputStream(fin);
  28.  
  29. //get the first entry from the source zip file
  30. ZipEntry entry = zin.getNextEntry();
  31.  
  32. //crate OutputStream to extract the entry from zip file
  33. OutputStream os = new FileOutputStream("c:/extractedFile.css");
  34.  
  35.  
  36. byte[] buffer = new byte[1024];
  37. int length;
  38.  
  39. //read the entry from zip file and extract it to disk
  40. while( (length = zin.read(buffer)) > 0)
  41. {
  42. os.write(buffer, 0, length);
  43. }
  44.  
  45. //close the streams
  46. os.close();
  47.  
  48. //close the zip file
  49. zin.close();
  50.  
  51. System.out.println("File Extracted from zip file");
  52. }
  53. catch(IOException e)
  54. {
  55. System.out.println("IOException :" + e);
  56. }
  57.  
  58. }
  59.  
  60. }
  61.  
  62. /*
  63. Output of this program would be
  64. File Extracted from zip file
  65. */

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!