|
@@ -4,9 +4,13 @@ import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.collection.ListUtil;
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
-import com.github.junrar.Archive;
|
|
|
-import com.github.junrar.exception.RarException;
|
|
|
-import com.github.junrar.rarfile.FileHeader;
|
|
|
+import net.sf.sevenzipjbinding.ExtractOperationResult;
|
|
|
+import net.sf.sevenzipjbinding.IInArchive;
|
|
|
+import net.sf.sevenzipjbinding.SevenZip;
|
|
|
+import net.sf.sevenzipjbinding.SevenZipException;
|
|
|
+import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
|
|
|
+import net.sf.sevenzipjbinding.simple.ISimpleInArchive;
|
|
|
+import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem;
|
|
|
import org.apache.commons.compress.archivers.ArchiveEntry;
|
|
|
import org.apache.commons.compress.archivers.ArchiveException;
|
|
|
import org.apache.commons.compress.archivers.ArchiveInputStream;
|
|
@@ -19,7 +23,6 @@ import java.io.*;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
import java.nio.file.Paths;
|
|
|
-import java.util.ArrayList;
|
|
|
import java.util.Enumeration;
|
|
|
import java.util.List;
|
|
|
|
|
@@ -93,40 +96,47 @@ public class ExcelUtil {
|
|
|
return filePathList;
|
|
|
}
|
|
|
|
|
|
- public static List<String> extractRar(String inputFilePath, String outputDirPath) throws IOException {
|
|
|
- List<String> fileList = new ArrayList<>();
|
|
|
- // 创建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 (RarException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
- return fileList;
|
|
|
- }
|
|
|
+// public static List<String> extractRar(String inputFilePath, String outputDirPath) throws IOException {
|
|
|
+// List<String> fileList = new ArrayList<>();
|
|
|
+// // 创建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 (RarException e) {
|
|
|
+// try {
|
|
|
+// List<File> extract = Junrar.extract(new File(inputFilePath), new File(outputDirPath));
|
|
|
+// for (File file : extract) {
|
|
|
+// fileList.add(file.getAbsolutePath());
|
|
|
+// }
|
|
|
+// } catch (RarException ex) {
|
|
|
+// throw new RuntimeException(ex);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// return fileList;
|
|
|
+// }
|
|
|
|
|
|
public static List<String> extractSplitZip(String zipFilePath, String destFilePath) throws IOException {
|
|
|
List<String> resultList = ListUtil.list(false);
|
|
@@ -147,12 +157,78 @@ public class ExcelUtil {
|
|
|
return resultList;
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) throws IOException, ArchiveException {
|
|
|
- String zipFilePath = "1.zip";
|
|
|
- String destFilePath = "xx";
|
|
|
- List<String> fileList = extractCompressedFiles(zipFilePath, destFilePath);
|
|
|
- for (String s : fileList) {
|
|
|
- System.out.println(s);
|
|
|
+ public static List<String> extractRar5(String rarFilePath, String outputDir) throws Exception {
|
|
|
+ // 初始化 SevenZipJBinding 本地库
|
|
|
+ SevenZip.initSevenZipFromPlatformJAR();
|
|
|
+
|
|
|
+ RandomAccessFile randomAccessFile = null;
|
|
|
+ IInArchive inArchive = null;
|
|
|
+
|
|
|
+ List<String> resultList = ListUtil.list(false);
|
|
|
+ try {
|
|
|
+ // 打开 RAR 文件
|
|
|
+ randomAccessFile = new RandomAccessFile(rarFilePath, "r");
|
|
|
+ inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile));
|
|
|
+
|
|
|
+ // 获取压缩包中的文件列表
|
|
|
+ ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();
|
|
|
+ for (ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
|
|
|
+ if (!item.isFolder()) {
|
|
|
+ resultList.add(extractItem(item, outputDir));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ // 释放资源
|
|
|
+ if (inArchive != null) {
|
|
|
+ inArchive.close();
|
|
|
+ }
|
|
|
+ if (randomAccessFile != null) {
|
|
|
+ randomAccessFile.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return resultList;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String extractItem(ISimpleInArchiveItem item, String outputDir) throws SevenZipException {
|
|
|
+ String filePath = outputDir + File.separator + item.getPath();
|
|
|
+ File outputFile = new File(filePath);
|
|
|
+
|
|
|
+ // 创建父目录
|
|
|
+ File parentDir = outputFile.getParentFile();
|
|
|
+ if (!parentDir.exists() && !parentDir.mkdirs()) {
|
|
|
+ throw new SevenZipException("无法创建目录: " + parentDir.getAbsolutePath());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 提取文件内容
|
|
|
+ try (FileOutputStream fos = new FileOutputStream(outputFile)) {
|
|
|
+ ExtractOperationResult result = item.extractSlow(data -> {
|
|
|
+ try {
|
|
|
+ fos.write(data);
|
|
|
+ return data.length; // 返回写入的字节数
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new SevenZipException("写入文件失败", e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ if (result != ExtractOperationResult.OK) {
|
|
|
+ throw new SevenZipException("解压失败: " + result);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new SevenZipException("文件操作失败", e);
|
|
|
+ }
|
|
|
+ return filePath;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ String zipFilePath = "D:\\home\\wwwroot\\mo_report_file\\wangzaijun@simuwang.com\\20250321\\20250321143709排排网确认单.rar";
|
|
|
+ String destFilePath = "D:\\home\\wwwroot\\mo_report_file\\wangzaijun@simuwang.com\\20250321";
|
|
|
+ List<String> strings = extractRar5(zipFilePath, destFilePath);
|
|
|
+ for (String string : strings) {
|
|
|
+
|
|
|
}
|
|
|
+// List<String> fileList = extractCompressedFiles(zipFilePath, destFilePath);
|
|
|
+// for (String s : fileList) {
|
|
|
+// System.out.println(s);
|
|
|
+// }
|
|
|
}
|
|
|
}
|