maven库:
<!--FileUtil、ZipUtil 依赖-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.1</version>
</dependency>
<!--IOUtils 依赖-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<!--MockMultipartFile 依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--MultipartFile 依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
代码:
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import org.apache.commons.io.IOUtils;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) throws IOException {
//生成File文件
File file = new File("H:\\testFile\\123.zip");
//File文件转MultipartFile
FileInputStream input = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));
//pdf文件名称
List<String> pdfName = new ArrayList<>();
//multipartFileList--存储解压后的,pdf文件
List<MultipartFile> multipartFileList = new ArrayList<>();
//设置解压后文件的临时路径
String destDirPath = FileUtil.getTmpDirPath() + "/standardFile/" + +System.nanoTime() + "/";
try {
//解压文件
List<File> files = parseMainTextZip(multipartFile, destDirPath);
System.out.println("解压文件个数:"+ files.size());
for (File filePdf : files) {
InputStream inputStream = new FileInputStream(filePdf);
MultipartFile multipartFileTmp = new MockMultipartFile("file", filePdf.getName(), "text/plain", IOUtils.toByteArray(inputStream));
//multipartFileList--存储解压后的,pdf文件
multipartFileList.add(multipartFileTmp);
String pdfNameStr = filePdf.getName();
pdfName.add(pdfNameStr.substring(0, pdfNameStr.length() - 4));
inputStream.close();
}
}catch (Exception e) {
e.printStackTrace();
} finally {
//删除临时路径中解压后的文件
FileUtil.del(destDirPath);
}
//处理解压后的文件--multipartFileList
for (MultipartFile filePdf : multipartFileList) {
String originalFilepdf = filePdf.getOriginalFilename();
if (originalFilepdf.endsWith("pdf")) {
originalFilepdf = originalFilepdf.substring(0, originalFilepdf.length() - 4);
System.out.println("文件名称为:"+originalFilepdf);
}
}
}
//解压文件
private static List<File> parseMainTextZip(MultipartFile mainTextFile, String destDirPath) throws Exception {
if (!FileUtil.exist(destDirPath)) {
//创建解压后的目录
createDirectory(destDirPath);
}
String tmpFilePath = destDirPath + mainTextFile.getOriginalFilename();
File fromStream = FileUtil.writeFromStream(mainTextFile.getInputStream(), tmpFilePath);
File zipFile;
try {
zipFile = ZipUtil.unzip(fromStream.getAbsolutePath(), destDirPath, Charset.forName("GBK"));
} catch (Exception e) {
zipFile = ZipUtil.unzip(fromStream.getAbsolutePath(), destDirPath, StandardCharsets.UTF_8);
}
List<File> extractedFileList = FileUtil.loopFiles(zipFile, file -> !file.getName().endsWith("zip"));
return extractedFileList;
}
/**
* 创建目录
*
* @param descDirName 目录名,包含路径
* @return 如果创建成功,则返回true,否则返回false
*/
public static boolean createDirectory(String descDirName) {
String descDirNames = descDirName;
if (!descDirNames.endsWith(File.separator)) {
descDirNames = descDirNames + File.separator;
}
File descDir = new File(descDirNames);
if (descDir.exists()) {
System.out.println("目录 " + descDirNames + " 已存在!");
return false;
}
// 创建目录
if (descDir.mkdirs()) {
System.out.println("目录 " + descDirNames + " 创建成功!");
return true;
} else {
System.out.println("目录 " + descDirNames + " 创建失败!");
return false;
}
}
}
文件目录:
压缩包为"123.zip":