import java.util.zip.*; import java.io.*; import java.util.*; // File: jkunzip.java // /** * * This class will allow you to perform basic pkzip compatible * data uncompression.

* *

* The basic steps are:
* 1. get the zipped input stream
* 2. get the zipped entries
* 3. prepare the uncompressed output stream
* 4. read source zipped data and write to uncompressed stream
* 5. close the source and target stream
* *

* Command syntax
*

 *  Usage: java jkunzip [-v] zipfile 
* where option includes:
* -v List zip file contents
*
* *

* NOTE: This version expands entries w/ directory structures. * * @author Chad (shod) Darby, darby@j-nine.com * @version 3.13, 16 Sep 1999 * */ public class jkunzip { //------------------------------------------------- // DATA MEMBERS // protected String zipFileName; protected boolean showListingFlag = false; protected final int DATA_BLOCK_SIZE = 2048; //------------------------------------------------- // CONSTRUCTORS // /** * * The constructor is used to create a new jkunzip object based * on the command line arguments.
* *

* Command syntax
*

   *    Usage: java jkunzip [-v] zipfile 
* where option includes:
* -v List zip file contents
*
* * @param args a string array of command line arguments. * */ public jkunzip(String args[]) { parseCommandLineArgs(args); if (zipFileName == null) { System.out.println(getUsageString()); System.exit(1); } } //------------------------------------------------- // METHODS // /** * Parses the command line arguments.
* * @param args command line args as an array of strings. * */ protected void parseCommandLineArgs(String args[]) { String name; int length = args.length; // check if empty arguments if (length == 0) { System.out.println(getUsageString()); System.exit(1); } if (length == 1) { if ( ! (args[0].equals("-v")) ) { zipFileName = args[0]; return; } else { System.out.println(getUsageString()); System.exit(1); } } else { zipFileName = args[1]; showListingFlag = true; } } /** * * Returns the usage string for this class. * * @return Returns the usage string. * */ protected String getUsageString() { String temp = null; temp = "\nJKUNZIP ver 1.13\n\n"; temp += "Usage: java jkunzip [-v] zipfile\n\n"; temp += "where option includes:\n"; temp += "-v\tList zip file contents\n"; return temp; } /** * * Lists the contents of the zipped file.
* * @param zipFileName name of the zipped file. * */ protected void listContents(String zipFileName) { ZipFile zf; ZipEntry theEntry; try { zf = new ZipFile(zipFileName); Enumeration entries = zf.entries(); // display the listing header System.out.println("Length\t\tSize\t\tName"); System.out.println("------\t\t----\t\t----"); // list the contents of each zipped entry while (entries.hasMoreElements()) { theEntry = (ZipEntry) entries.nextElement(); System.out.print(theEntry.getSize() + "\t\t"); System.out.print(theEntry.getCompressedSize() + "\t\t"); System.out.println(theEntry.getName()); } } catch (IOException exc) { exc.printStackTrace(); } } /** * * Performs the different switches for the jkunzip object.
* * Determines if it should list contents or extract files.
* */ public void unzipIt() { System.out.println("Searching ZIP: " + zipFileName + "\n"); if (showListingFlag) { listContents(zipFileName); } else { extractFiles(zipFileName); } } /** * * Extracts the files contained in the zipped file.
* *

* The basic steps are:
* 1. get the zipped input stream
* 2. get the zipped entries
* 3. prepare the uncompressed output stream
* 4. read source zipped data and write to uncompressed stream
* 5. close the source and target stream
* * @param theZipFleName name of the zipped archive * */ protected void extractFiles(String theZipFileName) { FileInputStream fis = null; ZipInputStream sourceZipStream; FileOutputStream fos = null; BufferedOutputStream targetStream; ZipEntry theEntry = null; String entryName = null; try { // 1. get the zipped input stream fis = new FileInputStream(theZipFileName); sourceZipStream = new ZipInputStream(fis); // 2. get the zipped entries while ( (theEntry = sourceZipStream.getNextEntry()) != null ) { entryName = theEntry.getName(); // 3. prepare the uncompressed output stream try { fos = new FileOutputStream(entryName); } catch (FileNotFoundException exc) { // the directory is not created...so let's build it! buildDirectory(entryName); fos = new FileOutputStream(entryName); } targetStream = new BufferedOutputStream(fos, DATA_BLOCK_SIZE); System.out.println("\tUnzipping: " + theEntry); int byteCount; byte data[] = new byte[DATA_BLOCK_SIZE]; // 4. read source zipped data and write to uncompressed stream while ( (byteCount = sourceZipStream.read(data, 0, DATA_BLOCK_SIZE)) != -1) { targetStream.write(data, 0, byteCount); } // 5. close the target stream targetStream.flush(); targetStream.close(); } // close the source stream sourceZipStream.close(); } catch (IOException exc) { exc.printStackTrace(); } } /** * Creates the directory structure */ protected void buildDirectory(String entryName) throws IOException { StringTokenizer st = new StringTokenizer(entryName, "/"); int levels = st.countTokens() - 1; StringBuffer directory = new StringBuffer(); File newDir; for (int i=0; i < levels; i++) { directory.append(st.nextToken() + "/"); } newDir = new File(directory.toString()); newDir.mkdirs(); } /** * The main driver routine! */ public static void main(String args[]) { jkunzip myApp = new jkunzip(args); myApp.unzipIt(); } }