public class DeCompressUtils {
/**
* 压缩zip文件
*
* @param zipFilePath
* 压缩后zip包路径
* @param lstFilesPath
* 所要压缩的文件list
* @throws Exception
*/
public static void zip(String zipFilePath, List<String> lstFilesPath) throws Exception {
// 判断是否为zip文件
if (zipFilePath.length() <= 4 || !zipFilePath.toLowerCase().endsWith(".zip")) {
Exception ex = new Exception("zip文件格式错误!");
throw ex;
}
// 压缩处理
ZipFile zf = null;
try {
zf = new ZipFile(zipFilePath);
ArrayList<File> lstFiles = new ArrayList<File>();
for (String strFilePath : lstFilesPath) {
lstFiles.add(new File(strFilePath));
}
ZipParameters parameters = new ZipParameters();
// 默认,对应好压的标准
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
// 压缩级别
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
zf.addFiles(lstFiles, parameters);
} catch (ZipException e) {
e.printStackTrace();
Exception ex = new Exception("文件压缩zip时失败!");
throw ex;
}
}
/**
* 解压zip文件
*
* @param zipFilePath
* zip文件路径
* @param folderPath
* 解压后文件路径
* @throws Exception
*/
public static void unZip(String zipFilePath, String folderPath) throws Exception {
// 判断是否为zip文件 注意:folderPath传值为dealPath + File.separator
if (zipFilePath.length() <= 4 || !zipFilePath.toLowerCase().endsWith(".zip")) {
Exception ex = new Exception(zipFilePath + ":zip文件格式错误!");
throw ex;
}
// 解压处理
ZipFile zf = null;
try {
zf = new ZipFile(zipFilePath);
zf.extractAll(folderPath);
} catch (ZipException e) {
e.printStackTrace();
Exception ex = new Exception(zipFilePath + ":zip文件解压时失败!");
throw ex;
}
}