Zip4j 压缩包加密压缩与解压

引入依赖:zip4j

 <dependency>
       <groupId>net.lingala.zip4j</groupId>
       <artifactId>zip4j</artifactId>
       <version>1.3.2</version>
</dependency>

一、压缩

不多说直接上图,如下图为我测试的文件路径。
桌面上的“测试加密压缩”文件夹下,一个为文件,一个为文件夹。
测试文件路径图

public class Test {
    public static void main(String[] args) throws ZipException {
        compressedFileWithPassword("C:/Users/12495/Desktop/测试加密压缩",
                "C:/Users/12495/Desktop/测试加密压缩","123456");
    }

    /**
     * 压缩包加密
     *
     * @param resourcesPath 需要压缩的源文件路径
     * @param targetPath 压缩后的目标路径
     * @param password 压缩密码
     * @throws ZipException
     */
    public static void compressedFileWithPassword(String resourcesPath, String targetPath, String password) throws ZipException {
        File resourcesFile = new File(resourcesPath);
        // 没有目标目录为父路径
        if (StringUtils.isBlank(targetPath)) {
            targetPath = resourcesFile.getParent();
        }
        File targetFile = new File(targetPath);
        //如果目的路径不存在,则新建
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        ZipFile zipFile = new ZipFile(resourcesPath + ".zip");
        //设置编码
        zipFile.setFileNameCharset("GBK");
        //设置参数
        ZipParameters parameters = new ZipParameters();
        //设置压缩方式.
        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
        //设置压缩级别,默认为0(即不压缩)
        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
        //设置压缩密码(至少3步)
        //设置加密文件
        parameters.setEncryptFiles(true);
        //设置加密方式
        parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
        if (StringUtils.isNotBlank(password)) {
            parameters.setPassword(password);
        }
        File[] fs = resourcesFile.listFiles();
        for (File f : fs) {
            if (f.isDirectory()) {
                //添加文件夹
                zipFile.addFolder(f, parameters);
            } else {
                //添加文件
                zipFile.addFile(f, parameters);
            }
        }
    }
}

压缩后效果图:
压缩后效果图
在这里插入图片描述
成功加密压缩。

二、解压

解压就比较简单了,路径对就没问题了

public class Test {
    public static void main(String[] args) throws Exception {
//        compressedFileWithPassword("C:/Users/12495/Desktop/测试加密压缩",
//                "C:/Users/12495/Desktop/测试加密压缩","123456");
        zipUncompressPassword("C:/Users/12495/Desktop/测试加密压缩.zip", "123456");
    }

    /**
     * 解压缩
     * @param inputFile 压缩包的路径
     * @throws Exception
     */
    public static void zipUncompressPassword(String inputFile, String password) throws Exception {
        File file = new File(inputFile);
        ZipFile zipFile = new ZipFile(file);
        //设置文件编码,根据实际场景
        zipFile.setFileNameCharset("GBK");
        if (zipFile.isEncrypted()) {
            zipFile.setPassword(password.toCharArray());
        }
        zipFile.extractAll(inputFile.replace(".zip", ""));
    }
}

以上就是解压的过程。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值