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* * @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.
* where option includes:
* -v List zip file contents
*
* 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();
}
}