package com.hisign.issms.util;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.CompressionLevel;
import net.lingala.zip4j.model.enums.CompressionMethod;
import net.lingala.zip4j.model.enums.EncryptionMethod;
import org.springframework.util.StringUtils;
import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class ZipUtils {
public static String compress(String destFileName, String passwd, File... files) {
InitZipObj initZipObj = new InitZipObj(destFileName, passwd).invoke();
ZipParameters parameters = initZipObj.getParameters();
ZipFile zipFile = initZipObj.getZipFile();
try {
for (File file : files) {
zipFile.addFile(file, parameters);
}
return destFileName;
} catch (ZipException e) {
e.printStackTrace();
}
return null;
}
public static String compress(String destFileName, String passwd, String... filePaths) {
int size = filePaths.length;
File[] files = new File[size];
for (int i = 0; i < size; i++) {
files[i] = new File(filePaths[i]);
}
return compress(destFileName, passwd, files);
}
public static void compressFolder(String destFileName, String passwd, String folder) {
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(CompressionMethod.DEFLATE);
parameters.setCompressionLevel(CompressionLevel.NORMAL);
ZipFile zipFile=null;
if (!StringUtils.isEmpty(passwd)) {
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);
zipFile = new ZipFile(destFileName,passwd.toCharArray());
}else{
zipFile = new ZipFile(destFileName);
}
try {
zipFile.addFolder(new File(folder),parameters);
} catch (ZipException e) {
e.printStackTrace();
}
}
public static void compressFolderWithOutList(String destFileName, String passwd, String folder) {
File folderParam = new File(folder);
if (folderParam.isDirectory()) {
File[] files = folderParam.listFiles();
compress(destFileName, passwd, files);
}
}
@SuppressWarnings("unchecked")
public static File[] deCompress(File zipFile, String dest, String passwd) throws ZipException {
File destDir = new File(dest);
if (destDir.isDirectory() && !destDir.exists()) {
destDir.mkdir();
}
ZipFile zFile = new ZipFile(zipFile);
zFile.setCharset(Charset.forName("UTF-8"));
if (!zFile.isValidZipFile()) {
throw new ZipException("压缩文件不合法,可能被损坏.");
}
if (zFile.isEncrypted()) {
zFile.setPassword(passwd.toCharArray());
}
zFile.extractAll(dest);
List<FileHeader> headerList = zFile.getFileHeaders();
List<File> extractedFileList = new ArrayList<File>();
for (FileHeader fileHeader : headerList) {
if (!fileHeader.isDirectory()) {
extractedFileList.add(new File(destDir, fileHeader.getFileName()));
}
}
File[] extractedFiles = new File[extractedFileList.size()];
extractedFileList.toArray(extractedFiles);
return extractedFiles;
}
public static File[] deCompress(File zipFile, String dest) {
try {
return deCompress(zipFile, dest, null);
} catch (ZipException e) {
e.printStackTrace();
}
return null;
}
public static File[] deCompress(String zipFilePath, String dest, String passwd) {
try {
return deCompress(new File(zipFilePath), dest, passwd);
} catch (ZipException e) {
e.printStackTrace();
}
return null;
}
public static File[] deCompress(String zipFilePath, String dest) {
try {
return deCompress(new File(zipFilePath), dest, null);
} catch (ZipException e) {
e.printStackTrace();
}
return null;
}
private static class InitZipObj {
private String destFileName;
private String passwd;
private ZipParameters parameters;
private ZipFile zipFile;
public InitZipObj(String destFileName, String passwd) {
this.destFileName = destFileName;
this.passwd = passwd;
}
public ZipParameters getParameters() {
return parameters;
}
public ZipFile getZipFile() {
return zipFile;
}
public InitZipObj invoke() {
parameters = new ZipParameters();
parameters.setCompressionMethod(CompressionMethod.DEFLATE);
parameters.setCompressionLevel(CompressionLevel.NORMAL);
zipFile = null;
if (!StringUtils.isEmpty(passwd)) {
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);
zipFile = new ZipFile(destFileName,passwd.toCharArray());
}else{
zipFile = new ZipFile(destFileName);
}
return this;
}
}
}