DirectoryZipper.java
import java.io.*;

import java.text.*;

import java.util.*;
import java.util.zip.*;


/**
 * Basic application to copy/backup the contents of a specified directory
 * into a compressed zip file. The generated zip file will be named
 * in the following format:
 * <p>
 * {directory name}-dd-mm-yyyy.zip
 * <p>
 * Runtime parameters:
 * <ul>
 *   <li>Full path to the directory to be copied/backed up
 *   <li>Full path to the directory in which to write the zipped backup file
 * </ul>
 *
 * @author Michael Fitzmaurice, June 2004
 */
public class DirectoryZipper {
    public static void main(String[] args) throws IOException {
        File sourceDir = new File(args[0]);
        File targetDir = new File(args[1]);

        // build the name for the zip file to create
        String dirShortname = sourceDir.getName();

        Calendar today  = new GregorianCalendar();
        int day         = today.get(Calendar.DAY_OF_MONTH);
        int month       = today.get(Calendar.MONTH) + 1;
        int year        = today.get(Calendar.YEAR);

        // make sure day & month are always 2 digits
        NumberFormat format = new DecimalFormat("00");
        String dd           = format.format(day);
        String mm           = format.format(month);

        String zipFilename =
            dirShortname + "-" + dd + "-" + mm + "-" + year + ".zip";

        System.out.println("Backing up " + sourceDir + " to " + zipFilename
                            + "\n");

        List seedNames          = new ArrayList();
        String[] filesToArchive = listDirFiles(sourceDir, seedNames);

        File zipFile = new File(targetDir, zipFilename);
        makeZipFile(filesToArchive, zipFile);

        System.out.println("\nWrote " + filesToArchive.length + " files to " +
            zipFile);
    }

    /**
     * Helper method to build an array containing all the absolute file
     * names recursively extracted from a specified directory
     *
     * @param dir The directory to be listed
     * @param dirContents An object containing filenames extracted so far
     *
     * @return An array containing the absolute path to all files in
     *         <code>dir</code> and all it's subdirectories
     */
    private static String[] listDirFiles(File dir, List dirContents) {
        File[] dirFiles = dir.listFiles();

        // recursively collect all the filenames from this directory and
        // all sub-directories
        for (int i = 0; i < dirFiles.length; i++) {
            File file = dirFiles[i];

            if ( ! file.isDirectory() ) {
                dirContents.add( file.getAbsolutePath() );
            }
            else {
                // this is a directory - recurse to get its contents
                listDirFiles(file, dirContents);
            }
        }

        String[] files = new String[dirContents.size()];

        return (String[])dirContents.toArray(files);
    }

    /**
     * Helper method to write specified files into a compressed
     * zip file
     *
     * @param filesToZip An array containing the absolute paths to all
     *        the files to be written
     * @param zipFile The file into which <code>fileToZip</code> will be
     *        written
     *
     * @return The resultant compressed zip file
     *
     * @throws IOException If a read/write error occurs
     */
    private static File makeZipFile(String[] filesToZip, File zipFile)
        throws IOException {

        ZipOutputStream out = new ZipOutputStream( new FileOutputStream(zipFile) );

        // create a 1 KB buffer for reading the files
        byte[] buf = new byte[1024];

        try {
            // compress the files
            for (int i = 0; i < filesToZip.length; i++) {
                String file = filesToZip[i];
                System.out.println("Writing " + file);

                FileInputStream in = new FileInputStream(file);

                out.putNextEntry( new ZipEntry(file) );

                // transfer bytes from the file to the ZIP file
                int len;

                while ( (len = in.read(buf) ) > 0) {
                    out.write(buf, 0, len);
                }

                out.closeEntry();
                in.close();
            }
        }
        finally {
            out.close();
        }

        return zipFile;
    }
}


DirectoryZipper.java