使用commons-compress 解压zip文件
依赖包
- org.apache.commons:commons-compress:1.11
- org.apache.commons:commons-lang3:3.4
只支持解压zip类型的压缩文件
示例代码
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class ExtractZip {
public static final int BUFFER_SIZE = 1024;
/**
* 解压 zip 文件
* @param zipFile zip 压缩文件
* @param destDir zip 压缩文件解压后保存的目录
* @return 返回 zip 压缩文件里的文件名的 list
* @throws Exception
*/
public static List<String> unZip(File zipFile, String destDir) throws Exception {
// 如果 destDir 为 null, 空字符串, 或者全是空格, 则解压到压缩文件所在目录
if(StringUtils.isBlank(destDir)) {
destDir = zipFile.getParent();
}
destDir = destDir.endsWith(File.separator) ? destDir : destDir + File.separator;
ZipArchiveInputStream is = null;
List<String> fileNames = new ArrayList<String>();
try {
is = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(zipFile), BUFFER_SIZE));
ZipArchiveEntry entry = null;
while ((entry = is.getNextZipEntry()) != null) {
fileNames.add(entry.getName());
if (entry.isDirectory()) {
File directory = new File(destDir, entry.getName());
directory.mkdirs();
} else {
OutputStream os = null;
try {
os = new BufferedOutputStream(new FileOutputStream(new File(destDir, entry.getName())), BUFFER_SIZE);
IOUtils.copy(is, os);
} finally {
IOUtils.closeQuietly(os);
}
}
}
} catch(Exception e) {
e.printStackTrace();
throw e;
} finally {
IOUtils.closeQuietly(is);
}
return fileNames;
}
/**
* 解压 zip 文件
* @param zipfile zip 压缩文件的路径
* @param destDir zip 压缩文件解压后保存的目录
* @return 返回 zip 压缩文件里的文件名的 list
* @throws Exception
*/
public static List<String> unZip(String zipfile, String destDir) throws Exception {
File zipFile = new File(zipfile);
return unZip(zipFile, destDir);
}
public static void main(String[] args) throws Exception {
List<String> names = unZip("/Users/Biao/Desktop/Archive.zip", "/Users/Biao/Desktop");
System.out.println(names);
}
}