Zip4jUtil.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package com.qmth.teachcloud.common.util;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import com.qmth.teachcloud.common.contant.SystemConstant;
  4. import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
  5. import net.lingala.zip4j.core.ZipFile;
  6. import net.lingala.zip4j.exception.ZipException;
  7. import net.lingala.zip4j.model.ZipParameters;
  8. import net.lingala.zip4j.util.Zip4jConstants;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.util.CollectionUtils;
  12. import java.io.File;
  13. import java.util.List;
  14. import java.util.Objects;
  15. import java.util.Optional;
  16. /**
  17. * @Description: zip4j工具
  18. * @Param:
  19. * @return:
  20. * @Author: wangliang
  21. * @Date: 2022/8/12
  22. */
  23. public class Zip4jUtil {
  24. private final static Logger log = LoggerFactory.getLogger(HttpKit.class);
  25. /**
  26. * 压缩zip
  27. *
  28. * @param scrPath
  29. * @param files
  30. */
  31. public static void zipFile(String scrPath, List<File> files) {
  32. Optional.ofNullable(scrPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("原始路径不能为空"));
  33. if (!scrPath.endsWith(SystemConstant.ZIP_PREFIX)) {
  34. throw ExceptionResultEnum.ERROR.exception("压缩文件必须为zip");
  35. }
  36. if (CollectionUtils.isEmpty(files)) {
  37. throw ExceptionResultEnum.ERROR.exception("没有待压缩的文件");
  38. }
  39. commonZipFile(scrPath, null, files, null);
  40. }
  41. /**
  42. * 压缩zip
  43. *
  44. * @param scrPath
  45. * @param destPath
  46. */
  47. public static void zipFile(String scrPath, String destPath) {
  48. Optional.ofNullable(scrPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("原始路径不能为空"));
  49. if (!scrPath.endsWith(SystemConstant.ZIP_PREFIX)) {
  50. throw ExceptionResultEnum.ERROR.exception("压缩文件必须为zip");
  51. }
  52. Optional.ofNullable(destPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("压缩路径不能为空"));
  53. commonZipFile(scrPath, destPath, null, null);
  54. }
  55. /**
  56. * 压缩zip带密码
  57. *
  58. * @param scrPath
  59. * @param files
  60. * @param password
  61. * @throws ZipException
  62. */
  63. public static void zipEncryptFile(String scrPath, List<File> files, String password) {
  64. Optional.ofNullable(scrPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("原始路径不能为空"));
  65. if (!scrPath.endsWith(SystemConstant.ZIP_PREFIX)) {
  66. throw ExceptionResultEnum.ERROR.exception("压缩文件必须为zip");
  67. }
  68. if (CollectionUtils.isEmpty(files)) {
  69. throw ExceptionResultEnum.ERROR.exception("没有待压缩的文件");
  70. }
  71. Optional.ofNullable(password).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("压缩密码不能为空"));
  72. commonZipFile(scrPath, null, files, password);
  73. }
  74. /**
  75. * 压缩zip带密码
  76. *
  77. * @param scrPath
  78. * @param destPath
  79. * @param password
  80. */
  81. public static void zipEncryptFile(String scrPath, String destPath, String password) {
  82. Optional.ofNullable(scrPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("原始路径不能为空"));
  83. if (!scrPath.endsWith(SystemConstant.ZIP_PREFIX)) {
  84. throw ExceptionResultEnum.ERROR.exception("压缩文件必须为zip");
  85. }
  86. Optional.ofNullable(destPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("压缩路径不能为空"));
  87. Optional.ofNullable(password).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("压缩密码不能为空"));
  88. commonZipFile(scrPath, destPath, null, password);
  89. }
  90. /**
  91. * 解压zip
  92. *
  93. * @param scrPath
  94. * @param destPath
  95. */
  96. public static void unzip(String scrPath, String destPath) {
  97. Optional.ofNullable(scrPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("原始路径不能为空"));
  98. Optional.ofNullable(destPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("解压路径不能为空"));
  99. commonUnZipFile(scrPath, destPath, null);
  100. }
  101. /**
  102. * 解压zip带密码
  103. *
  104. * @param scrPath
  105. * @param destPath
  106. * @param password
  107. */
  108. public static void unzipEncryptFile(String scrPath, String destPath, String password) {
  109. Optional.ofNullable(scrPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("原始路径不能为空"));
  110. Optional.ofNullable(destPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("解压路径不能为空"));
  111. Optional.ofNullable(password).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("解压密码不能为空"));
  112. commonUnZipFile(scrPath, destPath, password);
  113. }
  114. /**
  115. * 解压zip包
  116. *
  117. * @param scrPath
  118. * @param destPath
  119. * @param password
  120. */
  121. private static void commonUnZipFile(String scrPath, String destPath, String password) {
  122. try {
  123. File file = new File(scrPath);
  124. if (!file.exists()) {
  125. file.getParentFile().mkdirs();
  126. }
  127. ZipFile zipFile = new ZipFile(scrPath);
  128. if (Objects.nonNull(password) && zipFile.isEncrypted()) {
  129. zipFile.setPassword(password);
  130. }
  131. File currentFile = new File(destPath);
  132. if (!currentFile.exists()) {
  133. throw ExceptionResultEnum.ERROR.exception("待解压的路径不存在");
  134. }
  135. zipFile.extractAll(destPath);
  136. } catch (ZipException e) {
  137. log.error(SystemConstant.LOG_ERROR, e);
  138. throw ExceptionResultEnum.ERROR.exception(e.getMessage());
  139. }
  140. }
  141. /**
  142. * 压缩zip包
  143. *
  144. * @param scrPath
  145. * @param destPath
  146. * @param files
  147. * @param password
  148. */
  149. private static void commonZipFile(String scrPath, String destPath, List<File> files, String password) {
  150. try {
  151. File file = new File(scrPath);
  152. if (!file.exists()) {
  153. file.getParentFile().mkdirs();
  154. }
  155. // 生成的压缩文件
  156. ZipFile zipFile = new ZipFile(scrPath);
  157. ZipParameters parameters = new ZipParameters();
  158. // 压缩级别
  159. parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
  160. // 压缩级别
  161. parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
  162. if (Objects.nonNull(password)) {
  163. parameters.setEncryptFiles(true);
  164. parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
  165. parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
  166. parameters.setPassword(password);
  167. }
  168. File[] fs = null;
  169. if (!CollectionUtil.isEmpty(files)) {
  170. // 要打包的文件夹
  171. fs = files.toArray(new File[files.size()]);
  172. } else if (Objects.nonNull(destPath)) {
  173. File currentFile = new File(destPath);
  174. if (!currentFile.exists()) {
  175. throw ExceptionResultEnum.ERROR.exception("待压缩的路径不存在");
  176. }
  177. fs = currentFile.listFiles();
  178. }
  179. // 遍历test文件夹下所有的文件、文件夹
  180. for (File f : fs) {
  181. if (f.isDirectory()) {
  182. zipFile.addFolder(f.getPath(), parameters);
  183. } else {
  184. zipFile.addFile(f, parameters);
  185. }
  186. }
  187. } catch (Exception e) {
  188. log.error(SystemConstant.LOG_ERROR, e);
  189. throw ExceptionResultEnum.ERROR.exception(e.getMessage());
  190. }
  191. }
  192. }