淡语

导航

java操作zip压缩文件加密码和解密工具类

1.添加依赖

<!-- zip压缩文件工具类 -->
        <dependency>
            <groupId>net.lingala.zip4j</groupId>
            <artifactId>zip4j</artifactId>
            <version>1.3.2</version>
        </dependency>

2.工具类

package com.danyu.common.utils.file;

import com.danyu.common.utils.StringUtils;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * danyu
 * 压缩文件工具类
 */
public class ZipUtil {

    private static final Logger log = LoggerFactory.getLogger(ZipUtil.class);

    /**
     *
     * 压缩指定路径的文件
     * @param srcFilePath 待压缩文件路径
     * @param zipPathFileName zip文件全路径名
     * @param password 加密密码
     * @return
     */
    public static boolean zipFile(String srcFilePath, String zipPathFileName, String password){

        try {
            // 生成的压缩文件
            ZipFile zipFile = new ZipFile(zipPathFileName);
            ZipParameters parameters = new ZipParameters();
            // 压缩级别
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

            if(!StringUtils.isEmpty(password)){
                parameters.setEncryptFiles(true);
                parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
                parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
                parameters.setPassword(password);
            }

            // 要打包的文件夹
            File currentFile = new File(srcFilePath);
            File[] fs = currentFile.listFiles();
            // 遍历test文件夹下所有的文件、文件夹
            for (File f : fs) {
                if (f.isDirectory()) {
                    zipFile.addFolder(f.getPath(), parameters);
                } else {
                    zipFile.addFile(f, parameters);
                }
            }
            return true;
        } catch (ZipException e) {
            e.printStackTrace();
            log.error("压缩文件【"+srcFilePath+"】到路径【"+zipPathFileName+"】失败:\n"+e.getMessage());
            return false;
        }

    }

    /**
     *  @param zipFileFullName zip文件所在的路径名
     * @param filePath 解压到的路径
     * @param password 需要解压的密码
     * @return
     */
    public static boolean unZipFile(String zipFileFullName, String filePath, String password) {
        try {
            ZipFile zipFile = new ZipFile(zipFileFullName);
            // 如果解压需要密码
            if(StringUtils.isNotEmpty(password)&&zipFile.isEncrypted()) {
                zipFile.setPassword(password);
            }
            zipFile.extractAll(filePath);
            return true;
        } catch (ZipException e) {
            e.printStackTrace();
            log.error("解压文件【"+zipFileFullName+"】到路径【"+filePath+"】失败:\n"+e.getMessage());
            return false;
        }
    }



    /**
     * 添加文件到压缩文件中
     * @param zipFullFileName zip文件所在路径及全名
     * @param fullFileNameList 待添加的文件全路径集合
     * @param rootFolderInZip 在压缩文件里的文件夹名
     * @return
     */
    public static boolean addFilesToZip(String zipFullFileName, List<String> fullFileNameList, String rootFolderInZip) {

        try {
            ZipFile zipFile = new ZipFile(zipFullFileName);
            ArrayList<File> addFiles = new ArrayList<>();
            for (String fileName:fullFileNameList) {

                addFiles.add(new File(fileName));
            }
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
            if(StringUtils.isNotEmpty(rootFolderInZip)){
                if(rootFolderInZip.endsWith("/")==false){
                    rootFolderInZip = rootFolderInZip+"/";
                }
                parameters.setRootFolderInZip(rootFolderInZip);
            }
            zipFile.addFiles(addFiles, parameters);
            return true;
        } catch (ZipException e) {
            e.printStackTrace();
            log.error("添加文件失败:\n"+e.getMessage());
            return false;
        }
    }


    /**
     * 从压缩文件中删除路径
     * @param zipFullFileName
     * @param fileName
     * @return
     */
    public static boolean deleteFileInZip(String zipFullFileName, String fileName) {
        try {
            ZipFile zipFile = new ZipFile(zipFullFileName);
            zipFile.removeFile(fileName);
            return true;
        } catch (ZipException e) {
            e.printStackTrace();
            log.error("删除文件失败:\n"+e.getMessage());
            return false;
        }

    }


}

  

posted on 2021-01-29 11:07  object360  阅读(4712)  评论(0编辑  收藏  举报