Zip and UnZip in Java

3 minute read

package com.ots.pocketpolice.sync;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
 * A convenience class that compresses and decompresses files to and from Zip
 * archives.</br>Updated: 2012-01-19 09:54
 * @author CManios
 * @version 1.0 
 */
public class Zipper {
// links that helped a lot
// http://www.crazysquirrel.com/computing/java/basics/java-directory-zipping.jspx
// http://stackoverflow.com/questions/1399126/java-util-zip-recreating-directory-structure

private static final int BUFFER_SIZE = 2048;

/**
 * Compresses a file or a directory to a Zip archive. The method is
 * recursive and includes all subdirectories
 * 
 * @param source
 *            a file or a directory
 * @param zip
 *            the Zip archive to create
 * @throws IOException
 */
public static void zip(File source, File zip) throws IOException {
	ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip));
	zipAll(source, source, zos);
	zos.close();
}

private static void zipAll(File sourceFile, File base, ZipOutputStream zos)
		throws IOException {
	byte[] buffer = new byte[BUFFER_SIZE];
	FileInputStream in;
	int read = 0;

	if (!sourceFile.isDirectory()) {
		in = new FileInputStream(sourceFile);

		ZipEntry entry = getZipEntry(sourceFile, base);

		zos.putNextEntry(entry);
		while ((read = in.read(buffer)) != -1) {
			zos.write(buffer, 0, read);
		}
	} else {
		File[] files = sourceFile.listFiles();

		for (int i = 0; i < files.length; i++) {
			if (files[i].isDirectory()) {

				zipAll(files[i], base, zos);
			} else {
				in = new FileInputStream(files[i]);

				ZipEntry entry = getZipEntry(files[i], base);

				zos.putNextEntry(entry);
				while ((read = in.read(buffer)) != -1) {
					zos.write(buffer, 0, read);
				}
				in.close();
			}
		}
	}
}

private static ZipEntry getZipEntry(File fileos, File base) {
	if (fileos.equals(base)) {
		return new ZipEntry(fileos.getName());
	}

	return new ZipEntry(fileos.getAbsolutePath().substring(
			base.getAbsolutePath().length() + 1));

}

/**
 * Compresses an array of files or directories to a Zip archive. The method
 * is recursive and includes all subdirectories
 * 
 * @param zip
 *            the Zip archive to create
 * @param sourceFiles
 *            an array of files or directories
 * @throws IOException
 */
public static void zip(File sourceFiles[], File zip) throws IOException {
	ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip));
	zipAll(sourceFiles, null, zos);

	zos.close();
}

private static void zipAll(File sourceFile[], File base, ZipOutputStream zos)
		throws IOException {

	for (int i = 0; i < sourceFile.length; i++) {

		if (sourceFile[i].isDirectory()) {

			zipAll(sourceFile[i], sourceFile[i].getAbsoluteFile()
					.getParentFile(), zos);

			continue;
		}
		zipAll(sourceFile[i], sourceFile[i], zos);

	}
}

/**
 * Extracts the contents of a Zip archive to a specific directory. If the
 * directory does not exist, it is created
 * 
 * @param zip
 *            the Zip archive
 * @param extractTo
 *            directory path
 * @throws IOException
 */
public static final void unzip(File zip, File extractTo) throws IOException {
	ZipFile archive = new ZipFile(zip);
	Enumeration<? extends ZipEntry> e = archive.entries();
	
	while (e.hasMoreElements()) {
		ZipEntry entry = (ZipEntry) e.nextElement();
		File file = new File(extractTo, entry.getName());
		if (entry.isDirectory() && !file.exists()) {
			file.mkdirs();
		} else {
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}

			InputStream in = archive.getInputStream(entry);
			BufferedOutputStream out = new BufferedOutputStream(
					new FileOutputStream(file));

			byte[] buffer = new byte[BUFFER_SIZE];
			int read;

			while (-1 != (read = in.read(buffer))) {
				out.write(buffer, 0, read);
			}

			in.close();
			out.close();
		}
	}
}
}

Updated:

Comments