import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ZipUtil {
public static void unzip(String zipFilePath, String unzipDirectory)
throws Exception {
// 定义输入输出流对象
InputStream input = null;
OutputStream output = null;
try {
// 创建文件对象
File file = new File(zipFilePath);
// 创建zip文件对象
ZipFile zipFile = new ZipFile(file);
// 创建本zip文件解压目录
String name = file.getName().substring(0, file.getName().lastIndexOf("."));
File unzipFile = new File(unzipDirectory + "/" + name);
if (unzipFile.exists())
unzipFile.delete();
unzipFile.mkdir();
// 得到zip文件条目枚举对象
@SuppressWarnings("rawtypes")
Enumeration zipEnum = zipFile.entries();
// 定义对象
ZipEntry entry = null;
String entryName = null, path = null;
String names[] = null;
int length;
// 循环读取条目
while (zipEnum.hasMoreElements()) {
// 得到当前条目
entry = (ZipEntry) zipEnum.nextElement();
entryName = new String(entry.getName());
// 用/分隔条目名称
names = entryName.split("\\/");
length = names.length;
path = unzipFile.getAbsolutePath();
for (int v = 0; v < length; v++) {
if (v < length - 1) { // 最后一个目录之前的目录
File f = new File(path += "/" + names[v] + "/");
if (f.exists())
f.delete();
f.mkdir();
} else { // 最后一个
if (entryName.endsWith("/")) { // 为目录,则创建文件夹
File f = new File(unzipFile.getAbsolutePath() + "/" + entryName);
if (f.exists())
f.delete();
f.mkdir();
} else { // 为文件,则输出到文件
input = zipFile.getInputStream(entry);
output = new FileOutputStream(new File(unzipFile.getAbsolutePath() + "/" + entryName));
byte[] buffer = new byte[1024 * 8];
int readLen = 0;
while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1)
output.write(buffer, 0, readLen);
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// 关闭流
if (input != null)
input.close();
if (output != null) {
output.flush();
output.close();
}
}
}
public static void main(String[] args) throws Exception {
unzip("d:/jquery-easyui-1.2.2.zip", "e:/unzip");
}
}