From eb9df0f9e9b91757f82a6211a74b996629860f53 Mon Sep 17 00:00:00 2001
From: whyclxw <810412026@qq.com>
Date: 星期六, 14 六月 2025 17:40:13 +0800
Subject: [PATCH] 编辑多个和单个图片上传,删除图片

---
 src/main/java/com/whyc/util/FileUtil.java |  119 ++++++++++++++++++++++++++++++++++++++---------------------
 1 files changed, 76 insertions(+), 43 deletions(-)

diff --git a/src/main/java/com/whyc/util/FileUtil.java b/src/main/java/com/whyc/util/FileUtil.java
index ba1fcde..5517e36 100644
--- a/src/main/java/com/whyc/util/FileUtil.java
+++ b/src/main/java/com/whyc/util/FileUtil.java
@@ -1,8 +1,5 @@
 package com.whyc.util;
 
-import com.github.junrar.Archive;
-import com.github.junrar.exception.RarException;
-import com.github.junrar.rarfile.FileHeader;
 import org.apache.commons.compress.archivers.ArchiveEntry;
 import org.apache.commons.compress.archivers.ArchiveException;
 import org.apache.commons.compress.archivers.ArchiveInputStream;
@@ -12,10 +9,13 @@
 import javax.servlet.http.HttpServletResponse;
 import java.io.*;
 import java.net.URLEncoder;
