月球大数据地理空间分析展示平台-【后端】-月球后台服务
13693261870
2023-06-02 c31e03f0e51214a524d3fc34d30f3459698ff625
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.moon.server.helper;
 
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.StandardCharsets;
 
/**
 * Zip4j帮助类
 * @author WWW
 */
public class Zip4jHelper {
    private final static Log log = LogFactory.getLog(Zip4jHelper.class);
 
    /**
     * Zip压缩
     *
     * @param zipFile    zip文件
     * @param sourcePath 源路径
     * @param pwd        密码
     * @return 成功是否
     */
    public static boolean zip(String zipFile, String sourcePath, String pwd) {
        try {
            ZipFile zip = StringHelper.isEmpty(pwd) ? new ZipFile(zipFile) : new ZipFile(zipFile, pwd.toCharArray());
            zip.setCharset(StandardCharsets.UTF_8);
 
            File f = zip.getFile();
            if (!f.getParentFile().exists()) {
                f.getParentFile().mkdirs();
            }
            if (f.exists()) {
                f.delete();
            }
 
            ZipParameters params = getZipParams(!StringHelper.isEmpty(pwd));
 
            // 要打包的文件或文件夹
            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);
            return false;
        }
    }
 
    /**
     * 创建ZipFile
     */
    public static ZipFile createZipFile(String zipFile, String pwd) {
        try {
            ZipFile zip = StringHelper.isEmpty(pwd) ? new ZipFile(zipFile) : new ZipFile(zipFile, pwd.toCharArray());
            // zip.setCharset(Charset.forName("GBK"))
            zip.setCharset(StandardCharsets.UTF_8);
 
            File f = zip.getFile();
            if (!f.getParentFile().exists()) {
                f.getParentFile().mkdirs();
            }
            if (f.exists()) {
                f.delete();
            }
 
            return zip;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        }
    }
 
    /**
     * 获取ZipParameters
     */
    public static ZipParameters getZipParams(boolean hasPwd) {
        // 设置压缩文件参数
        ZipParameters params = new ZipParameters();
        // 压缩方式
        params.setCompressionMethod(CompressionMethod.DEFLATE);
        // 压缩级别
        params.setCompressionLevel(CompressionLevel.MAXIMUM);
 
        if (hasPwd) {
            // 是否设置加密文件
            params.setEncryptFiles(true);
            // 设置AES加密强度:KEY_STRENGTH_256
            params.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_128);
            // 设置加密算法
            params.setEncryptionMethod(EncryptionMethod.AES);
        }
 
        return params;
    }
 
    /**
     * 添加文件至压缩包
     *
     * @param zip    ZipFile
     * @param params ZipParameters
     * @param file   File
     * @throws ZipException
     */
    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 (!StringHelper.isEmpty(password)) {
                zip.setPassword(password.toCharArray());
            }
 
            // 解压缩所有文件以及文件夹
            zip.extractAll(toPath);
 
            return true;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return false;
        }
    }
}