Zip4jUtil.java 8.2 KB

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