Browse Source

下载zip文件加密

wangliang 2 năm trước cách đây
mục cha
commit
9686e41606

+ 21 - 11
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/Zip4jUtil.java

@@ -31,13 +31,14 @@ public class Zip4jUtil {
      * @param files
      */
     public static void zipFile(String scrPath, List<File> files) {
+        Optional.ofNullable(scrPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("原始路径不能为空"));
+        if (!scrPath.endsWith(".zip")) {
+            throw ExceptionResultEnum.ERROR.exception("压缩文件必须为zip");
+        }
+        if (CollectionUtils.isEmpty(files)) {
+            throw ExceptionResultEnum.ERROR.exception("没有待压缩的文件");
+        }
         try {
-            if (!scrPath.endsWith(".zip")) {
-                throw ExceptionResultEnum.ERROR.exception("压缩文件必须为zip");
-            }
-            if (CollectionUtils.isEmpty(files)) {
-                throw ExceptionResultEnum.ERROR.exception("没有待压缩的文件");
-            }
             // 生成的压缩文件
             ZipFile zipFile = new ZipFile(scrPath);
             ZipParameters parameters = new ZipParameters();
@@ -66,12 +67,13 @@ public class Zip4jUtil {
      *
      * @param scrPath
      * @param destPath
-     * @throws ZipException
      */
-    public static void zipFile(String scrPath, String destPath) throws ZipException {
+    public static void zipFile(String scrPath, String destPath) {
+        Optional.ofNullable(scrPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("原始路径不能为空"));
         if (!scrPath.endsWith(".zip")) {
             throw ExceptionResultEnum.ERROR.exception("压缩文件必须为zip");
         }
+        Optional.ofNullable(destPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("压缩路径不能为空"));
         try {
             // 生成的压缩文件
             ZipFile zipFile = new ZipFile(scrPath);
@@ -108,13 +110,15 @@ public class Zip4jUtil {
      * @param password
      * @throws ZipException
      */
-    public static void zipEncryptFile(String scrPath, List<File> files, String password) throws ZipException {
+    public static void zipEncryptFile(String scrPath, List<File> files, String password) {
+        Optional.ofNullable(scrPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("原始路径不能为空"));
         if (!scrPath.endsWith(".zip")) {
             throw ExceptionResultEnum.ERROR.exception("压缩文件必须为zip");
         }
         if (CollectionUtils.isEmpty(files)) {
             throw ExceptionResultEnum.ERROR.exception("没有待压缩的文件");
         }
+        Optional.ofNullable(password).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("压缩密码不能为空"));
         try {
             // 生成的压缩文件
             ZipFile zipFile = new ZipFile(scrPath);
@@ -151,12 +155,14 @@ public class Zip4jUtil {
      * @param scrPath
      * @param destPath
      * @param password
-     * @throws ZipException
      */
-    public static void zipEncryptFile(String scrPath, String destPath, String password) throws ZipException {
+    public static void zipEncryptFile(String scrPath, String destPath, String password) {
+        Optional.ofNullable(scrPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("原始路径不能为空"));
         if (!scrPath.endsWith(".zip")) {
             throw ExceptionResultEnum.ERROR.exception("压缩文件必须为zip");
         }
+        Optional.ofNullable(destPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("压缩路径不能为空"));
+        Optional.ofNullable(password).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("压缩密码不能为空"));
         try {
             // 生成的压缩文件
             ZipFile zipFile = new ZipFile(scrPath);
@@ -198,6 +204,8 @@ public class Zip4jUtil {
      * @param destPath
      */
     public static void unzip(String scrPath, String destPath) {
+        Optional.ofNullable(scrPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("原始路径不能为空"));
+        Optional.ofNullable(destPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("解压路径不能为空"));
         try {
             ZipFile zipFile = new ZipFile(scrPath);
             zipFile.extractAll(destPath);
@@ -215,6 +223,8 @@ public class Zip4jUtil {
      * @param password
      */
     public static void unzipEncryptFile(String scrPath, String destPath, String password) {
+        Optional.ofNullable(scrPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("原始路径不能为空"));
+        Optional.ofNullable(destPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("解压路径不能为空"));
         Optional.ofNullable(password).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("解压密码不能为空"));
         try {
             ZipFile zipFile = new ZipFile(scrPath);