在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