104 lines
3.7 KiB
Java
104 lines
3.7 KiB
Java
|
|
package com.engine.secret.service.impl;
|
||
|
|
|
||
|
|
import com.engine.core.impl.Service;
|
||
|
|
import com.engine.secret.exception.CustomizeRunTimeException;
|
||
|
|
import com.engine.secret.service.QualificationApplicationService;
|
||
|
|
import com.wbi.util.Util;
|
||
|
|
import net.lingala.zip4j.ZipFile;
|
||
|
|
import net.lingala.zip4j.exception.ZipException;
|
||
|
|
import net.lingala.zip4j.model.FileHeader;
|
||
|
|
import org.apache.commons.lang.StringUtils;
|
||
|
|
import weaver.file.ImageFileManager;
|
||
|
|
|
||
|
|
import java.io.IOException;
|
||
|
|
import java.io.InputStream;
|
||
|
|
import java.nio.file.Files;
|
||
|
|
import java.nio.file.Path;
|
||
|
|
import java.nio.file.Paths;
|
||
|
|
import java.nio.file.StandardCopyOption;
|
||
|
|
import java.util.HashMap;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Map;
|
||
|
|
import java.util.Optional;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @author:dxfeng
|
||
|
|
* @createTime: 2024/08/29
|
||
|
|
* @version: 1.0
|
||
|
|
*/
|
||
|
|
public class QualificationApplicationServiceImpl extends Service implements QualificationApplicationService {
|
||
|
|
@Override
|
||
|
|
public Map<String, Object> parsingFiles(Map<String, Object> param) {
|
||
|
|
try {
|
||
|
|
int fileId = 1627;
|
||
|
|
String imageId = Util.null2String(param.get("imageId"));
|
||
|
|
if (StringUtils.isBlank(imageId)) {
|
||
|
|
throw new CustomizeRunTimeException("文件获取失败,请确认文件是否上传");
|
||
|
|
}
|
||
|
|
Map<String, Object> returnMap = new HashMap<>();
|
||
|
|
// 根据文件id获取文件流
|
||
|
|
ImageFileManager manager = new ImageFileManager();
|
||
|
|
manager.getImageFileInfoById(fileId);
|
||
|
|
manager.getImageFileName();
|
||
|
|
InputStream inputStream = manager.getInputStream();
|
||
|
|
Path tempZipFile = Files.createTempFile("temp", ".zip");
|
||
|
|
Files.copy(inputStream, tempZipFile, StandardCopyOption.REPLACE_EXISTING);
|
||
|
|
|
||
|
|
String password = "zizhi102!";
|
||
|
|
unzipWithPassword(tempZipFile, Paths.get("output"), password);
|
||
|
|
return returnMap;
|
||
|
|
} catch (Exception e) {
|
||
|
|
throw new CustomizeRunTimeException("文件解析失败", e);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
private static void unzipWithPassword(Path zipFilePath, Path outputDir, String password) {
|
||
|
|
try {
|
||
|
|
ZipFile zipFile = new ZipFile(zipFilePath.toFile());
|
||
|
|
if (zipFile.isEncrypted()) {
|
||
|
|
zipFile.setPassword(password.toCharArray()); // 设置密码
|
||
|
|
}
|
||
|
|
|
||
|
|
// 遍历并解压所有文件
|
||
|
|
zipFile.extractAll(outputDir.toString());
|
||
|
|
System.out.println("解压成功");
|
||
|
|
// 12345678_资质申请附件/(database)data.json
|
||
|
|
|
||
|
|
// 存储文件
|
||
|
|
|
||
|
|
// 遍历 ZIP 内文件(可选)
|
||
|
|
List<FileHeader> fileHeaders = zipFile.getFileHeaders();
|
||
|
|
|
||
|
|
Optional<String> commonRoot = fileHeaders.stream()
|
||
|
|
.map(FileHeader::getFileName)
|
||
|
|
.filter(path -> !path.trim().isEmpty())
|
||
|
|
.map(path -> path.split("/")[0]) // 取第一级目录或文件名
|
||
|
|
.distinct()
|
||
|
|
.reduce((a, b) -> a.equals(b) ? a : null);
|
||
|
|
String rootPath = commonRoot.orElse(null);
|
||
|
|
Map<String,FileHeader> fileHeaderMap = new HashMap<>();
|
||
|
|
|
||
|
|
for (FileHeader header : fileHeaders) {
|
||
|
|
System.out.println("文件: " + header.getFileName());
|
||
|
|
fileHeaderMap.put(header.getFileName(),header);
|
||
|
|
}
|
||
|
|
// 遍历配置表
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
} catch (ZipException e) {
|
||
|
|
if (e.getMessage().contains("Wrong password")) {
|
||
|
|
System.err.println("密码错误");
|
||
|
|
} else {
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
} finally {
|
||
|
|
try {
|
||
|
|
Files.deleteIfExists(zipFilePath);
|
||
|
|
} catch (IOException e) {
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|