|
@@ -16,11 +16,9 @@ import org.apache.commons.compress.archivers.zip.ZipFile;
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
|
|
import java.io.*;
|
|
import java.io.*;
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.nio.file.Paths;
|
|
-import java.nio.file.StandardCopyOption;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.Enumeration;
|
|
import java.util.Enumeration;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
@@ -49,37 +47,34 @@ public class ExcelUtil {
|
|
public static List<String> extractCompressedFiles(String zipFilePath, String destFilePath) throws IOException, ArchiveException {
|
|
public static List<String> extractCompressedFiles(String zipFilePath, String destFilePath) throws IOException, ArchiveException {
|
|
List<String> filePathList = CollUtil.newArrayList();
|
|
List<String> filePathList = CollUtil.newArrayList();
|
|
|
|
|
|
- File destFile = FileUtil.file(destFilePath).getCanonicalFile();
|
|
|
|
|
|
+ File destFile = FileUtil.file(destFilePath);
|
|
if (!destFile.exists()) {
|
|
if (!destFile.exists()) {
|
|
Files.createDirectories(destFile.toPath());
|
|
Files.createDirectories(destFile.toPath());
|
|
}
|
|
}
|
|
|
|
|
|
try (BufferedInputStream fis = new BufferedInputStream(new FileInputStream(zipFilePath));
|
|
try (BufferedInputStream fis = new BufferedInputStream(new FileInputStream(zipFilePath));
|
|
- ArchiveInputStream<? extends ArchiveEntry> ais = new ArchiveStreamFactory("UTF-8")
|
|
|
|
- .createArchiveInputStream(ArchiveStreamFactory.ZIP, fis)) {
|
|
|
|
|
|
+ ArchiveInputStream<? extends ArchiveEntry> ais = new ArchiveStreamFactory().createArchiveInputStream(fis)) {
|
|
ArchiveEntry entry;
|
|
ArchiveEntry entry;
|
|
int i = 1;
|
|
int i = 1;
|
|
while ((entry = ais.getNextEntry()) != null) {
|
|
while ((entry = ais.getNextEntry()) != null) {
|
|
- // 跳过不可读条目(如加密文件)
|
|
|
|
- if (!ais.canReadEntryData(entry)) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
String name = entry.getName();
|
|
String name = entry.getName();
|
|
- File entryFile = FileUtil.file(destFilePath, name);
|
|
|
|
-
|
|
|
|
- // 安全检查
|
|
|
|
- if (!entryFile.toPath().normalize().startsWith(destFile.toPath())) {
|
|
|
|
- throw new IOException("非法路径: " + name);
|
|
|
|
|
|
+ File entryFile;
|
|
|
|
+ try {
|
|
|
|
+ entryFile = FileUtil.file(destFilePath, name);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ String zipFilename = FileUtil.getName(destFilePath);
|
|
|
|
+ String ext = FileUtil.extName(name);
|
|
|
|
+ name = zipFilename + "_" + i + "." + ext;
|
|
|
|
+ entryFile = FileUtil.file(destFilePath, name);
|
|
|
|
+ i++;
|
|
}
|
|
}
|
|
-
|
|
|
|
if (entry.isDirectory()) {
|
|
if (entry.isDirectory()) {
|
|
Files.createDirectories(entryFile.toPath());
|
|
Files.createDirectories(entryFile.toPath());
|
|
} else {
|
|
} else {
|
|
- Files.createDirectories(entryFile.getParentFile().toPath());
|
|
|
|
- try (OutputStream fos = Files.newOutputStream(entryFile.toPath())) {
|
|
|
|
|
|
+ try (FileOutputStream fos = new FileOutputStream(entryFile)) {
|
|
IOUtils.copy(ais, fos);
|
|
IOUtils.copy(ais, fos);
|
|
|
|
+ filePathList.add(entryFile.getPath());
|
|
}
|
|
}
|
|
- filePathList.add(entryFile.getPath());
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -97,55 +92,39 @@ public class ExcelUtil {
|
|
|
|
|
|
public static List<String> extractRar(String inputFilePath, String outputDirPath) throws IOException {
|
|
public static List<String> extractRar(String inputFilePath, String outputDirPath) throws IOException {
|
|
List<String> fileList = new ArrayList<>();
|
|
List<String> fileList = new ArrayList<>();
|
|
- Path outputPath = Paths.get(outputDirPath).toRealPath();
|
|
|
|
-
|
|
|
|
- try (FileInputStream fis = new FileInputStream(inputFilePath);
|
|
|
|
- Archive archive = new Archive(fis)) { // 假设Archive支持try-with-resources
|
|
|
|
-
|
|
|
|
- for (FileHeader entry : archive.getFileHeaders()) {
|
|
|
|
- // 跳过目录条目(由文件创建逻辑自动生成目录)
|
|
|
|
- if (entry.isDirectory()) continue;
|
|
|
|
-
|
|
|
|
- // 处理文件名编码和路径
|
|
|
|
- String fileName = new String(entry.getFileNameByteArray(), StandardCharsets.UTF_8);
|
|
|
|
- Path targetPath = outputPath.resolve(fileName).normalize();
|
|
|
|
-
|
|
|
|
- // 路径安全检查
|
|
|
|
- if (!targetPath.startsWith(outputPath)) {
|
|
|
|
- throw new IOException("检测到非法路径: " + fileName);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 创建父目录
|
|
|
|
- Files.createDirectories(targetPath.getParent());
|
|
|
|
-
|
|
|
|
- // 处理文件重名
|
|
|
|
- targetPath = resolveDuplicate(targetPath);
|
|
|
|
-
|
|
|
|
- // 解压文件内容
|
|
|
|
- try (InputStream entryStream = archive.getInputStream(entry)) {
|
|
|
|
- Files.copy(entryStream, targetPath, StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
- fileList.add(targetPath.toAbsolutePath().toString());
|
|
|
|
|
|
+ // 创建Archive对象,用于读取rar压缩文件格式
|
|
|
|
+ try {
|
|
|
|
+ String zipFilename = FileUtil.getName(inputFilePath);
|
|
|
|
+ Archive archive = new Archive(new FileInputStream(inputFilePath));
|
|
|
|
+ // 读取压缩文件中的所有子目录或子文件(FileHeader对象)
|
|
|
|
+ List<FileHeader> fileHeaderList = archive.getFileHeaders();
|
|
|
|
+ int i = 1;
|
|
|
|
+ // 遍历子目录和子文件
|
|
|
|
+ for (FileHeader fd : fileHeaderList) {
|
|
|
|
+ String fileName = fd.getFileName();
|
|
|
|
+ String ext = FileUtil.extName(fileName);
|
|
|
|
+ fileName = zipFilename + "_" + i + "." + ext;
|
|
|
|
+ i++;
|
|
|
|
+ File f = FileUtil.file(outputDirPath + File.separator + fileName);
|
|
|
|
+ if (fd.isDirectory()) {
|
|
|
|
+ // 创建新子目录
|
|
|
|
+ Files.createDirectories(f.toPath());
|
|
|
|
+ } else {
|
|
|
|
+ // 创建新子文件
|
|
|
|
+ Files.createFile(f.toPath());
|
|
|
|
+ // 获取压缩包中的子文件输出流
|
|
|
|
+ InputStream in = archive.getInputStream(fd);
|
|
|
|
+ // 复制文件输入流至新子文件
|
|
|
|
+ FileUtil.copyFile(in, f);
|
|
|
|
+ fileList.add(f.getAbsolutePath());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- } catch (UnsupportedEncodingException | RarException e) {
|
|
|
|
- throw new IOException("解析RAR文件失败", e);
|
|
|
|
|
|
+ } catch (RarException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
}
|
|
}
|
|
return fileList;
|
|
return fileList;
|
|
}
|
|
}
|
|
|
|
|
|
- private static Path resolveDuplicate(Path path) {
|
|
|
|
- if (!Files.exists(path)) return path;
|
|
|
|
- String base = path.getFileName().toString();
|
|
|
|
- String name = base.replaceFirst("[.][^.]+$", "");
|
|
|
|
- String ext = base.substring(name.length());
|
|
|
|
- int counter = 1;
|
|
|
|
- while (true) {
|
|
|
|
- Path newPath = path.resolveSibling(name + "_" + counter + ext);
|
|
|
|
- if (!Files.exists(newPath)) return newPath;
|
|
|
|
- counter++;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
public static List<String> extractSplitZip(String zipFilePath, String destFilePath) throws IOException {
|
|
public static List<String> extractSplitZip(String zipFilePath, String destFilePath) throws IOException {
|
|
List<String> resultList = ListUtil.list(false);
|
|
List<String> resultList = ListUtil.list(false);
|
|
File file = new File(zipFilePath);
|
|
File file = new File(zipFilePath);
|