Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Recently encountered such a requirement: pass a background packet compression, decompression after reading the background save the files inside, now learning to do. Here be a record
File Upload
There are many ways to upload files, it is recommended here with a very good sense of their own, the code offer:
@PostMapping(value = "/import", headers = "content-type=multipart/*")
public R importSqlLite(@RequestParam("file") MultipartFile file) {
String path = "C:/Users/aaa/Desktop/新建文件夹/";
File newFile = new File(path + file.getOriginalFilename());
//通过CommonsMultipartFile的方法直接写文件(注意这个时候)
file.transferTo(newFile);
}
File decompression
Taken a lot of blog, we found a way to relieve the two, one is to use a tool org.apache.tools.zip.ZipFile this package is SpringBoot comes net.lingala.zip4j.core.ZipFile. Because using a SpringBoot project on the use of a second, offer codes:
//解压路径
private String dest = "C:\\Users\\aaa\\Desktop\\新建文件夹";
//解压后图片保存的路径
private String picPath = "C:/Users/aaa/Desktop/新建文件夹/pic";
public String Uncompress(String source) {
List<String> picPaths = new ArrayList<>();
try {
File zipFile = new File(source);
ZipFile zFile = new ZipFile(zipFile);// 首先创建ZipFile指向磁盘上的.zip文件
zFile.setFileNameCharset("GBK");
File destDir = new File(dest);// 解压目录
zFile.extractAll(dest);// 将文件抽出到解压目录
if (zFile.isEncrypted()) {
zFile.setPassword(password.toCharArray()); // 设置密码
}
zFile.extractAll(dest); // 将文件抽出到解压目录(解压)
List<net.lingala.zip4j.model.FileHeader > headerList = zFile.getFileHeaders();
List<File> extractedFileList= newArrayList<File>();
for(FileHeader fileHeader : headerList) {
if (!fileHeader.isDirectory()) {
extractedFileList.add(new File(destDir,fileHeader.getFileName()));
}
}
File [] extractedFiles = new File[extractedFileList.size()];
extractedFileList.toArray(extractedFiles);
for(File f:extractedFileList){
System.out.println(f.getAbsolutePath()+"....");
}
}catch(ZipException e) {
}