Преглед на файлове

fix:修复rar5无法解压的问题

wangzaijun преди 1 месец
родител
ревизия
15f061e976

+ 5 - 5
mo-daq/pom.xml

@@ -92,11 +92,11 @@
             <artifactId>commons-compress</artifactId>
             <version>1.27.1</version>
         </dependency>
-        <dependency>
-            <groupId>com.github.junrar</groupId>
-            <artifactId>junrar</artifactId>
-            <version>7.5.1</version>
-        </dependency>
+<!--        <dependency>-->
+<!--            <groupId>com.github.junrar</groupId>-->
+<!--            <artifactId>junrar</artifactId>-->
+<!--            <version>7.5.1</version>-->
+<!--        </dependency>-->
         <dependency>
             <groupId>net.sf.sevenzipjbinding</groupId>
             <artifactId>sevenzipjbinding</artifactId>

+ 0 - 2
mo-daq/src/main/java/com/smppw/modaq/domain/dto/report/ReportFundInfoDTO.java

@@ -57,9 +57,7 @@ public class ReportFundInfoDTO extends BaseReportDTO<ReportFundInfoDO> {
         return "{" +
                 super.toString() +
                 ", fundName='" + fundName + '\'' +
-                ", fundCode='" + fundCode + '\'' +
                 ", companyName='" + companyName + '\'' +
-                ", currency='" + currency + '\'' +
                 '}';
     }
 }

+ 11 - 0
mo-daq/src/main/java/com/smppw/modaq/domain/dto/report/ReportFundTransactionDTO.java

@@ -290,4 +290,15 @@ public class ReportFundTransactionDTO extends BaseReportDTO<ReportFundTransactio
         this.initEntity(entity);
         return entity;
     }
+
+    @Override
+    public String toString() {
+        return "{" +
+                super.toString() +
+                ", transactionType='" + transactionType + '\'' +
+                ", holdingDate='" + holdingDate + '\'' +
+                ", amount='" + amount + '\'' +
+                ", share='" + share + '\'' +
+                '}';
+    }
 }

+ 10 - 0
mo-daq/src/main/java/com/smppw/modaq/domain/dto/report/ReportInvestorInfoDTO.java

@@ -50,4 +50,14 @@ public class ReportInvestorInfoDTO extends BaseReportDTO<ReportInvestorInfoDO> {
         this.initEntity(entity);
         return entity;
     }
+
+    @Override
+    public String toString() {
+        return "{" +
+                super.toString() +
+                ", investorName='" + investorName + '\'' +
+                ", certificateNumber='" + certificateNumber + '\'' +
+                ", tradingAccount='" + tradingAccount + '\'' +
+                '}';
+    }
 }

+ 3 - 3
mo-daq/src/main/java/com/smppw/modaq/domain/service/EmailParseService.java

@@ -152,7 +152,7 @@ public class EmailParseService {
         }
     }
 
-    public List<EmailZipFileDTO> parseZipEmail(EmailContentInfoDTO emailContentInfoDTO) throws ArchiveException, IOException {
+    public List<EmailZipFileDTO> parseZipEmail(EmailContentInfoDTO emailContentInfoDTO) throws Exception {
         List<EmailZipFileDTO> resultList = ListUtil.list(false);
         Integer emailType = emailContentInfoDTO.getEmailType();
         String filepath = emailContentInfoDTO.getFilePath();
@@ -166,7 +166,7 @@ public class EmailParseService {
         return resultList;
     }
 
-    private void handleCompressedFiles(String filepath, String extension, Integer emailType, List<EmailZipFileDTO> resultList) throws IOException, ArchiveException {
+    private void handleCompressedFiles(String filepath, String extension, Integer emailType, List<EmailZipFileDTO> resultList) throws Exception {
         String destPath = getDestinationPath(filepath, extension);
         log.info("压缩包地址:{}, 解压后文件地址:{}", filepath, destPath);
 
@@ -181,7 +181,7 @@ public class EmailParseService {
         if (ExcelUtil.isZip(filepath)) {
             extractedDirs = ExcelUtil.extractCompressedFiles(filepath, destPath);
         } else if (ExcelUtil.isRAR(filepath)) {
-            extractedDirs = ExcelUtil.extractRar(filepath, destPath);
+            extractedDirs = ExcelUtil.extractRar5(filepath, destPath);
         } else {
             return;
         }

+ 120 - 44
mo-daq/src/main/java/com/smppw/modaq/infrastructure/util/ExcelUtil.java

@@ -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);
+//        }
     }
 }