|
@@ -1,6 +1,5 @@
|
|
|
package com.smppw.modaq.infrastructure.util;
|
|
|
|
|
|
-import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.collection.ListUtil;
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
@@ -16,17 +15,11 @@ import org.apache.commons.compress.archivers.ArchiveEntry;
|
|
|
import org.apache.commons.compress.archivers.ArchiveException;
|
|
|
import org.apache.commons.compress.archivers.ArchiveInputStream;
|
|
|
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
|
|
|
-import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
|
|
-import org.apache.commons.compress.archivers.zip.ZipFile;
|
|
|
-import org.apache.commons.io.IOUtils;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.nio.file.Files;
|
|
|
-import java.nio.file.Path;
|
|
|
-import java.nio.file.Paths;
|
|
|
import java.util.Arrays;
|
|
|
-import java.util.Enumeration;
|
|
|
import java.util.List;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
@@ -78,8 +71,6 @@ public class ExcelUtil {
|
|
|
}
|
|
|
|
|
|
public static List<String> extractCompressedFiles(String zipFilePath, String destFilePath) throws IOException, ArchiveException {
|
|
|
- List<String> filePathList = CollUtil.newArrayList();
|
|
|
-
|
|
|
File destFile = FileUtil.file(destFilePath);
|
|
|
if (!destFile.exists()) {
|
|
|
Files.createDirectories(destFile.toPath());
|
|
@@ -90,178 +81,7 @@ public class ExcelUtil {
|
|
|
encoding = "GBK";
|
|
|
}
|
|
|
|
|
|
- try (BufferedInputStream fis = new BufferedInputStream(new FileInputStream(zipFilePath));
|
|
|
- ArchiveInputStream<? extends ArchiveEntry> ais = new ArchiveStreamFactory()
|
|
|
- .createArchiveInputStream(ArchiveStreamFactory.detect(fis), fis, encoding)) {
|
|
|
- ArchiveEntry entry;
|
|
|
- while ((entry = ais.getNextEntry()) != null) {
|
|
|
- String name = entry.getName();
|
|
|
- if (entry.isDirectory()) {
|
|
|
- File entryFile = FileUtil.file(destFilePath, name);
|
|
|
- Files.createDirectories(entryFile.toPath());
|
|
|
- } else {
|
|
|
- if (name.startsWith("__MACOSX/")) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- String zipFilename = FileUtil.getName(destFilePath);
|
|
|
- if (zipFilename.contains("确认") && !name.contains("确认")) {
|
|
|
- String ext = FileUtil.extName(name);
|
|
|
- name = StrUtil.subBefore(name, ".", true);
|
|
|
- name = name + "_确认单." + ext;
|
|
|
- }
|
|
|
- File entryFile = FileUtil.file(destFilePath, name);
|
|
|
- try (FileOutputStream fos = new FileOutputStream(entryFile)) {
|
|
|
- IOUtils.copy(ais, fos);
|
|
|
- filePathList.add(entryFile.getPath());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- if (e.getMessage() != null
|
|
|
- && (e.getMessage().contains("split")
|
|
|
- || e.getMessage().contains("volume"))) {
|
|
|
- filePathList.addAll(extractSplitZip(zipFilePath, destFilePath, encoding));
|
|
|
- } else {
|
|
|
- throw e;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return filePathList;
|
|
|
-// return extractCompressedFiles(zipFilePath, destFilePath, 0, new HashSet<>());
|
|
|
- }
|
|
|
-
|
|
|
-// /**
|
|
|
-// * 递归解压压缩文件(支持嵌套)
|
|
|
-// *
|
|
|
-// * @param zipFilePath 压缩文件路径
|
|
|
-// * @param destDir 目标目录
|
|
|
-// * @param depth 当前递归深度(防止无限递归)
|
|
|
-// * @param processed 已处理的文件哈希(防止重复解压)
|
|
|
-// */
|
|
|
-// private static List<String> extractCompressedFiles(String zipFilePath, String destDir, int depth, Set<String> processed) throws IOException, ArchiveException {
|
|
|
-// // 防御:限制递归深度防止栈溢出
|
|
|
-// if (depth > 4) {
|
|
|
-// throw new ArchiveException("Maximum recursion depth (4) exceeded");
|
|
|
-// }
|
|
|
-//
|
|
|
-// String encoding = detectEncoding(zipFilePath); // 编码检测
|
|
|
-// if (encoding == null) {
|
|
|
-// encoding = "GBK";
|
|
|
-// }
|
|
|
-// List<String> extractedFiles = ListUtil.list(false);
|
|
|
-// File destDirFile = FileUtil.mkdir(destDir);
|
|
|
-//
|
|
|
-// try (InputStream fis = new BufferedInputStream(Files.newInputStream(Paths.get(zipFilePath)));
|
|
|
-// ArchiveInputStream<?> ais = createArchiveInputStream(fis, encoding)) {
|
|
|
-// ArchiveEntry entry;
|
|
|
-// while ((entry = ais.getNextEntry()) != null) {
|
|
|
-// String entryName = sanitizeEntryName(entry.getName()); // 清理非法字符
|
|
|
-// if (entryName.startsWith("__MACOSX/")) {
|
|
|
-// continue;
|
|
|
-// }
|
|
|
-// String zipFilename = FileUtil.getName(destDir);
|
|
|
-// if (zipFilename.contains("确认") && !entryName.contains("确认")) {
|
|
|
-// String ext = FileUtil.extName(entryName);
|
|
|
-// entryName = StrUtil.subBefore(entryName, ".", true);
|
|
|
-// entryName = entryName + "_确认单." + ext;
|
|
|
-// }
|
|
|
-// File entryFile = new File(destDirFile, entryName);
|
|
|
-// if (entry.isDirectory()) {
|
|
|
-// Files.createDirectories(entryFile.toPath());
|
|
|
-// } else {
|
|
|
-// extractSingleFile(ais, entryFile);
|
|
|
-// if (isArchiveFile(entryFile)) { // 判断是否为压缩文件
|
|
|
-// extractedFiles.addAll(processNestedArchive(entryFile, destDir, depth, processed));
|
|
|
-// } else {
|
|
|
-// extractedFiles.add(entryFile.getAbsolutePath());
|
|
|
-// }
|
|
|
-// }
|
|
|
-// }
|
|
|
-// } catch (Exception e) {
|
|
|
-// handleArchiveException(e, zipFilePath, destDir, extractedFiles, encoding);
|
|
|
-// }
|
|
|
-// return extractedFiles;
|
|
|
-// }
|
|
|
-//
|
|
|
-// // ----------- 辅助方法 -----------
|
|
|
-// private static ArchiveInputStream<?> createArchiveInputStream(InputStream is, String encoding) throws ArchiveException {
|
|
|
-// return new ArchiveStreamFactory(encoding)
|
|
|
-// .createArchiveInputStream(ArchiveStreamFactory.detect(is), is);
|
|
|
-// }
|
|
|
-//
|
|
|
-// private static void extractSingleFile(ArchiveInputStream<?> ais, File targetFile) throws IOException {
|
|
|
-// try (OutputStream fos = Files.newOutputStream(targetFile.toPath())) {
|
|
|
-// IOUtils.copy(ais, fos);
|
|
|
-// } finally {
|
|
|
-// if (ais != null) {
|
|
|
-// ais.close();
|
|
|
-// }
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// private static List<String> processNestedArchive(File archiveFile, String parentDir, int depth, Set<String> processed) throws IOException, ArchiveException {
|
|
|
-// // 强制关闭所有可能关联的流(防御性编程)
|
|
|
-// System.gc(); // 触发垃圾回收释放潜在未关闭的流
|
|
|
-//
|
|
|
-// String fileHash = DigestUtil.md5Hex(new FileInputStream(archiveFile)); // 文件哈希校验(需实现)
|
|
|
-// if (processed.contains(fileHash)) {
|
|
|
-// return ListUtil.empty(); // 避免重复解压相同文件
|
|
|
-// }
|
|
|
-// processed.add(fileHash);
|
|
|
-//
|
|
|
-// String nestedDestDir = parentDir + File.separator + FileUtil.mainName(archiveFile.getName());
|
|
|
-// List<String> nestedFiles = extractCompressedFiles(
|
|
|
-// archiveFile.getAbsolutePath(),
|
|
|
-// nestedDestDir,
|
|
|
-// depth + 1,
|
|
|
-// processed
|
|
|
-// );
|
|
|
-//
|
|
|
-// Files.delete(archiveFile.toPath()); // 删除原压缩包
|
|
|
-// return nestedFiles;
|
|
|
-// }
|
|
|
-//
|
|
|
-// private static boolean isArchiveFile(File file) {
|
|
|
-// try (InputStream is = Files.newInputStream(file.toPath());
|
|
|
-// BufferedInputStream bis = new BufferedInputStream(is)) {
|
|
|
-// ArchiveStreamFactory.detect(bis); // 通过文件头检测
|
|
|
-// return true;
|
|
|
-// } catch (Exception e) {
|
|
|
-// return false;
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 清理非法路径字符(防御路径穿越攻击)
|
|
|
-// private static String sanitizeEntryName(String name) {
|
|
|
-// return name.replaceAll("[:\\\\/*\"?|<>]", "_");
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 统一异常处理
|
|
|
-// private static void handleArchiveException(Exception e, String zipFile, String destDir, List<String> extractedFiles, String encoding) throws ArchiveException, IOException {
|
|
|
-// if (e.getMessage().contains("split")) {
|
|
|
-// extractedFiles.addAll(extractSplitZip(zipFile, destDir, encoding));
|
|
|
-// } else {
|
|
|
-// throw new ArchiveException("Failed to extract " + zipFile, e);
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
- public static List<String> extractSplitZip(String zipFilePath, String destFilePath, String encoding) throws IOException {
|
|
|
- List<String> resultList = ListUtil.list(false);
|
|
|
- File file = new File(zipFilePath);
|
|
|
- try (ZipFile zipFile = ZipFile.builder().setFile(file).setCharset(encoding).get()) {
|
|
|
- Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
|
|
|
- while (entries.hasMoreElements()) {
|
|
|
- ZipArchiveEntry entry = entries.nextElement();
|
|
|
- // 解压到目标目录
|
|
|
- try (InputStream is = zipFile.getInputStream(entry)) {
|
|
|
- Path path = Paths.get(destFilePath, entry.getName());
|
|
|
- FileUtil.del(path);
|
|
|
- Files.copy(is, path);
|
|
|
- resultList.add(path.toAbsolutePath().toString());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return resultList;
|
|
|
+ return ZipUtil.decompressZip(zipFilePath, destFilePath, 2, encoding);
|
|
|
}
|
|
|
|
|
|
public static List<String> extractRar5(String rarFilePath, String outputDir) throws Exception {
|