使用zip4j压缩/解压工具

1.引入相关依赖

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

2.将某个目录(文件夹) 压缩为ZIP包

/**
     * zip4j压缩目录为一个zip包
     *
     * 同一路径多次压缩只会生成一个zip包
     * 返回null表示压缩失败,
     * 返回ZipFile对象表示压缩成功。
     * @param productZipDir   生成的zip包存放路径 (d:\\xxx\\xx\\)
     * @param productZipName   生成的zip包名 (xxx.zip)
     * @param originalDestPath  要压缩的原始目录地址(e:\\xxx\\xx\\)
     * @throws ZipException
     */
    public static ZipFile fastZip(String productZipDir,String productZipName  ,String originalDestPath) throws ZipException {

        if (StringUtils.isEmpty(productZipDir)){
            return null;
        }
        if (StringUtils.isEmpty(productZipName)){
            return null;
        }
        if (StringUtils.isEmpty(originalDestPath)){
            return null;
        }

        //zip包存放路径 不存在则建立
        File productDir = new File(productZipDir);
        if (! productDir.exists()){
            productDir.mkdirs();
        }

        //要压缩的原始目录地址 不存在则返回null
        File originalPath =new File(originalDestPath);
        if (! originalPath.isDirectory()){
            return null;
        }
        if (!originalPath.exists()){
            return null;
        }


        // 生成的压缩文件
        ZipFile zipFile = new ZipFile(productZipDir+productZipName);


        ZipParameters parameters = new ZipParameters();
        // 压缩方式 8
        parameters.setCompressionMethod(8);
        // 压缩级别 5
        parameters.setCompressionLevel(5);
        // 要打包的文件夹
        File currentFile = new File(originalDestPath);
        File[] fs = currentFile.listFiles();
        // 遍历originalDestPath文件夹下所有的文件、文件夹
        for (File f : fs) {
            if (f.isDirectory()) {
                zipFile.addFolder( new File(f.getPath()) , parameters);
            } else {
                zipFile.addFile(f, parameters);
            }
        }
        //返回zip包对象
        return zipFile;
    }

3.解压ZIP包到目标路径

/**
     * 解压ZIP包
     * @param zipDir zip包路径
     * @param destPath  目标解压路径
     * @throws ZipException
     */
    public static void unzip(File zipDir, String destPath) throws ZipException {
        //1.设置zip包路径
        ZipFile zipFile = new ZipFile(zipDir);
        //设置文件名GBK编码
        zipFile.setFileNameCharset("GBK");

        if (!zipFile.isValidZipFile()) {
            log.error("{} 压缩包已经损坏!",zipDir);
            return;
        }

        // 解压目录不存在 则创建
        File destDir = new File(destPath);
        if (destDir.isDirectory() && !destDir.exists()) {
            destDir.mkdir();
        }


        // 设置密码
        if (zipFile.isEncrypted()) {
            //zipFile.setPassword(passwd.toCharArray());
            log.error("{} 压缩包存在密码,无法解压!,请配置解压密码",zipDir);
            return;
        }


        //执行解压
        zipFile.extractAll(destPath);


    }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ThinkPet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值