管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-11-28 8cce08d1d96384edbd2d362173ffa98ca933ef13
src/main/java/com/lf/server/helper/Zip4jHelper.java
@@ -1,6 +1,5 @@
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;
@@ -12,7 +11,7 @@
import org.apache.commons.logging.LogFactory;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/**
 * Zip4j帮助类
@@ -24,15 +23,15 @@
    /**
     * Zip压缩
     *
     * @param zipFilePath zip文件
     * @param sourcePath  源路径
     * @param password    密码
     * @param zipFile    zip文件
     * @param sourcePath 源路径
     * @param pwd        密码
     * @return 成功是否
     */
    public static boolean zip(String zipFilePath, String sourcePath, String password) {
    public static boolean zip(String zipFile, String sourcePath, String pwd) {
        try {
            ZipFile zip = new ZipFile(zipFilePath);
            zip.setCharset(Charset.forName("UTF-8"));
            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()) {
@@ -42,22 +41,7 @@
                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());
            }
            ZipParameters params = getZipParams();
            // 要打包的文件或文件夹
            File currentFile = new File(sourcePath);
@@ -74,6 +58,49 @@
        }
    }
    /**
     * 创建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(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() {
        // 设置压缩文件参数
        ZipParameters params = new ZipParameters();
        // 压缩方式
        params.setCompressionMethod(CompressionMethod.DEFLATE);
        // 压缩级别
        params.setCompressionLevel(CompressionLevel.MAXIMUM);
        // 是否设置加密文件
        params.setEncryptFiles(true);
        // 设置AES加密强度:KEY_STRENGTH_256
        params.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_128);
        // 设置加密算法
        params.setEncryptionMethod(EncryptionMethod.AES);
        return params;
    }
    private static void addZipFile(ZipFile zip, ZipParameters params, File file) throws ZipException {
        if (file.isDirectory()) {
            File[] files = file.listFiles();