From 031b7da19894c8539ff9fd6a7d4b5246f8a66b7d Mon Sep 17 00:00:00 2001
From: ageerle <32251822+ageerle@users.noreply.github.com>
Date: 星期五, 16 五月 2025 16:29:21 +0800
Subject: [PATCH] Merge pull request #84 from janzhou123/main

---
 ruoyi-modules-api/ruoyi-knowledge-api/src/main/java/org/ruoyi/utils/ZipUtils.java |   88 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 88 insertions(+), 0 deletions(-)

diff --git a/ruoyi-modules-api/ruoyi-knowledge-api/src/main/java/org/ruoyi/utils/ZipUtils.java b/ruoyi-modules-api/ruoyi-knowledge-api/src/main/java/org/ruoyi/utils/ZipUtils.java
index 7ae1b2b..3c16131 100644
--- a/ruoyi-modules-api/ruoyi-knowledge-api/src/main/java/org/ruoyi/utils/ZipUtils.java
+++ b/ruoyi-modules-api/ruoyi-knowledge-api/src/main/java/org/ruoyi/utils/ZipUtils.java
@@ -11,6 +11,8 @@
 import java.util.List;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
+import org.springframework.mock.web.MockMultipartFile;
+import org.springframework.web.multipart.MultipartFile;
 
 /**
  * ZIP鏂囦欢澶勭悊宸ュ叿绫�
@@ -92,4 +94,90 @@
         }
         return base64Contents.toArray(new String[0]);
     }
+
+  /**
+   * 瑙e帇ZIP鏂囦欢骞惰繑鍥濵ultipartFile鏁扮粍
+   *
+   * @param zipData ZIP鏂囦欢鐨勫瓧鑺傛暟缁�
+   * @return MultipartFile鏁扮粍
+   * @throws IOException 濡傛灉瑙e帇杩囩▼涓彂鐢熼敊璇�
+   */
+  public static MultipartFile[] unzipToMultipartFiles(byte[] zipData) throws IOException {
+    List<MultipartFile> multipartFiles = new ArrayList<>();
+    try (ByteArrayInputStream bis = new ByteArrayInputStream(zipData);
+        ZipInputStream zis = new ZipInputStream(bis)) {
+
+      ZipEntry zipEntry;
+      while ((zipEntry = zis.getNextEntry()) != null) {
+        if (!zipEntry.isDirectory()) {
+          // 璇诲彇鏂囦欢鍐呭鍒板唴瀛�
+          ByteArrayOutputStream baos = new ByteArrayOutputStream();
+          byte[] buffer = new byte[4096];
+          int read;
+          while ((read = zis.read(buffer)) != -1) {
+            baos.write(buffer, 0, read);
+          }
+
+          // 鍒涘缓MultipartFile瀵硅薄
+          String fileName = zipEntry.getName();
+          byte[] content = baos.toByteArray();
+          String contentType = determineContentType(fileName);
+
+          MultipartFile multipartFile = new MockMultipartFile(
+              fileName,                  // 鏂囦欢鍚�
+              fileName,                  // 鍘熷鏂囦欢鍚�
+              contentType,               // 鍐呭绫诲瀷
+              content                    // 鏂囦欢鍐呭
+          );
+
+          multipartFiles.add(multipartFile);
+        }
+        zis.closeEntry();
+      }
+    }
+    return multipartFiles.toArray(new MultipartFile[0]);
+  }
+
+  /**
+   * 鏍规嵁鏂囦欢鍚嶇‘瀹氬唴瀹圭被鍨�
+   *
+   * @param fileName 鏂囦欢鍚�
+   * @return 鍐呭绫诲瀷
+   */
+  private static String determineContentType(String fileName) {
+    String extension = "";
+    int i = fileName.lastIndexOf('.');
+    if (i > 0) {
+      extension = fileName.substring(i + 1).toLowerCase();
+    }
+
+    switch (extension) {
+      case "txt":
+        return "text/plain";
+      case "html":
+      case "htm":
+        return "text/html";
+      case "pdf":
+        return "application/pdf";
+      case "jpg":
+      case "jpeg":
+        return "image/jpeg";
+      case "png":
+        return "image/png";
+      case "gif":
+        return "image/gif";
+      case "doc":
+      case "docx":
+        return "application/msword";
+      case "xls":
+      case "xlsx":
+        return "application/vnd.ms-excel";
+      case "xml":
+        return "application/xml";
+      case "json":
+        return "application/json";
+      default:
+        return "application/octet-stream";
+    }
+  }
 }
\ No newline at end of file

--
Gitblit v1.9.3