//--------------------------------------------------------------------------- // Copyright © 2009-2009 Eamonn Duffy. All Rights Reserved. //--------------------------------------------------------------------------- // // $RCSfile: $ // // $Revision: $ // // Created: Eamonn A. Duffy, 9-Nov-2009. // // Purpose: Simple UnZip helper. // // Credits: As of 9-Nov-2009, some incomplete UnZip code courtesy of: http://www.devx.com/getHelpOn/10MinuteSolution/20447 // It did not handle Directories very well, at least not on a Zip file created by Windows Vista Explorer, so I // added and tested some more thorough Directory support. // //--------------------------------------------------------------------------- package simpleunzip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.zip.*; public class SimpleUnZip { public static final void copyInputStream(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) >= 0) out.write(buffer, 0, len); in.close(); out.close(); } public static void main(String[] args) { System.out.printf("Simple UnZip V1.0.1 : Eamonn Duffy, 9-Nov-2009.\n\n"); if (args.length <= 0) { System.out.println("Please specify at least one File to UnZip."); } else { boolean bAddNewLine = false; for (String Arg : args) { if (bAddNewLine) System.out.println(); else bAddNewLine = true; System.out.printf("Attempting to UnZip: %s\n", Arg); try { ZipFile ZipFile = new ZipFile(Arg); try { Enumeration Entries = ZipFile.entries(); while (Entries.hasMoreElements()) { ZipEntry Entry = (ZipEntry)Entries.nextElement(); if (Entry.isDirectory()) { File DirectoryPath = new File(Entry.getName()); if (! DirectoryPath.exists()) DirectoryPath.mkdirs(); } else { File File = new File(Entry.getName()); String FilePath = File.getPath(); String FileName = File.getName(); if (FilePath.length() > 0) FilePath = FilePath.substring(0, FilePath.length() - FileName.length()); if (FilePath.length() > 0) { File DirectoryPath = new File(FilePath); if (! DirectoryPath.exists()) DirectoryPath.mkdirs(); } if (FileName.length() > 0) { System.out.println("File: " + Entry.getName()); copyInputStream(ZipFile.getInputStream(Entry), new BufferedOutputStream(new FileOutputStream(Entry.getName()))); } } } } catch (Exception Exception) { System.err.printf("Exception = %s\n", Exception.getMessage()); } ZipFile.close(); } catch (Exception Exception) { System.err.printf("Zip File Exception = %s\n", Exception.getMessage()); } } } } } //--------------------------------------------------------------------------- // End Of $RCSfile: $ //---------------------------------------------------------------------------