import java.util.zip.*; import java.io.*; // File: jkzip.java // /** * * This class will allow you to perform basic pkzip compatible * data compression.
*
* The basic steps are:
* 1. Create the zip output stream
* 2. Open source data file
* 3. Create the zip entry
* 4. Put the entry
* 5. Read source and write the data to the zip output stream
* 6. Close the zip entry and other open streams
*
*
* Command syntax
*
Usage: java jkzip zipfile [files]
* NOTE: This version does not recurse sub-directories.
*
* @author Chad (shod) Darby, darby@j-nine.com
* @version 1.13, 27 Sep 97
*
*
*/
public class jkzip
{
//-------------------------------------------------
// DATA MEMBERS
//
String archiveFileName;
String fileNamesArray[];
//-------------------------------------------------
// CONSTRUCTORS
//
/**
*
* The constructor is used to create a new jkzip object based
* on the command line arguments.
* Usage:
java jkzip zipfile [files]
* The basic steps are:
* 1. Create the zip output stream
* 2. Open source data file
* 3. Create the zip entry
* 4. Put the entry
* 5. Read source and write the data to the zip output stream
* 6. Close the zip entry and other open streams
*/
public void zipIt()
{
BufferedInputStream sourceStream;
File theFile;
FileInputStream fis;
ZipOutputStream targetStream;
FileOutputStream fos;
ZipEntry theEntry;
final int DATA_BLOCK_SIZE = 2048;
int byteCount;
byte data[];
try
{
// 1. create the ZipOutputStream
//
System.out.println("Creating ZIP: " + archiveFileName);
fos = new FileOutputStream(archiveFileName);
targetStream = new ZipOutputStream(fos);
targetStream.setMethod(ZipOutputStream.DEFLATED);
// loop thru the array
for (int i=0; i < fileNamesArray.length; i++)
{
theFile = new File(fileNamesArray[i]);
// check if file is a directory, if so then skip
if (theFile.isDirectory())
{
// i won't recurse directories so let's skip
System.out.println("\tSkipping directory: " + theFile);
continue;
}
// 2. open source file
//
fis = new FileInputStream(fileNamesArray[i]);
sourceStream = new BufferedInputStream(fis);
// 3. create the ZipEntry
//
theEntry = new ZipEntry(fileNamesArray[i]);
System.out.print("\tAdding: " + theEntry.getName());
// 4. put the entry
//
targetStream.putNextEntry(theEntry);
// 5. read source data and write target data
// to compressed output stream
//
data = new byte[DATA_BLOCK_SIZE];
while ( (byteCount = sourceStream.read(data, 0, DATA_BLOCK_SIZE)) != -1)
{
targetStream.write(data, 0, byteCount);
}
targetStream.flush();
// 6. close the entry
//
System.out.println(", done.");
targetStream.closeEntry();
sourceStream.close();
} // end for loop
targetStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
*
* Returns the usage string for this class.
*
* @return Returns the usage string.
*
*/
public String getUsageString()
{
String temp = null;
temp = "\nJKZIP ver 1.13\n\n";
temp += "Usage: java jkzip zipfile [files]\n";
return temp;
}
public static void main(String args[])
{
jkzip myApp = new jkzip(args);
myApp.zipIt();
}
}