Zip and UnZip in Java

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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();
		}
	}
}
}
Licensed under CC BY-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy