700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java上传rar文件_java实现上传zip/rar压缩文件 自动解压

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

时间:2021-01-11 02:45:50

相关推荐

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: /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("unPack zip file to " + destPath + " fail ....", e.getMessage(), e);

}

}

/**

* rar文件解压(不支持有密码的压缩包)

*

* @param rarFile rar压缩包

* @param destPath 解压保存路径

*/

public static void unPackRar(File rarFile, String destPath) {

try (Archive archive = new Archive(rarFile)) {

if (null != archive) {

FileHeader fileHeader = archive.nextFileHeader();

File file = null;

while (null != fileHeader) {

// 防止文件名中文乱码问题的处理

String fileName = fileHeader.getFileNameW().isEmpty() ? fileHeader.getFileNameString() : fileHeader.getFileNameW();

if (fileHeader.isDirectory()) {

//是文件夹

file = new File(destPath + File.separator + fileName);

file.mkdirs();

} else {

//不是文件夹

file = new File(destPath + File.separator + fileName.trim());

if (!file.exists()) {

if (!file.getParentFile().exists()) {

// 相对路径可能多级,可能需要创建父目录.

file.getParentFile().mkdirs();

}

file.createNewFile();

}

FileOutputStream os = new FileOutputStream(file);

archive.extractFile(fileHeader, os);

os.close();

}

fileHeader = archive.nextFileHeader();

}

}

} catch (Exception e) {

log.error("unpack rar file fail....", e.getMessage(), e);

}

}

}

页面HTML

Title

上传压缩包:

解压路径:

解压密码(为空可不传):

controller代码:

package com.hf.uncompress.controller;

import com.hf.uncompress.Result.AjaxList;

import com.hf.uncompress.service.FileUploadService;

import com.hf.uncompress.vo.PackParam;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

/**

* @Description:

* @Date: /1/22

* @Auther:

*/

@Controller

@RequestMapping("/user")

@Slf4j

public class FileUploadController {

@Autowired

private FileUploadService fileUploadService;

@GetMapping("/redirect")

public String redirectHtml() {

return "work";

}

@PostMapping("/upload/zip")

@ResponseBody

public String uploadZip(MultipartFile zipFile, @RequestBody PackParam packParam) {

AjaxListajaxList = fileUploadService.handlerUpload(zipFile, packParam);

return ajaxList.getData();

}

}

service实现类代码

package com.hf.uncompress.service.impl;

import com.hf.uncompress.Result.AjaxList;

import com.hf.uncompress.enums.FileTypeEnum;

import com.hf.uncompress.service.FileUploadService;

import com.hf.uncompress.utils.UnPackeUtil;

import com.hf.uncompress.vo.PackParam;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;

import java.io.IOException;

/**

* @Description:

* @Date: /1/22

* @Auther:

*/

@Service

@Slf4j

public class FileUploadServiceImpl implements FileUploadService {

@Override

public AjaxListhandlerUpload(MultipartFile zipFile, PackParam packParam) {

if (null == zipFile) {

return AjaxList.createFail("请上传压缩文件!");

}

boolean isZipPack = true;

String fileContentType = zipFile.getContentType();

//将压缩包保存在指定路径

String packFilePath = packParam.getDestPath() + File.separator + zipFile.getName();

if (FileTypeEnum.FILE_TYPE_ZIP.type.equals(fileContentType)) {

//zip解压缩处理

packFilePath += FileTypeEnum.FILE_TYPE_ZIP.fileStufix;

} else if (FileTypeEnum.FILE_TYPE_RAR.type.equals(fileContentType)) {

//rar解压缩处理

packFilePath += FileTypeEnum.FILE_TYPE_RAR.fileStufix;

isZipPack = false;

} else {

return AjaxList.createFail("上传的压缩包格式不正确,仅支持rar和zip压缩文件!");

}

File file = new File(packFilePath);

try {

zipFile.transferTo(file);

} catch (IOException e) {

log.error("zip file save to " + packParam.getDestPath() + " error", e.getMessage(), e);

return AjaxList.createFail("保存压缩文件到:" + packParam.getDestPath() + " 失败!");

}

if (isZipPack) {

//zip压缩包

UnPackeUtil.unPackZip(file, packParam.getPassword(), packParam.getDestPath());

} else {

//rar压缩包

UnPackeUtil.unPackRar(file, packParam.getDestPath());

}

return AjaxList.createSuccess("解压成功");

}

}

使用到的枚举类:

package com.hf.uncompress.enums;

import lombok.AllArgsConstructor;

import lombok.NoArgsConstructor;

/**

* @Description: 压缩文件类型

* @Date: /1/22

* @Auther:

*/

@AllArgsConstructor

@NoArgsConstructor

public enum FileTypeEnum {

FILE_TYPE_ZIP("application/zip", ".zip"),

FILE_TYPE_RAR("application/octet-stream", ".rar");

public String type;

public String fileStufix;

public static String getFileStufix(String type) {

for (FileTypeEnum orderTypeEnum : FileTypeEnum.values()) {

if (orderTypeEnum.type.equals(type)) {

return orderTypeEnum.fileStufix;

}

}

return null;

}

}

同一返回值定义:

package com.hf.uncompress.Result;

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

/**

* @Description: 返回值处理

* @Date: /1/22

* @Auther:

*/

@AllArgsConstructor

@NoArgsConstructor

@Data

public class AjaxList{

private boolean isSuccess;

private T data;

public static AjaxListcreateSuccess(T data) {

return new AjaxList(true, data);

}

public static AjaxListcreateFail(T data) {

return new AjaxList(false, data);

}

}

前端上传封装的vo

package com.hf.uncompress.vo;

import lombok.Data;

/**

* @Description: 上传压缩的参数

* @Date: /1/23

* @Auther:

*/

@Data

public class PackParam {

/**

* 解压密码

*/

private String password;

/**

* 解压文件存储地址

*/

private String destPath;

}

在application.properties中定义其上传的阀域

#设置上传单个文件的大小限制

spring.servlet.multipart.max-file-size=500MB

# 上传文件总的最大值

spring.servlet.multipart.max-request-size=500MB

spring.thymeleaf.cache=false

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。