I wanted to zip a data file with a vbs wsh file. However, there isn't a good way to do it without purchasing an overpowered zip program or popping a command window. So I wrote a little java utility that zips files. I'm dedicating it into the public domain, and suggestions are welcome. Please ignore any /. typos in the code below, and please give credit if used in your program (although that is not a requirement).
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.io.FileInputStream;
import java.io.File;
import java.io.IOException;
/**
* Facade around Java's zip classes to make it easier to create zip files.
* Based on
* <a href="http://javaalmanac.com/egs/java.util.zip/CreateZip.html">an
* example</a> from the Java Developer's Almanac 1.4.
* @author James Francis Cerra
* @version 0.2
* @see java.util.zip
*/
public class Zip {
/**
* Writes the file arguments to a zip file and then the standard output.
* <h2>Usage:</h2>
* <p class="note">From a command line:</p>
* <dl>
* <dt>To zip <code>filename</code> to <code>out.zip</code></dt>
* <dd><code>java Zip filename > out.zip</code></dd>
* <dt>To zip directory <code>dirname</code> to <code>out.zip</code></dt>
* <dd><code>java Zip dirname > out.zip</code></dd>
* <dt>To zip file <code>f1</code>, <code>f2</code>, and <code>f3</code>
* to <code>out.zip</code></dt>
* <dd><code>java Zip f1 f2 f3 > out.zip</code></dd>
* <dt>To zip file <code>f1</code>, <code>f2</code>, and <code>f3</code>
* and the directories <code>d1</code>, <code>d2</code>, and
* <code>d3</code> to <code>out.zip</code></dt>
* <dd><code>java Zip f1 f2 f3 d1 d2 d3 > out.zip</code></dd>
* </dl>
* <p class="note">Remember: on some systems you may need to put quotes
* around long file or directory paths. Relative paths
* should also work too.</p>
*
* @param args array of file or directory name strings to zip.
*/
public static void main(String[] args) throws IOException {
ZipOutputStream out = new ZipOutputStream(System.out);
writeFiles(out, args);
out.close();
}
/** Writes files built from an array of filenames to a zip output stream. */
public static void writeFiles(ZipOutputStream out, String[] filenames)
throws IOException {
int numberOfFiles = filenames.length;
for(int ii=0; ii < numberOfFiles; ii++) {
writeFile(out, new File(filenames[ii]));
}
}
/** Writes an array of File arguments to a zip output stream. */
public static void writeFiles(ZipOutputStream out, File[] files)
throws IOException {
int numberOfFiles = files.length;
for(int ii=0; ii < numberOfFiles; ii++) {
writeFile(out, files[ii]);
}
}
/** Writes a single File to a zip output stream. */
public static void writeFile(ZipOutputStream out, File file)
throws IOException {
if(file.isDirectory()) writeFiles(out, file.listFiles());
else if(file.canRead()) writeReadableFile(out, file);
else throw new IOException(
"Can't write to zip since" +
file.toString() +
"is not a directory nor readable."
);
}
/** Writes a single readable File to a zip output stream. */
private static void writeReadableFile(ZipOutputStream out, File file)
throws IOException {
FileInputStream in = new FileInputStream(file);
out.putNextEntry(new ZipEntry(file.getPath()));
int len;
byte[] buf = new byte[1024];
while ((len = in.read(buf)) > 0) out.write(buf, 0, len);
out.closeEntry();
in.close();
}
}