在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

使用zip4j加密和解密文件和目录

2016-12-20 13:16| 发布者: zhangjf| 查看: 621| 评论: 0

摘要: 闲话少说,直接看工具类: 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.ZipPara ...

闲话少说,直接看工具类:

  1. package com.ilucky.zip4j.util;
  2. import java.io.File;
  3. import net.lingala.zip4j.core.ZipFile;
  4. import net.lingala.zip4j.exception.ZipException;
  5. import net.lingala.zip4j.model.ZipParameters;
  6. import net.lingala.zip4j.util.Zip4jConstants;
  7. /**
  8. * @author IluckySi
  9. * @since 20150723
  10. */
  11. public class Zip4jUtil {
  12. private String srcPath;
  13. private String dstPath;
  14. private String password = "123456";
  15. public String getSrcPath() {
  16. return srcPath;
  17. }
  18. public void setSrcPath(String srcPath) {
  19. this.srcPath = srcPath;
  20. }
  21. public String getDstPath() {
  22. return dstPath;
  23. }
  24. public void setDstPath(String dstPath) {
  25. this.dstPath = dstPath;
  26. }
  27. public String getPassword() {
  28. return password;
  29. }
  30. public void setPassword(String password) {
  31. this.password = password;
  32. }
  33. /**
  34. * 加密
  35. * 支持将某个文件或某个目录下所有的文件加密.
  36. * 1.某个文件:D:\test\src.zip.
  37. * 2某个目录:D:\test\src
  38. * @return boolean
  39. */
  40. public boolean encrypt() {
  41. try {
  42. if(!new File(srcPath).exists()) {
  43. System.out.println("源路径不存在 "+srcPath);
  44. return false;
  45. }
  46. ZipParameters parameters = new ZipParameters();
  47. parameters.setEncryptFiles(true);
  48. parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
  49. parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
  50. parameters.setPassword(password.toCharArray());
  51. File srcFile = new File(srcPath);
  52. ZipFile destFile = new ZipFile(dstPath);
  53. if(srcFile.isDirectory()) {
  54. destFile.addFolder(srcFile, parameters);
  55. } else {
  56. destFile.addFile(srcFile, parameters);
  57. }
  58. System.out.println("成功加密文件");
  59. return true;
  60. } catch (Exception e) {
  61. System.out.println("加密文件发生异常:"+e);
  62. return false;
  63. }
  64. }
  65. /**
  66. * 解密
  67. * 支持将某个加密文件解压缩到某个指定目录下面.
  68. * @return boolean
  69. */
  70. public boolean decrypt() {
  71. try {
  72. if(!new File(srcPath).exists()) {
  73. System.out.println("源路径不存在 "+srcPath);
  74. return false;
  75. }
  76. ZipFile srcFile = new ZipFile(srcPath);
  77. srcFile.setFileNameCharset("GBK");
  78. srcFile.setPassword(password.toCharArray());
  79. srcFile.extractAll(dstPath);
  80. System.out.println("成功解密文件");
  81. return true;
  82. } catch (ZipException e) {
  83. System.out.println("解密文件发生异常:"+e);
  84. return false;
  85. }
  86. }
  87. }
复制代码

然后看测试类:
  1. package com.ilucky.zip4j.util;
  2. /**
  3. * @author IluckySi
  4. * @since 20150723
  5. */
  6. public class MainTest {
  7. public static void main(String[] args) {
  8. //加密.
  9. Zip4jUtil zip4jUtil = new Zip4jUtil();
  10. zip4jUtil.setSrcPath("D:\test\src.zip");
  11. zip4jUtil.setDstPath("D:\test\dst.zip");
  12. zip4jUtil.setPassword("123");
  13. zip4jUtil.encrypt();
  14. //解密.
  15. zip4jUtil.setSrcPath("D:\test\dst.zip");
  16. zip4jUtil.setDstPath("D:\test\");
  17. zip4jUtil.setPassword("123");
  18. //zip4jUtil.decrypt();
  19. }
  20. }
复制代码

最后看pom文件:
  1. <dependency>
  2. <groupId>net.lingala.zip4j</groupId>
  3. <artifactId>zip4j</artifactId>
  4. <version>1.3.2</version>
  5. </dependency>
复制代码

来自:http://blog.csdn.net/sidongxue2/article/details/47026909

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-8-23 04:27

Copyright 2015-2025 djqfx

返回顶部