废话连篇,上代码~
<!-- zip -->
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.1</version>
</dependency>
/**
* 上传压缩文件
* @param file
* @return
*
*/
@PostMapping("/uploadZip")
public R uploadZip(MultipartFile file) {
try {
// 存放压缩文件目录
String path = "/usr/share/nginx/html/edcp/";
// 压缩文件名称 采用时间戳+3位随机数
String url = System.currentTimeMillis()+IdUtils.getRandom3(3);
// 上传文件到服务器
File file1 = new File(path + url + ".zip");
FileUtils.writeByteArrayToFile(file1, file.getBytes());
// 使用ZipFile解压
ZipFile zipFile = new ZipFile(file1);
// 给的vr是gbk,如果这里,你上传的zip包为utf-8,那么这里改为utf-8
zipFile.setFileNameCharset("gbk");
// zipFile.setFileNameCharset("utf-8");
// 解压到指定目录,因为上传的压缩文件名称都是相同的,所以新增目录将存放解压后的文件
zipFile.extractAll(path + url);
return R.ok();
} catch (ZipException e) {
e.printStackTrace();
return R.error("上传文件失败 ZipException:" + e.getMessage());
} catch (FileNotFoundException e) {
e.printStackTrace();
return R.error("上传文件失败 FileNotFoundException:" + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
return R.error("上传文件失败 IOException:" + e.getMessage());
}
}