管道基础大数据平台系统开发-【后端】-Server
13693261870
2022-10-20 3249d41536899bbacf88af45f290db3efdbdc790
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.lf.server.helper;
 
import com.google.common.base.Strings;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.AesKeyStrength;
import net.lingala.zip4j.model.enums.CompressionLevel;
import net.lingala.zip4j.model.enums.CompressionMethod;
import net.lingala.zip4j.model.enums.EncryptionMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
import java.io.File;
import java.nio.charset.Charset;
 
/**
 * Zip4j帮助类
 * @author WWW
 */
public class Zip4jHelper {
    private final static Log log = LogFactory.getLog(Zip4jHelper.class);
 
    /**
     * Zip压缩
     *
     * @param zipFilePath zip文件
     * @param sourcePath  源路径
     * @param password    密码
     * @return 成功是否
     */
    public static boolean zip(String zipFilePath, String sourcePath, String password) {
        try {
            ZipFile zip = new ZipFile(zipFilePath);
            zip.setCharset(Charset.forName("UTF-8"));
 
            File f = zip.getFile();
            if (!f.getParentFile().exists()) {
                f.getParentFile().mkdirs();
            }
            if (f.exists()) {
                f.delete();
            }
 
            // 设置压缩文件参数
            ZipParameters params = new ZipParameters();
            // 压缩方式
            params.setCompressionMethod(CompressionMethod.DEFLATE);
            // 压缩级别
            params.setCompressionLevel(CompressionLevel.NORMAL);
            // 是否设置加密文件
            params.setEncryptFiles(true);
            // 设置AES加密强度:KEY_STRENGTH_256
            params.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_128);
            // 设置加密算法
            params.setEncryptionMethod(EncryptionMethod.AES);
            // 设置密码
            if (!Strings.isNullOrEmpty(password)) {
                zip.setPassword(password.toCharArray());
            }
 
            // 要打包的文件或文件夹
            File currentFile = new File(sourcePath);
            if (currentFile.isDirectory()) {
                zip.addFolder(currentFile, params);
            } else {
                zip.addFile(currentFile, params);
            }
 
            return true;
        } catch (Exception ex) {
            log.error(ex.getMessage() + ex.getStackTrace() + "\n");
            return false;
        }
    }
 
    private static void addZipFile(ZipFile zip, ZipParameters params, File file) throws ZipException {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                addZipFile(zip, params, f);
            }
        } else {
            zip.addFile(file, params);
        }
    }
 
    /**
     * Zip解压
     *
     * @param zipFile  zip文件
     * @param toPath   解压路径
     * @param password 密码
     * @return 成功是/否
     */
    public static boolean unzip(String zipFile, String toPath, String password) {
        try {
            // 生成的压缩文件
            ZipFile zip = new ZipFile(zipFile);
 
            // 设置密码
            if (!Strings.isNullOrEmpty(password)) {
                zip.setPassword(password.toCharArray());
            }
 
            // 解压缩所有文件以及文件夹
            zip.extractAll(toPath);
 
            return true;
        } catch (Exception ex) {
            log.error(ex.getMessage() + ex.getStackTrace() + "\n");
            return false;
        }
    }
}