1.引入依赖
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
2.加密代码
2.1判空
public static boolean isEmpty(Object ob) {
if (ob == null) {
return true;
} else {
if (ob instanceof String) {
if (((String)ob).length() == 0) {
return true;
}
} else if (ob instanceof Collection) {
if (((Collection)ob).size() == 0) {
return true;
}
} else if (ob instanceof Map) {
if (((Map)ob).size() == 0) {
return true;
}
}
return false;
}
}
2.2创建压缩文件存放目录,比如指定的存放路径并没有被创建
private static void createDestDirectory(String destParam){
File destDir=null;
if(destParam.endsWith(File.separator)){
destDir=new File(destParam);
}else {
destDir=new File(destParam.substring(0,destParam.lastIndexOf(File.separator)));
}
if(!destDir.exists()){
destDir.mkdirs();
}
}
2.3构建压缩文件存放路径,如果不存在将会创建
private static String createZipFilePath(File srcFile, String destParam){
if(isEmpty(destParam)){
if(srcFile.isDirectory()){
destParam=srcFile.getParent()+File.separator+srcFile.getName()+".zip";
}else {
String fileName=srcFile.getName().substring(0,srcFile.getName().lastIndexOf('.'));
destParam=srcFile.getParent()+File.separator+fileName+".zip";
}
}else {
createDestDirectory(destParam);
if(destParam.endsWith(File.separator)){
String fileName="";
if (srcFile.isDirectory()) {
fileName=srcFile.getName();
}else {
fileName=srcFile.getName().substring(0,srcFile.getName().lastIndexOf('.'));
}
destParam +=fileName+".zip";
}
}
return destParam;
}
2.4使用给定密码压缩指定文件或文件夹到指定位置
public static String zip(String src,String dest,boolean isCreatDir,String password) throws Exception{
File srcFile = new File(src);
dest=createZipFilePath(srcFile,null);
ZipParameters zp = new ZipParameters();
//压缩方式
zp.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
//压缩级别
zp.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
//加密
if(!isEmpty(password)){
zp.setEncryptFiles(true);
//加密方式
zp.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
zp.setPassword(password.toCharArray());
}
ZipFile zf = new ZipFile(dest);
if(srcFile.isDirectory()){
if(!isCreatDir){
File[] subFiles = srcFile.listFiles();
if(subFiles.length>0 && subFiles !=null){
for(File file:subFiles){
if(file.isDirectory()){
zf.addFolder(file,zp);
}else {
zf.addFile(file, zp);
}
}
}
return dest;
}
zf.addFolder(srcFile, zp);
}else {
zf.addFile(srcFile, zp);
}
return "dest";
}
3.解密
public static void unZipByPw(File src,String dest,String password) throws Exception{
if(!src.exists()){
throw new ZipException("文件不存在");
}
ZipFile zipFile = new ZipFile(src);
if (!zipFile.isValidZipFile()) {
throw new ZipException("文件不合法或不存在");
}
zipFile.setFileNameCharset(DEFAULT_ENCODING);
//检查是否需要密码
if (zipFile.isEncrypted()) {
zipFile.setPassword(password);
}
List<FileHeader> fileHeaders = zipFile.getFileHeaders();
BufferedWriter bw=null;
BufferedReader br=null;
try{
for(int i=0,size=fileHeaders.size();i<size;i++){
FileHeader fd = fileHeaders.get(i);
br = new BufferedReader(new InputStreamReader(zipFile.getInputStream(fd)));
bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest)));
String line;
while((line=br.readLine() )!=null){
bw.write(line);
bw.write("\n");
}
bw.flush();
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{if(bw !=null){ bw.close();}}catch (Exception e){e.printStackTrace();}
try{if(br !=null){ br.close();}}catch (Exception e){e.printStackTrace();}
}
}
4.测试
public static void main(String[] args) {
/*try {
ZipUtil.zip("C:\\Users\\lyj\\Desktop\\cs.cvs","1234");
} catch (Exception e) {
e.printStackTrace();
}*/
File src = new File("C:\\Users\\lyj\\Desktop\\cs.zip");
String dest="C:\\Users\\lyj\\Desktop\\cs.xls";
try {
ZipUtil.unZipByPw(src,dest,"1234");
} catch (Exception e) {
e.printStackTrace();
}
}