在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("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: 2019/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: 2019/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: 2019/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: 2019/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: 2019/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

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

  1. html解压zip文件怎么打开方式,使用zip.js压缩文件和解压文件

    官方例子支持在线演示效果. 研究的目的是:如何获取zip包中的信息并读取传输(其实使用JAVA或者node.js更容易实现,之所以使用js也是因为业务的特殊性). 准备库: 下载成功解压是这样的,如图 ...

  2. 解压报错 你需要从上一压缩卷启动解压命令以便解压 解决方案及WinRAR怎么分卷压缩详解

    一.解决方案: 1.问题描述: 解压的时候报错,提示"你需要从上一压缩卷启动解压命令以便解压" 2.解决: 会出现这个问题,是因为你正在解压的文件为分卷压缩生成的压缩包.首先你要确 ...

  3. Java多word文件生成后进行压缩并导出下载后,压缩文件损坏并提示“不可预料的压缩文件末端”和“CRC校验失败”

    Java多word文件生成后进行压缩并导出下载后,压缩文件损坏并提示"不可预料的压缩文件末端"和"CRC校验失败" WinRAR 打开情况: 提示不可预料的压缩 ...

  4. 解决在Tomcat上手动部署WAR服务器不能自动解压的方法

    手动部署文件的方式有很多,其中一个方法是将项目打包成war包,放置到webapps目录下,正常效果是Tomcat自动解压war包,项目至此部署成功,但第一次部署时失效,我首先怀疑Tomcat版本的问题 ...

  5. linux解压安装包失败了怎么办,解压文件出错怎么办?百度网盘、Winrar等解压文件出错解决办法...

    2017年9月开始,百度网盘下载后错误尤为严重,某大型游戏下载站也是有网友报错,已向客服反应但都不回应,解决方法只有重新下载,修复,以及我们重新上传. 本站在2017年10月份要求上传者全部添加5%的 ...

  6. python 图像压缩后前端解压_Python在后台自动解压各种压缩文件的实现方法

    1.需求描述 编写一个 Python 程序,每次下载压缩包形式的文件后,自动将内部文件解压到当前文件夹后将压缩包删除,通过本案例可以学到的知识点: os 模块综合应用 glob 模块综合应用 利用 g ...

  7. linux系统下压缩文件,Linux系统下文件的压缩.打包与解压

    处理 .zip 文件的 zip 和 unzip zip 和 unzip 程序位于 /usr/bin 目录中,它们和 MS - DOS 下的 pkzip.pkunzip 以及 MS-windows 的 ...

  8. easyexcel结合zip 导出压缩文件(包含多个excel)

    easyexcel结合zip 导出压缩文件(包含多个excel) 直接上代码- 分批次查询处理示例代码 int limit = 1;int pageNum = 500;ByteArrayOutputS ...

  9. python zipfile压缩的文件用shell命令解压_Python学习第177课——bzip2、zip方式压缩文件和解压文件...

    之前我们学习了tar打包.解包.gzip压缩,现在我们学习gzip解压. ●gzip解压 现在我们把上节生成的压缩文件linux_compressed.gz进行解压,使用命令: tar -xzf li ...

  10. 【Linux command 06】zip命令 – 压缩文件

    1.功能 zip命令的功能是用于压缩文件,解压命令为unzip.通过zip命令可以将文件打包成.zip格式的压缩包,里面会附含文件的名称.路径.创建时间.上次修改时间等等信息,与tar命令相似. 2. ...

最新文章

  1. 激光雷达lidar与点云数据
  2. HIGHGUI ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV
  3. java获取当前方法
  4. 现代微波滤波器结构与设计_高功率射频及微波无源器件中的考虑和限制
  5. 百度飞桨和Imagination宣布在全球AI生态系统方面开展合作
  6. Win 10 终于干趴了 Win 7
  7. linux posix 线程池_linux多线程--POSIX Threads Programming
  8. jQuery操作cookie
  9. 数据结构:静态链表实现树的同构
  10. 接受字符串参数,返回一个元组,并分别统计字符串中大小写的个数
  11. 15.Object Manager
  12. [Apio2012]dispatching 左偏树
  13. 64位计算机安装xp,练习u盘如何安装XP 64位系统
  14. 淘宝客接入PHP(一)
  15. java自动往数据库里插shuaku_x大x鸟的青鸟云课堂自动答题实现原理
  16. PC 音频,视频硬件输出设置
  17. 计算机游戏比赛,计算机学习系统问世 机器会学习游戏比赛
  18. SAP UI5 进阶 - XML 视图里定义的 UI 控件,运行时实例化的技术细节剖析试读版
  19. 计算机哪个按键可以和弦,钢琴键盘和弦图解大全!作曲必看!老师和家长快收藏起来...
  20. STL学习_(一)STL简介-STL六大组件简介

热门文章

  1. 深入理解Solidity
  2. 缴满15年能领多少钱 养老金计算公式网上疯传
  3. 15条常用的视频音频编辑脚本命令(mencoder/ffmpeg等)
  4. @JsonFormat Date类型时间 格式化 注解 使用
  5. 解决: Caused by: java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver
  6. 出现 java.lang.NullPointerException 的几种原因、可能情况
  7. TCP/IP协议族 详解(TCP/IP四层模型、OSI七层模型)
  8. 解决IntelliJ Idea中文乱码问题、修改IDEA编码
  9. easyUI 日期控件修改...
  10. Vue Google浏览器插件 Vue Devtools无法使用的解决办法