package org.springblade.common.utils;
import java.io.*;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* @Description
* @ClassName UnzipUtils
* @Author yuhuofei
* @Date 2022/8/10 21:03
* @Version 1.0
*/
public class UnzipUtils {
/**
* 解压zip压缩文件到指定目录
*
* @param zipPath zip压缩文件绝对路径
* @param descDir 指定的解压目录
*/
public static void unzipFile(String zipPath, String descDir) throws IOException {
try {
File zipFile = new File(zipPath);
if (!zipFile.exists()) {
throw new IOException("要解压的压缩文件不存在");
}
File pathFile = new File(descDir);
if (!pathFile.exists()) {
pathFile.mkdirs();
}
InputStream input = new FileInputStream(zipPath);
unzipWithStream(input, descDir);
} catch (Exception e) {
throw new IOException(e);
}
}
/**
* 解压
*
* @param inputStream
* @param descDir
*/
public static void unzipWithStream(InputStream inputStream, String descDir) {
if (!descDir.endsWith(File.separator)) {
descDir = descDir + File.separator;
}
try (ZipInputStream zipInputStream = new ZipInputStream(inputStream, Charset.forName("GBK"))) {
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
String zipEntryNameStr = zipEntry.getName();
String zipEntryName = zipEntryNameStr;
if (zipEntryNameStr.contains("/")) {
String str1 = zipEntryNameStr.substring(0, zipEntryNameStr.indexOf("/"));
zipEntryName = zipEntryNameStr.substring(str1.length() + 1);
}
String outPath = (descDir + zipEntryName).replace("\\\\", "/");
File outFile = new File(outPath.substring(0, outPath.lastIndexOf(File.separator)));
if (!outFile.exists()) {
outFile.mkdirs();
}
if (new File(outPath).isDirectory()) {
continue;
}
writeFile(outPath, zipInputStream);
zipInputStream.closeEntry();
}
System.out.println("======解压成功=======");
} catch (IOException e) {
System.out.println("压缩包处理异常,异常信息{}" + e);
}
}
//将流写到文件中
public static void writeFile(String filePath, ZipInputStream zipInputStream) {
try (OutputStream outputStream = new FileOutputStream(filePath)) {
byte[] bytes = new byte[4096];
int len;
while ((len = zipInputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
}
} catch (IOException ex) {
System.out.println("解压文件时,写出到文件出错");
}
}
//测试方法
public static void main(String[] args) throws IOException {
String zipPath = "D:/test/测试文件.zip";
String descDir = "D:/test/解压/";
unzipFile(zipPath, descDir);
}
}
前端业务代码
使用这个工具类的案例
/**
* 此方法是导入 是导入 PVT平台 的数据
* 数据源是拷贝数据库
* 对数据不做判断,直接写入,会带有导入批次号以便区分
*
* @return
*/
@EtlLog
@PostMapping("/importExcelForPVT")
@ApiOperationSupport(order = 9)
@ApiOperation(value = "导入excel数据", notes = "传入excel文件")
public R importExcelForPVT(@RequestParam("file") MultipartFile file, @RequestParam("batchName") String batchName) throws IOException {
if (isETLRunningForPVT) {
throw new RuntimeException("正在执行ETL,请等待再次执行");
}
try {
isETLRunningForPVT = true;
List<PvtTotalBatchEntity> list = pvtTotalBatchService.list(new QueryWrapper<PvtTotalBatchEntity>().lambda().eq(PvtTotalBatchEntity::getName, batchName));
if (list.size() > 0) {
return R.fail("批次名已存在");
}
if (file.isEmpty()) {//判断文件是否为空
return R.fail("上传数据非法");
}
String fileName = file.getOriginalFilename(); //获得文件名
if (!fileName.endsWith("zip")) {
return R.fail(fileName + "不是zip压缩文件");
}
//这行代码构造了目标文件路径,将文件保存在名为"import_pvt"的目录下,文件名保持不变。
Path dest = Paths.get("files", fileName);
//保存文件在本地
try {
if (!dest.toFile().exists()) {
Files.createDirectories(dest);
}
Files.copy(file.getInputStream(), dest, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
log.error("上传文件异常," + e.getMessage());
return R.fail(fileName + ":保存文件异常");
}
String uuid = UUID.randomUUID().toString();
// String batchName = FileNameUtil.getBaseName(fileName);
Path dir = Paths.get("files", uuid);
UnzipUtils.unzipFile(dest.toFile().getAbsolutePath(), dir.toFile().getAbsolutePath());
//现在五个excel文件都在dir目录下,接下来要做的是将这五个文件合成一个excel,sheet名不变
File mergeExcelFile = MergeExcelUtil.mergeExcelFiles(dir);
//将合并后的excel文件导入数据库
ETLExecutionThreadLocal.setStartTime(System.currentTimeMillis());
String s = odsPvtService.importAllSheet(mergeExcelFile, batchName, fileName);
dwPvtServiceImpl.ODdsToDw();
//最终删除dir目录
FileUtils.deleteDirectory(dir.toFile());
//删除目录下 名为fileName的文件
Files.delete(dest);
if (s.equals("success")) {
return R.success("导入成功");
} else {
throw new RuntimeException(s);
}
} finally {
isETLRunningForPVT = false;
}
}