From 1fa878f57239475c925ae1ea4bd2793c3e831b49 Mon Sep 17 00:00:00 2001
From: lxw <810412026@qq.com>
Date: 星期一, 09 十月 2023 15:43:19 +0800
Subject: [PATCH] 产品查看原来压缩包中文件信息(文件目录和时间)

---
 src/main/java/com/whyc/controller/ZipAndRarController.java |   25 ++++++++
 src/main/java/com/whyc/service/ProductHistoryService.java  |    2 
 src/main/java/com/whyc/service/ZipAndRarService.java       |   33 +++++++++++
 src/main/java/com/whyc/util/ZipAndRarUtil.java             |   82 +++++++++++++++++++++++++++
 src/main/java/com/whyc/pojo/FileMessage.java               |   10 +++
 pom.xml                                                    |   11 +++
 6 files changed, 162 insertions(+), 1 deletions(-)

diff --git a/pom.xml b/pom.xml
index 3058112..d085db8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -158,6 +158,17 @@
             <artifactId>commons-lang3</artifactId>
             <version>3.4</version>
         </dependency>
+        <!--zip鍜宺ar鍘嬬缉鍖呬娇鐢� -->
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-compress</artifactId>
+            <version>1.12</version>
+        </dependency>
+        <dependency>
+            <groupId>com.github.junrar</groupId>
+            <artifactId>junrar</artifactId>
+            <version>3.0.0</version>
+        </dependency>
         <!--澶栭儴寮曞叆-->
         <dependency>
             <groupId>com.whyc</groupId>
diff --git a/src/main/java/com/whyc/controller/ZipAndRarController.java b/src/main/java/com/whyc/controller/ZipAndRarController.java
new file mode 100644
index 0000000..e7edbc9
--- /dev/null
+++ b/src/main/java/com/whyc/controller/ZipAndRarController.java
@@ -0,0 +1,25 @@
+package com.whyc.controller;
+
+import com.whyc.dto.Response;
+import com.whyc.service.ZipAndRarService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+@Api(tags = "鍘嬬缉鍖�")
+@RestController
+@RequestMapping("zipAndRar")
+public class ZipAndRarController {
+    @Autowired
+    private ZipAndRarService service;
+
+    @ApiOperation(value = "浜у搧鏌ョ湅鍘熸潵鍘嬬缉鍖呬腑鏂囦欢淇℃伅锛堟枃浠剁洰褰曞拰鏃堕棿锛�")
+    @GetMapping("getzipAndRarInfo")
+    public Response getzipAndRarInfo(@RequestParam String fileUrl){
+        return service.getzipAndRarInfo(fileUrl);
+    }
+}
diff --git a/src/main/java/com/whyc/pojo/FileMessage.java b/src/main/java/com/whyc/pojo/FileMessage.java
new file mode 100644
index 0000000..e614dbb
--- /dev/null
+++ b/src/main/java/com/whyc/pojo/FileMessage.java
@@ -0,0 +1,10 @@
+package com.whyc.pojo;
+
+import lombok.Data;
+
+@Data
+public class FileMessage {
+    private String fileName;
+    private String fileTime;
+    private Long fileSize;
+}
\ No newline at end of file
diff --git a/src/main/java/com/whyc/service/ProductHistoryService.java b/src/main/java/com/whyc/service/ProductHistoryService.java
index 60c1e36..cf8c97b 100644
--- a/src/main/java/com/whyc/service/ProductHistoryService.java
+++ b/src/main/java/com/whyc/service/ProductHistoryService.java
@@ -112,7 +112,7 @@
         query.eq("parent_code",parentCode).eq("custom_code",customCode).orderByDesc("version").last(" limit 1");
         return mapper.selectOne(query);
     }
