فهرست منبع

fix: 文件类型增加RAR类型

chenjianhua 6 ماه پیش
والد
کامیت
ffcc611d96

+ 21 - 1
service-base/pom.xml

@@ -172,7 +172,27 @@
             <groupId>jakarta.servlet</groupId>
             <artifactId>jakarta.servlet-api</artifactId>
         </dependency>
-
+        <!-- 压缩文件jar -->
+        <dependency>
+            <groupId>com.github.junrar</groupId>
+            <artifactId>junrar</artifactId>
+            <version>7.5.1</version>
+        </dependency>
+        <dependency>
+            <groupId>net.sf.sevenzipjbinding</groupId>
+            <artifactId>sevenzipjbinding</artifactId>
+            <version>16.02-2.01</version>
+        </dependency>
+        <dependency>
+            <groupId>net.sf.sevenzipjbinding</groupId>
+            <artifactId>sevenzipjbinding-all-platforms</artifactId>
+            <version>16.02-2.01</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.ant</groupId>
+            <artifactId>ant</artifactId>
+            <version>1.9.6</version>
+        </dependency>
         <!-- jjwt依赖包 -->
         <dependency>
             <groupId>com.auth0</groupId>

+ 1 - 1
service-base/src/main/java/com/simuwang/base/common/util/EmailUtil.java

