Java ZIP文件解压与压缩工具类
import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
/**
* 解压文件
*
* @param zipFile 指定解压Zip文件
* @param descDir 指定解压目录
*/
public static void unZip(File zipFile, File descDir) {
if (!descDir.exists()) {
descDir.mkdirs();
}
ZipFile zip = null;
try {
zip = new ZipFile(zipFile);
Enumeration<?> entries = zip.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
// 如果是文件夹,就创建个文件夹
if (entry.isDirectory()) {
File dir = new File(descDir, entry.getName());
dir.mkdirs();
} else {
// 如果是文件,就先创建一个文件,然后用io流把内容copy过去
File targetFile = new File(descDir + "/" + entry.getName());
// 保证这个文件的父文件夹必须要存在
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
}
targetFile.createNewFile();
// 将压缩文件内容写入到这个文件中
InputStream is = zip.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(targetFile);
int len;
byte[] buf = new byte[1024];
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
// 关流顺序,先打开的后关闭
fos.close();
is.close();
}
}
System.out.println("解压文件" + zipFile.getAbsolutePath() + "成功");
} catch (Exception e) {
throw new RuntimeException("unzip error from ZipUtil", e);
} finally {
if (zip != null) {
try {
zip.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 压缩文件或文件夹
*
* @param src 指定待压缩目录或文件
* @param zip 指定压缩Zip文件
*/
public static void toZip(File src, File zip) {
ZipOutputStream zipOutputStream = null;
try {
if (!src.exists()) {
System.err.println("压缩文件或目录不存在");
return;
}
if (!zip.exists()) {
zip.createNewFile();
}
zipOutputStream = new ZipOutputStream(new FileOutputStream(zip));
compress(src, zipOutputStream, null);
zipOutputStream.close();
System.out.println("压缩文件" + zip.getAbsolutePath() + "成功");
} catch (IOException e) {
System.err.println("压缩失败");
e.printStackTrace();
} finally {
if (zipOutputStream != null) {
try {
zipOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* @param sourceFile 待压缩文件或文件夹
* @param zos Zip输出流
* @param parentPath 父目录结构
* @throws IOException
*/
private static void compress(File sourceFile, ZipOutputStream zos, String parentPath) throws IOException {
byte[] buff = new byte[1024];
if (sourceFile.isDirectory()) {
//确保空文件夹也可以压缩进去
zos.putNextEntry(new ZipEntry((parentPath != null ? parentPath : "") + sourceFile.getName() + "/"));
zos.closeEntry();
//循环压缩子文件或文件夹
for (File file : sourceFile.listFiles()) {
compress(file, zos, (parentPath != null ? parentPath : "") + sourceFile.getName() + "/");
}
} else {
//压缩文件
zos.putNextEntry(new ZipEntry((parentPath != null ? parentPath : "") + sourceFile.getName()));
int len;
FileInputStream in = new FileInputStream(sourceFile);
while ((len = in.read(buff)) != -1) {
zos.write(buff, 0, len);
}
zos.closeEntry();
in.close();
}
}
public static void main(String[] args) {
//解压
unZip(new File("E:\\AAA.zip"), new File("E:\\AAA"));
//压缩
toZip(new File("E:\\AAA"), new File("E:\\AAA.zip"));
}
}