-    //鍘嗗彶鐗堟湰鍙敤鎬ц瀹�
+    //鍘嗗彶鐗堟湰鍙敤鎬ц瀹�<浜у搧涓寘鍚玃CB>
     @Transactional
     public Response setpHistoryEnable(String parentCode, String customCode, int version, int enabled, String reason, String versionTime) {
         UpdateWrapper wrapper=new UpdateWrapper();
diff --git a/src/main/java/com/whyc/service/ZipAndRarService.java b/src/main/java/com/whyc/service/ZipAndRarService.java
new file mode 100644
index 0000000..616a57d
--- /dev/null
+++ b/src/main/java/com/whyc/service/ZipAndRarService.java
@@ -0,0 +1,33 @@
+package com.whyc.service;
+
+import com.whyc.dto.FileDirPath;
+import com.whyc.dto.Response;
+import com.whyc.pojo.FileMessage;
+import com.whyc.util.ZipAndRarUtil;
+import org.springframework.stereotype.Service;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+@Service
+public class ZipAndRarService {
+
+
+    //浜у搧鏌ョ湅鍘熸潵鍘嬬缉鍖呬腑鏂囦欢淇℃伅锛堟枃浠剁洰褰曞拰鏃堕棿锛�
+    public Response getzipAndRarInfo(String fileUrl) {
+        String fileDirName = FileDirPath.getFileDirName();
+        List<FileMessage> list=new ArrayList<>();
+        try {
+            if(fileUrl.contains("zip")){
+                list=ZipAndRarUtil.getZipFileList(fileDirName+ File.separator+fileUrl);
+            }else if(fileUrl.contains("rar")){
+                list=ZipAndRarUtil.getRarList(fileDirName+ File.separator+fileUrl);
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return new Response().setII(1,list!=null,list,"鍘嬬缉鏂囦欢淇℃伅");
+    }
+}
diff --git a/src/main/java/com/whyc/util/ZipAndRarUtil.java b/src/main/java/com/whyc/util/ZipAndRarUtil.java
new file mode 100644
index 0000000..865e962
--- /dev/null
+++ b/src/main/java/com/whyc/util/ZipAndRarUtil.java
@@ -0,0 +1,82 @@
+package com.whyc.util;
+
+import com.github.junrar.Archive;
+import com.github.junrar.rarfile.FileHeader;
+import com.whyc.pojo.FileMessage;
+
+import java.io.BufferedInputStream;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.zip.ZipInputStream;
+
+public class ZipAndRarUtil {
+    public static List<FileMessage> getZipFileList(String zipFileName) throws IOException {
+        ZipInputStream zipInputStream = null;
+        try{
+            FileInputStream inputStream = new FileInputStream(zipFileName);
+            zipInputStream = new ZipInputStream(
+                    new BufferedInputStream(inputStream), StandardCharsets.UTF_8);
+            java.util.zip.ZipEntry ze;
+            List<FileMessage> list = new ArrayList<>();
+            //寰幆閬嶅巻
+            while ((ze = zipInputStream.getNextEntry()) != null) {
+                FileMessage fileMessage=new FileMessage();
+                if (!ze.isDirectory()) {
+                    fileMessage.setFileName(ze.getName());
+                    fileMessage.setFileSize(ze.getSize());
+                    String formatted = ActionUtil.sdfwithALL.format( new Date( ze.getLastModifiedTime().toMillis() ) );
+                    fileMessage.setFileTime(formatted);
+                    list.add(fileMessage);
+                }
+            }
+            return list;
+        } catch (Exception e) {
+            //logger.error(" getZipOneFileContent error:" + e.getMessage());
+            return null;
+        } finally {
+            if (zipInputStream != null) {
+                zipInputStream.closeEntry();
+            }
+        }
+    }
+
+    public static List<FileMessage> getRarList(String rarFileName) throws IOException {
+        FileInputStream inputStream = null;
+        Archive archive = null;
+        try {
+            inputStream = new FileInputStream(rarFileName);
+            archive = new Archive(inputStream);
+            FileHeader fileHeader = archive.nextFileHeader();
+            List<FileMessage> list = new ArrayList<>();
+            while (fileHeader != null) {
+                if (fileHeader.isDirectory()) {
+                    continue;
+                }
+                FileMessage fileMessage=new FileMessage();
+                fileMessage.setFileName(fileHeader.getFileNameString());
+                fileMessage.setFileSize(fileHeader.getFullUnpackSize());
+                String formatted = ActionUtil.sdfwithALL.format(fileHeader.getMTime());
+                fileMessage.setFileTime(formatted);
+                list.add(fileMessage);
+                fileHeader = archive.nextFileHeader();
+            }
+            return list;
+        } catch (Exception e) {
+            //logger.error(" getRarFileList error:" + e.getMessage());
+            return null;
+        } finally {
+            if (inputStream != null) {
+                inputStream.close();
+            }
+            if (archive != null) {
+                archive.close();
+            }
+        }
+    }
+
+}

--
Gitblit v1.9.1