java上传rar文件_java实现上传zip/rar压缩文件,自动解压

在pom中添加解压jar依赖

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.2.RELEASE

com.hf

uncompress

0.0.1-SNAPSHOT

uncompress

上传压缩文件(rar或者zip格式),解压

1.8

org.springframework.boot

spring-boot-starter-web

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter-thymeleaf

net.lingala.zip4j

zip4j

1.3.2

com.github.junrar

junrar

0.7

org.springframework.boot

spring-boot-maven-plugin

解压zip/rar的工具类

package com.hf.uncompress.utils;

import com.github.junrar.Archive;

import com.github.junrar.rarfile.FileHeader;

import lombok.extern.slf4j.Slf4j;

import net.lingala.zip4j.core.ZipFile;

import java.io.File;

import java.io.FileOutputStream;

/**

* @Description: 解压rar/zip工具类

* @Date: 2019/1/22

* @Auther:

*/

@Slf4j

public class UnPackeUtil {

/**

* zip文件解压

*

* @param destPath 解压文件路径

* @param zipFile 压缩文件

* @param password 解压密码(如果有)

*/

public static void unPackZip(File zipFile, String password, String destPath) {

try {

ZipFile zip = new ZipFile(zipFile);

/*zip4j默认用GBK编码去解压,这里设置编码为GBK的*/

zip.setFileNameCharset("GBK");

log.info("begin unpack zip file....");

zip.extractAll(destPath);

// 如果解压需要密码

if (zip.isEncrypted()) {

zip.setPassword(password);

}

} catch (Exception e) {

log.error("unPa

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值