+import java.nio.file.Files;
+import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
 
 public class FileUtil {
+
 
     public static List<String> getStaticFilePath(File file, List<String> list){
 
@@ -78,22 +78,26 @@
             System.out.println("鏂囦欢鍒犻櫎澶辫触,璇锋鏌ユ枃浠舵槸鍚﹀瓨鍦ㄤ互鍙婃枃浠惰矾寰勬槸鍚︽纭�");
             return false;
         }
-        //鑾峰彇鐩綍涓嬪瓙鏂囦欢
-        File[] files = file.listFiles();
-        //閬嶅巻璇ョ洰褰曚笅鐨勬枃浠跺璞�
-        for (File f : files) {
-            //鍒ゆ柇瀛愮洰褰曟槸鍚﹀瓨鍦ㄥ瓙鐩綍,濡傛灉鏄枃浠跺垯鍒犻櫎
-            if (f.isDirectory()) {
-                //閫掑綊鍒犻櫎鐩綍涓嬬殑鏂囦欢
-                deleteFile(f);
-            } else {
-                //鏂囦欢鍒犻櫎
-                f.delete();
-                //鎵撳嵃鏂囦欢鍚�
+        if(file.isDirectory()){
+            //鑾峰彇鐩綍涓嬪瓙鏂囦欢
+            File[] files = file.listFiles();
+            //閬嶅巻璇ョ洰褰曚笅鐨勬枃浠跺璞�
+            for (File f : files) {
+                //鍒ゆ柇瀛愮洰褰曟槸鍚﹀瓨鍦ㄥ瓙鐩綍,濡傛灉鏄枃浠跺垯鍒犻櫎
+                if (f.isDirectory()) {
+                    //閫掑綊鍒犻櫎鐩綍涓嬬殑鏂囦欢
+                    deleteFile(f);
+                } else {
+                    //鏂囦欢鍒犻櫎
+                    f.delete();
+                    //鎵撳嵃鏂囦欢鍚�
+                }
             }
+            //鏂囦欢澶瑰垹闄�
+            file.delete();
+        }else{
+            file.delete();
         }
-        //鏂囦欢澶瑰垹闄�
-        file.delete();
         return true;
     }
 
@@ -164,20 +168,25 @@
      * @param compressedFileUrl doc_file/xxx/xxx.zip 鎴栬�� rar
      * @return
      */
-    public static List<String> decompress(String compressedFileUrl) throws ArchiveException, IOException, RarException {
+    public static List<String> decompress(String compressedFileUrl) throws ArchiveException, IOException, InterruptedException {
         List<Object> resList = decompressOne(compressedFileUrl);
         File outputFolderFile = (File) resList.get(0);
         List<String>  fileList = (List<String>) resList.get(1);
         boolean existCompressedFile = false;
         int checkDecompress = 0;
+        //閬嶅巻鏂囦欢鍒楄〃锛屽垽鏂槸鍚﹀瓨鍦ㄥ帇缂╂枃浠�
         for(String tempFileName:fileList){
             if(tempFileName.endsWith("zip") || tempFileName.endsWith("rar")){
+                //瀛樺湪鍘嬬缉鏂囦欢,瑙e帇涓�娆�
                 decompressOne(tempFileName);
                 File file = new File(tempFileName);
-                file.delete();
+                //瑙i櫎鏂囦欢鍗犵敤骞跺垹闄ゆ枃浠�
+                //boolean delete = file.delete();
+                Files.delete(file.toPath());
                 existCompressedFile = true;
             }
         }
+        //濡傛灉瀛樺湪鍘嬬缉鏂囦欢骞跺凡瑙e帇,鍒欓渶瑕佹鏌ヤ竴娆℃槸鍚﹁繕鏈夊帇缂╂枃浠�
         if(existCompressedFile){
             checkDecompress ++;
         }
@@ -195,7 +204,7 @@
      * @param compressedFileUrl doc_file/xxx/xxx.zip 鎴栬�� rar
      * @return
      */
-    public static List<Object> decompressOne(String compressedFileUrl) throws IOException, ArchiveException, RarException {
+    public static List<Object> decompressOne(String compressedFileUrl) throws IOException, ArchiveException, InterruptedException {
         List<Object> resList = new LinkedList<>();
         String projectDir = CommonUtil.getProjectDir() + File.separator;
         String fullFilePath;
@@ -232,7 +241,7 @@
     }
 
     private static void decompressZip(File file, String outputFolder) throws IOException, ArchiveException {
-        ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream("zip", new FileInputStream(file));
+        ArchiveInputStream ais = new ArchiveStreamFactory("GBK").createArchiveInputStream("zip", new FileInputStream(file));
         ArchiveEntry entry;
         while ((entry = ais.getNextEntry()) != null) {
             if (!ais.canReadEntryData(entry) || entry.isDirectory()) {
@@ -257,38 +266,62 @@
                 outputStream.write(buffer, 0, bytesRead);
             }
 
+            //鍏虫祦
             outputStream.close();
         }
+        //鍏虫祦
+        ais.close();
 
     }
+    private static void decompressRar(File file, String outputFolder) throws IOException, InterruptedException {
+        String winrarPath = "C:\\Program Files\\WinRAR\\WinRAR.exe";
+        String cmd = winrarPath + " X " + file + " " + outputFolder;
+        Process proc = Runtime.getRuntime().exec(cmd);
+        proc.waitFor();
+    }
 
-    private static void decompressRar(File file, String outputFolder) throws IOException, RarException {
-        Archive archive = new Archive(file);
-        FileHeader fileHeader = archive.nextFileHeader();
-        if (fileHeader != null) {
-            while (fileHeader != null) {
-                if (fileHeader.isDirectory()) {
-                    fileHeader = archive.nextFileHeader();
-                    continue;
+    //璇诲彇鏂囦欢澶逛笅鐨勬墍鏈夋枃浠讹紙涓嶈鍙栨枃浠跺す鍐呯殑鏂囦欢锛�
+    public static List getFileNameWithOutDirectory(String filePath) {
+        File folder = new File(filePath); // 鏂囦欢澶硅矾寰�
+        List nameList=new ArrayList();
+        File[] listOfFiles = folder.listFiles();
+        if (listOfFiles != null) {
+            for (File file : listOfFiles) {
+                if (file.isFile()) {
+                    nameList.add(file.getName());
                 }
-                String tempFilePath = fileHeader.getFileName();
-                File out = new File(outputFolder + File.separator + tempFilePath);
-                if (!out.exists()) {
-                    if (!out.getParentFile().exists()) {
-                        out.getParentFile().mkdirs();
-                    }
-                    out.createNewFile();
-                }
-                FileOutputStream os = new FileOutputStream(out);
-                archive.extractFile(fileHeader, os);
-                os.close();
-                fileHeader = archive.nextFileHeader();
             }
         }
-        archive.close();
-
+        return nameList;
     }
 
+    //private static void decompressRar(File file, String outputFolder) throws IOException, RarException {
+    //    Archive archive = new Archive(file);
+    //    FileHeader fileHeader = archive.nextFileHeader();
+    //    if (fileHeader != null) {
+    //        while (fileHeader != null) {
+    //            if (fileHeader.isDirectory()) {
+    //                fileHeader = archive.nextFileHeader();
+    //                continue;
+    //            }
+    //            String tempFilePath = fileHeader.getFileName();
+    //            File out = new File(outputFolder + File.separator + tempFilePath);
+    //            if (!out.exists()) {
+    //                if (!out.getParentFile().exists()) {
+    //                    out.getParentFile().mkdirs();
+    //                }
+    //                out.createNewFile();
+    //            }
+    //            FileOutputStream os = new FileOutputStream(out);
+    //            archive.extractFile(fileHeader, os);
+    //            os.close();
+    //            fileHeader = archive.nextFileHeader();
+    //        }
+    //    }
+    //    archive.close();
+    //
+    //}
+
     public static void main(String[] args) {
         //File file = new File("C:\\Users\\29550\\Desktop\\AppScan10.rar");
         File file = new File("C:\\code\\web\\CadDrawManager\\target\\doc_file\\decompress\\doc_file@product@FBS-9600-GDPDX-XS1-DC220V-JH@standard@3@3++.rar");

--
Gitblit v1.9.1