whyclxw
2024-07-24 22e44f2a092a4cdcfb663d15cf033dac488322ba
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
package com.whyc.util;
 
import net.lingala.zip4j.core.ZipFile;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
 
public class Zip4jUtil {
 
    public static boolean checkZipFileParam(MultipartFile zipFile) {
        String contentType = zipFile.getContentType();
        //【导入Zip文件】上传的文件类型不是Zip
        return "application/zip".equals(contentType) || "application/x-zip-compressed".equals(contentType);
    }
 
    /**
     * zip文件解压
     *
     * @param destPath 解压文件路径
     * @param zipFile  压缩文件
     * @param password 解压密码(如果有)
     */
    public static boolean unPackZip(File zipFile, String password, String destPath) {
        try {
            ZipFile zip = new ZipFile(zipFile);
            /*zip4j默认用GBK编码去解压,这里设置编码为GBK的*/
            zip.setFileNameCharset("GBK");
            System.out.println("begin unpack zip file....");
            zip.extractAll(destPath);
            // 如果解压需要密码
            if (password != null) {
                if (zip.isEncrypted()) {
                    zip.setPassword(password);
                }
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}