清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
闲话少说,直接看工具类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | package com.ilucky.zip4j.util; import java.io.File; import net.lingala.zip4j.core.ZipFile; import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.util.Zip4jConstants; /** * @author IluckySi * @since 20150723 */ public class Zip4jUtil { private String srcPath; private String dstPath; private String password = "123456" ; public String getSrcPath() { return srcPath; } public void setSrcPath(String srcPath) { this .srcPath = srcPath; } public String getDstPath() { return dstPath; } public void setDstPath(String dstPath) { this .dstPath = dstPath; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } /** * 加密 * 支持将某个文件或某个目录下所有的文件加密. * 1.某个文件:D:\\test\\src.zip. * 2某个目录:D:\\test\\src * @return boolean */ public boolean encrypt() { try { if (! new File(srcPath).exists()) { System.out.println( "源路径不存在 " +srcPath); return false ; } ZipParameters parameters = new ZipParameters(); parameters.setEncryptFiles( true ); parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES); parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256); parameters.setPassword(password.toCharArray()); File srcFile = new File(srcPath); ZipFile destFile = new ZipFile(dstPath); if (srcFile.isDirectory()) { destFile.addFolder(srcFile, parameters); } else { destFile.addFile(srcFile, parameters); } System.out.println( "成功加密文件" ); return true ; } catch (Exception e) { System.out.println( "加密文件发生异常:" +e); return false ; } } /** * 解密 * 支持将某个加密文件解压缩到某个指定目录下面. * @return boolean */ public boolean decrypt() { try { if (! new File(srcPath).exists()) { System.out.println( "源路径不存在 " +srcPath); return false ; } ZipFile srcFile = new ZipFile(srcPath); srcFile.setFileNameCharset( "GBK" ); srcFile.setPassword(password.toCharArray()); srcFile.extractAll(dstPath); System.out.println( "成功解密文件" ); return true ; } catch (ZipException e) { System.out.println( "解密文件发生异常:" +e); return false ; } } } |
然后看测试类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.ilucky.zip4j.util; /** * @author IluckySi * @since 20150723 */ public class MainTest { public static void main(String[] args) { //加密. Zip4jUtil zip4jUtil = new Zip4jUtil(); zip4jUtil.setSrcPath( "D:\\test\\src.zip" ); zip4jUtil.setDstPath( "D:\\test\\dst.zip" ); zip4jUtil.setPassword( "123" ); zip4jUtil.encrypt(); //解密. zip4jUtil.setSrcPath( "D:\\test\\dst.zip" ); zip4jUtil.setDstPath( "D:\\test\\" ); zip4jUtil.setPassword( "123" ); //zip4jUtil.decrypt(); } } |
最后看pom文件:
1 2 3 4 5 | < dependency > < groupId >net.lingala.zip4j</ groupId > < artifactId >zip4j</ artifactId > < version >1.3.2</ version > </ dependency > |
来自:http://blog.csdn.net/sidongxue2/article/details/47026909