@@ -121,7 +121,7 @@ public class EmailUtil {
         if (StrUtil.isBlank(fileName)) {
             return false;
         }
-        return ExcelUtil.isZip(fileName) || ExcelUtil.isExcel(fileName) || ExcelUtil.isPdf(fileName) || ExcelUtil.isHTML(fileName);
+        return ExcelUtil.isZip(fileName) || ExcelUtil.isExcel(fileName) || ExcelUtil.isPdf(fileName) || ExcelUtil.isHTML(fileName) || ExcelUtil.isRAR(fileName);
     }
 
     /**

+ 38 - 4
service-base/src/main/java/com/simuwang/base/common/util/ExcelUtil.java

@@ -5,9 +5,12 @@ import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.exceptions.ExceptionUtil;
 import cn.hutool.core.lang.Pair;
 import cn.hutool.core.util.StrUtil;
+import com.github.junrar.Archive;
+import com.github.junrar.exception.RarException;
+import com.github.junrar.rarfile.FileHeader;
 import com.simuwang.base.common.conts.DateConst;
 import com.simuwang.base.common.conts.EmailDataDirectionConst;
-import com.simuwang.base.pojo.dto.EmailContentInfoDTO;
+import org.apache.commons.io.FileUtils;
 import org.apache.pdfbox.Loader;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.poi.hssf.usermodel.*;
@@ -26,9 +29,7 @@ import java.nio.file.Paths;
 import java.text.NumberFormat;
 import java.time.LocalDate;
 import java.time.format.DateTimeFormatter;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 import org.apache.commons.compress.archivers.ArchiveEntry;
 import org.apache.commons.compress.archivers.ArchiveInputStream;
@@ -59,6 +60,9 @@ public class ExcelUtil {
     public static boolean isHTML(String fileName) {
         return StrUtil.isNotBlank(fileName) && fileName.endsWith("html");
     }
+    public static boolean isRAR(String fileName) {
+        return StrUtil.isNotBlank(fileName) && (fileName.endsWith("rar") || fileName.endsWith("RAR"));
+    }
 
     public static Sheet getSheet(File file, int sheetIndex) {
         if (file == null || !file.exists()) {
@@ -245,6 +249,36 @@ public class ExcelUtil {
         return filePathList;
     }
 
+    public static List<String> extractRar(String inputFilePath, String outputDirPath){
+        List<String> fileList = new ArrayList<>();
+        // 创建Archive对象,用于读取rar压缩文件格式
+        try{
+            Archive archive = new Archive(new FileInputStream(inputFilePath));
+            // 读取压缩文件中的所有子目录或子文件(FileHeader对象)
+            List<FileHeader> fileHeaderList = archive.getFileHeaders();
+            // 遍历子目录和子文件
+            for (FileHeader fd:fileHeaderList) {
+                System.out.println(fd.getFileName());
+                File f = new File(outputDirPath+"/"+fd.getFileName());
+                if(fd.isDirectory()){
+                    // 创建新子目录
+                    f.mkdirs();
+                }else{
+                    // 创建新子文件
+                    f.createNewFile();
+                    // 获取压缩包中的子文件输出流
+                    InputStream in = archive.getInputStream(fd);
+                    // 复制文件输入流至新子文件
+                    FileUtils.copyInputStreamToFile(in,f);
+                    fileList.add(f.getAbsolutePath());
+                }
+            }
+            } catch (Exception e) {
+                logger.error(e.getMessage(),e);
+            }
+        return fileList;
+    }
+
     public static void writeDataToSheet(Sheet sheet, Elements rows) {
         int rowSize = rows.size();
         for (int rowNum = 0; rowNum < rowSize; rowNum++) {

+ 33 - 10
service-daq/src/main/java/com/simuwang/daq/service/NavEmailParser.java

@@ -79,7 +79,7 @@ public class NavEmailParser extends AbstractEmailParser {
             Optional.ofNullable(fundNavDTOList).ifPresent(emailFundNavDTOList::addAll);
         }
         // 4.解析邮件zip,rar附件
-        if (StrUtil.isNotBlank(emailContentInfoDTO.getFilePath()) && ExcelUtil.isZip(emailContentInfoDTO.getFileName())) {
+        if (StrUtil.isNotBlank(emailContentInfoDTO.getFilePath()) && (ExcelUtil.isZip(emailContentInfoDTO.getFileName()) || ExcelUtil.isRAR(emailContentInfoDTO.getFileName()))) {
             List<EmailFundNavDTO> fundNavDTOList = parsePackageFile(emailContentInfoDTO, emailContentInfoDTO.getFileName(), emailContentInfoDTO.getFilePath(), emailFieldMap);
             Optional.ofNullable(fundNavDTOList).ifPresent(emailFundNavDTOList::addAll);
         }
@@ -105,18 +105,41 @@ public class NavEmailParser extends AbstractEmailParser {
     }
 
     private List<EmailFundNavDTO> parsePackageFile(EmailContentInfoDTO emailContentInfoDTO, String fileName, String filePath, Map<String, List<String>> emailFieldMap) {
-        String destPath = filePath.replaceAll(".zip", "").replaceAll(".ZIP", "");
-        log.info("压缩包地址:{},解压后文件地址:{}", filePath, destPath);
-        List<String> dir = ExcelUtil.extractCompressedFiles(filePath, destPath);
         List<EmailFundNavDTO> emailFundNavDTOList = CollUtil.newArrayList();
-        for (String zipFilePath : dir) {
-            emailFundNavDTOList.addAll(parseZipFile(emailContentInfoDTO, zipFilePath, emailFieldMap));
-            File file = new File(zipFilePath);
-            if (file.isDirectory()) {
-                for (String navFilePath : Objects.requireNonNull(file.list())) {
-                    emailFundNavDTOList.addAll(parseZipFile(emailContentInfoDTO, navFilePath, emailFieldMap));
+        if(ExcelUtil.isZip(filePath)){
+            String destPath = filePath.replaceAll(".zip", "").replaceAll(".ZIP", "");
+            log.info("压缩包地址:{},解压后文件地址:{}", filePath, destPath);
+            List<String> dir = ExcelUtil.extractCompressedFiles(filePath, destPath);
+            for (String zipFilePath : dir) {
+                emailFundNavDTOList.addAll(parseZipFile(emailContentInfoDTO, zipFilePath, emailFieldMap));
+                File file = new File(zipFilePath);
+                if (file.isDirectory()) {
+                    for (String navFilePath : Objects.requireNonNull(file.list())) {
+                        emailFundNavDTOList.addAll(parseZipFile(emailContentInfoDTO, navFilePath, emailFieldMap));
+                    }
+                }
+            }
+        }
+        try{
+            if(ExcelUtil.isRAR(filePath)){
+                String destPath = filePath.replaceAll(".rar", "").replaceAll(".RAR", "");
+                File destFile = new File(destPath);
+                if (!destFile.exists()) {
+                    destFile.mkdirs();
+                }
+                List<String> rarDir = ExcelUtil.extractRar(filePath, destPath);
+                for (String rarFilePath : rarDir) {
+                    emailFundNavDTOList.addAll(parseZipFile(emailContentInfoDTO, rarFilePath, emailFieldMap));
+                    File file = new File(rarFilePath);
+                    if (file.isDirectory()) {
+                        for (String navFilePath : Objects.requireNonNull(file.list())) {
+                            emailFundNavDTOList.addAll(parseZipFile(emailContentInfoDTO, navFilePath, emailFieldMap));
+                        }
+                    }
                 }
             }
+        }catch (Exception e){
+            log.error(e.getMessage(),e);
         }
         return emailFundNavDTOList;
     }