How do I zip a directory and all its contents?
Author: Deron Eriksson
Description: This Java tutorial describes how to zip all the contents of a directory.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)


Page:    1 2 >

Zipping all the contents of a directory is fairly involved but still straightforward, as presented in the ZipDirectory class. In this class, we specify a directory to zip, C:\projects\workspace\testing\stuff, which is a directory in the testing project that I created. We use the recursive getAllFiles() method to populate a List of File objects with all the subdirectories and files contained within the main directory we are zipping.

That done, we then call the writeZipFile() method, passing it the File object of the directory to zip, and the list of File objects that we created with getAllFiles(). The writeZipFile() method creates the zip file that we write to by appending ".zip" to the end of the main directory name. It then loops over the list of File objects, and if the object is not a directory (ie, it is a file), it calls addToZip(directoryToZip, file, zos) with that "file" object as the second parameter.

The addToZip() method reads the contents of the File object and writes that content to the zip file, with a ZipEntry specified for that content. The zipFilePath variable is used for the ZipEntry file path. We chop the front part of the file's canonical path off so that the ZipEntry file path is relative to the directory that we are zipping.

ZipDirectory is shown here:

ZipDirectory.java

package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipDirectory {

	public static void main(String[] args) throws IOException {
		File directoryToZip = new File("C:\\projects\\workspace\\testing\\stuff");

		List<File> fileList = new ArrayList<File>();
		System.out.println("---Getting references to all files in: " + directoryToZip.getCanonicalPath());
		getAllFiles(directoryToZip, fileList);
		System.out.println("---Creating zip file");
		writeZipFile(directoryToZip, fileList);
		System.out.println("---Done");
	}

	public static void getAllFiles(File dir, List<File> fileList) {
		try {
			File[] files = dir.listFiles();
			for (File file : files) {
				fileList.add(file);
				if (file.isDirectory()) {
					System.out.println("directory:" + file.getCanonicalPath());
					getAllFiles(file, fileList);
				} else {
					System.out.println("     file:" + file.getCanonicalPath());
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void writeZipFile(File directoryToZip, List<File> fileList) {

		try {
			FileOutputStream fos = new FileOutputStream(directoryToZip.getName() + ".zip");
			ZipOutputStream zos = new ZipOutputStream(fos);

			for (File file : fileList) {
				if (!file.isDirectory()) { // we only zip files, not directories
					addToZip(directoryToZip, file, zos);
				}
			}

			zos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void addToZip(File directoryToZip, File file, ZipOutputStream zos) throws FileNotFoundException,
			IOException {

		FileInputStream fis = new FileInputStream(file);

		// we want the zipEntry's path to be a relative path that is relative
		// to the directory being zipped, so chop off the rest of the path
		String zipFilePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1,
				file.getCanonicalPath().length());
		System.out.println("Writing '" + zipFilePath + "' to zip file");
		ZipEntry zipEntry = new ZipEntry(zipFilePath);
		zos.putNextEntry(zipEntry);

		byte[] bytes = new byte[1024];
		int length;
		while ((length = fis.read(bytes)) >= 0) {
			zos.write(bytes, 0, length);
		}

		zos.closeEntry();
		fis.close();
	}

}

(Continued on page 2)

Page:    1 2 >