Quellcode durchsuchen

文件解压相关提交

wanger vor 5 Monaten
Ursprung
Commit
9524b83215

+ 6 - 6
onemap-common/onemap-common-core/src/main/java/com/onemap/common/core/utils/StringUtils.java

@@ -613,11 +613,11 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
     public static String getFileStaticPath(String filename, String platform) {
         filename = filename.replaceAll("\\\\", "/");
         if ("windows".equals(platform)) {
-            return filename.replace("D:/onemapfile/", "/file/statics/");
+            return filename.replace(platform, "/file/statics/");
         } else if ("linux".equals(platform)) {
-            return filename.replace("/home/onemapfile/", "/file/statics/");
+            return filename.replace(platform, "/file/statics/");
         }
-        return filename.replace("D:/onemapfile/", "/file/statics/");
+        return filename.replace(platform, "/file/statics/");
     }
 
     /**
@@ -629,11 +629,11 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
     public static String getFileAbsolutePath(String filename, String platform) {
         filename = filename.replaceAll("\\\\", "/");
         if ("windows".equals(platform)) {
-            return filename.replace("/file/statics/", "D:\\onemapfile\\");
+            return filename.replace("/file/statics/", platform);
         } else if ("linux".equals(platform)) {
-            return filename.replace("/file/statics/", "/home/onemapfile/");
+            return filename.replace("/file/statics/", platform);
         }
-        return filename.replace("/file/statics/", "D:\\onemapfile\\");
+        return filename.replace("/file/statics/", platform);
     }
 
     /**

+ 57 - 13
onemap-common/onemap-common-core/src/main/java/com/onemap/common/core/utils/UnPackageUtils.java

@@ -4,12 +4,11 @@ import com.github.junrar.Archive;
 import com.github.junrar.rarfile.FileHeader;
 import net.lingala.zip4j.core.ZipFile;
 
-import java.io.File;
-import java.io.FileOutputStream;
+import java.io.*;
 
 /**
- * @author : wangping
- * @createDate: 2021/7/12
+ * @author : wanger
+ * @createDate: 2025/2/24
  * @description:解压缩工具
  **/
 
@@ -24,18 +23,63 @@ public class UnPackageUtils {
      *                 //* @param password 解压密码(如果有)
      */
     public static void unPackZip(File zipFile, String destPath) {
-
         try {
-            ZipFile zip = new ZipFile(zipFile);
-            /*zip4j默认用GBK编码去解压,这里设置编码为GBK的*/
-            zip.setFileNameCharset("GBK");
-            zip.extractAll(destPath);
+            String system = getSystem();
+            String command = "";
+            if (system.equals("windows")) {
+                command = String.format("\"%s\" x \"%s\" \"%s\"", "C:\\Program Files\\WinRAR\\WinRAR.exe", zipFile.getAbsolutePath(), destPath);
+            } else {
+                command = String.format("%s \"%s\" -d \"%s\"", "sudo unzip", zipFile.getAbsolutePath(), destPath);
+                // 创建ProcessBuilder对象
+                ProcessBuilder processBuilder = new ProcessBuilder("unzip", zipFile.getAbsolutePath(), "-d", destPath);
+
+                // 启动进程
+                Process process = processBuilder.start();
+
+                // 读取标准输出流
+                BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
+                String line;
+                while ((line = stdInput.readLine()) != null) {
+                    System.out.println("STDOUT: " + line);
+                }
+
+                // 读取标准错误流
+                BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
+                while ((line = stdError.readLine()) != null) {
+                    System.out.println("STDERR: " + line);
+                }
 
-            // 如果解压需要密码
-//            if (zip.isEncrypted()) {
-//                zip.setPassword(password);
-//            }
+                // 等待命令执行完成
+                int exitCode = process.waitFor();
+                System.out.println("Exited with code: " + exitCode);
+                return;
+            }
+            System.out.println("开始执行解压命令");
+            System.out.println(command);
+            // 执行命令
+            Process process = Runtime.getRuntime().exec(command);
+            InputStream inputStream = process.getInputStream();
+            // 读取输出
+            int ch;
+            while ((ch = inputStream.read()) != -1) {
+                System.out.print((char) ch);
+            }
+            int exitCode = process.waitFor();
+            System.out.println("Exit code: " + exitCode);
+            System.out.println("结束执行解压命令");
         } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    public static String getSystem() {
+        String osName = System.getProperty("os.name").toLowerCase();
+        if (osName.contains("win")) {
+            return "windows";
+        } else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) {
+            return "linux";
+        } else {
+            return "";
         }
     }
 

+ 2 - 4
onemap-modules/onemap-analyse/src/main/java/com/onemap/analyse/service/impl/ChscServiceImpl.java

@@ -48,8 +48,6 @@ public class ChscServiceImpl implements IChscService {
     private FxfwDictDTO fxfwDictDTO = new FxfwDictDTO();
     @Value("${Hgxfx.temp}")
     private String temp;
-    @Value("${platform}")
-    private String platform;
 
     @Override
     public R<?> authorize(String appid, HttpServletRequest request, HttpServletResponse response) {
@@ -77,7 +75,7 @@ public class ChscServiceImpl implements IChscService {
             if (xzlbDTO.getHgxfx() == chsc.getXzlb()) {
                 HgxfxDTO hgxfx = hgxfxMapper.selectOne(new QueryWrapper<HgxfxDTO>().eq("bsm", bsm));
                 if (hgxfx != null) {
-                    DownloadUtils.download(response, hgxfx.getFxbg().replace(".docx", ".pdf"), "选址报告", platform);
+                    DownloadUtils.download(response, hgxfx.getFxbg().replace(".docx", ".pdf"), "选址报告", temp);
                     return null;
                 } else {
                     return WebResult.error("查询报告为空");
@@ -85,7 +83,7 @@ public class ChscServiceImpl implements IChscService {
             } else if (xzlbDTO.getXmxz() == chsc.getXzlb()) {
                 FzxzDTO fzxz = fzxzMapper.selectOne(new QueryWrapper<FzxzDTO>().eq("bsm", bsm));
                 if (fzxz != null) {
-                    DownloadUtils.download(response, fzxz.getFxbg(), "选址报告", platform);
+                    DownloadUtils.download(response, fzxz.getFxbg(), "选址报告", temp);
                     return null;
                 } else {
                     return WebResult.error("查询报告为空");

+ 5 - 5
onemap-modules/onemap-analyse/src/main/java/com/onemap/analyse/task/FzxzSchedule.java

@@ -58,7 +58,7 @@ public class FzxzSchedule {
     private String fzxzShp;
     @Value("${Hgxfx.temp}")
     private String temp;
-    @Value("${platform}")
+    @Value("${Hgxfx.temp}")
     private String platform;
     /**
      * Scheduled 定时器参数
@@ -177,10 +177,10 @@ public class FzxzSchedule {
                     , rootPath + File.separator + reportPath
                     , rootPath + File.separator + simplePath);
             //生成返回结果
-            fzxzReport.setRootPath(StringUtils.getFileStaticPath(rootPath, platform));
-            fzxzReport.setReportfile(StringUtils.getFileStaticPath(rootPath + File.separator + reportPath, platform));
-            fzxzReport.setSimplefile(StringUtils.getFileStaticPath(rootPath + File.separator + simplePath, platform));
-            fzxzReport.setZipfile(StringUtils.getFileStaticPath(rootPath + File.separator + zipPath, platform));
+            fzxzReport.setRootPath(StringUtils.getFileStaticPath(rootPath, temp));
+            fzxzReport.setReportfile(StringUtils.getFileStaticPath(rootPath + File.separator + reportPath, temp));
+            fzxzReport.setSimplefile(StringUtils.getFileStaticPath(rootPath + File.separator + simplePath, temp));
+            fzxzReport.setZipfile(StringUtils.getFileStaticPath(rootPath + File.separator + zipPath, temp));
             fzxzReport.setBsm(bsm);
             fzxzReport.setFxbg(fzxzReport.getReportfile());
             fzxzReport.setJsdw(res.getJsdw());

+ 2 - 4
onemap-modules/onemap-analyse/src/main/java/com/onemap/analyse/task/HgxfxSchedule.java

@@ -48,11 +48,9 @@ public class HgxfxSchedule {
     @Value("${Hgxfx.functionId}")
     private String functionId;
     @Value("${Hgxfx.temp}")
-    private String temp;
+    private static String temp;
     @Value("${Hgxfx.ftpOut}")
     private String ftpOut;
-    @Value("${platform}")
-    private String platform;
     private static final Logger log = LoggerFactory.getLogger(HgxfxSchedule.class);
     /**
      * Scheduled 定时器参数
@@ -218,7 +216,7 @@ public class HgxfxSchedule {
         String reportPath = rootPath
                 + File.separator + res.getXmmc() + "(合规性检查报告)_" + timeStamp + ".docx";
         //解析文件路径为web路径
-        String webFilePath = StringUtils.getFileStaticPath(reportPath, platform);
+        String webFilePath = StringUtils.getFileStaticPath(reportPath, temp);
         double mj = 0;
         if (res != null) {
             XWPFDocument document = null;

+ 1 - 1
onemap-modules/onemap-apply/src/main/java/com/onemap/apply/controller/yzt/ZttKController.java

@@ -34,7 +34,7 @@ public class ZttKController extends BaseController
     IZttKService zttkService;
     @Autowired
     ZttKMapper zttKMapper;
-    @Value("${platform}")
+    @Value("${Cggl.temp}")
     private String platform;
 
     /**

+ 6 - 5
onemap-modules/onemap-apply/src/main/java/com/onemap/apply/service/impl/cggl/CgglServiceImpl.java

@@ -13,6 +13,7 @@ import com.onemap.apply.utils.*;
 import com.onemap.common.core.domain.UploadZipDTO;
 import com.onemap.common.core.utils.FileStreamUtils;
 import com.onemap.common.core.utils.StringUtils;
+import com.onemap.common.core.utils.UnPackageUtils;
 import com.onemap.common.core.web.domain.AjaxResult;
 import com.onemap.common.core.web.domain.RequestResult;
 import com.onemap.common.redis.service.RedisService;
@@ -67,10 +68,10 @@ public class CgglServiceImpl implements CgglService {
     @Autowired
     private XzczghcgsbMapper xzczghcgsbMapper;
 
-    @Value("${platform}")
+    @Value("${Cggl.temp}")
     private static String platform;
     @Value("${Cggl.temp}")
-    private String temp;
+    private static String temp;
     @Value("${Cggl.appid}")
     private String appid;
     @Value("${Cggl.oauthUrl}")
@@ -184,7 +185,7 @@ public class CgglServiceImpl implements CgglService {
         wrapper.eq("name", escalationDTO.getName());
         EscalationDTO res = escalationMapper.selectOne(wrapper);
         if (res != null) {
-            return RequestResult.error("该名称已存在!", false);
+//            return RequestResult.error("该名称已存在!", false);
         }
 //        if (escalationDTO.getName().getBytes().length > 100) {
 //            return RequestResult.error("成果名称不能大于100个字节!", false);
@@ -865,7 +866,7 @@ public class CgglServiceImpl implements CgglService {
             data.setId(map.get(file.getAbsolutePath()));
             data.setPid(map.get(file.getParent()));
             data.setName(file.getName());
-            data.setFilepath(StringUtils.getFileStaticPath(file.getAbsolutePath(), platform));
+            data.setFilepath(StringUtils.getFileStaticPath(file.getAbsolutePath(), temp));
             data.setIsparent(true);
             if (file.getName().toLowerCase().indexOf(".gdb") > 0) {
                 data.setFilepath(file.getAbsolutePath());
@@ -898,7 +899,7 @@ public class CgglServiceImpl implements CgglService {
                 data.setId(map.get(file.getAbsolutePath()));
                 data.setPid(map.get(file.getParent()));
                 data.setName(file.getName());
-                data.setFilepath(StringUtils.getFileStaticPath(file.getAbsolutePath(), platform));
+                data.setFilepath(StringUtils.getFileStaticPath(file.getAbsolutePath(), temp));
                 data.setIsparent(false);
                 allFile.add(data);
             }

+ 1 - 2
onemap-modules/onemap-apply/src/main/java/com/onemap/apply/service/impl/sbjk/XxghcgsbServiceImpl.java

@@ -5,12 +5,11 @@ import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.onemap.apply.domain.sbjk.XxghcgsbDTO;
 import com.onemap.apply.mapper.sbjk.XxghcgsbMapper;
-import com.onemap.apply.mapper.sbjk.XzczghcgsbMapper;
 import com.onemap.apply.service.sbjk.XxghcgsbService;
 import com.onemap.apply.utils.Md5Utils;
 import com.onemap.apply.utils.RInterfaceUtil;
-import com.onemap.apply.utils.UnPackageUtils;
 import com.onemap.common.core.utils.StringUtils;
+import com.onemap.common.core.utils.UnPackageUtils;
 import com.onemap.common.core.utils.uuid.IdUtils;
 import com.onemap.common.core.web.domain.AjaxResult;
 import com.onemap.common.redis.service.RedisService;

+ 1 - 4
onemap-modules/onemap-apply/src/main/java/com/onemap/apply/service/impl/sbjk/XzczghcgsbServiceImpl.java

@@ -3,21 +3,18 @@ package com.onemap.apply.service.impl.sbjk;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.onemap.apply.domain.cggl.EscalationDTO;
 import com.onemap.apply.domain.cggl.ScmxDTO;
 import com.onemap.apply.domain.cggl.ScmxRecordDTO;
-import com.onemap.apply.domain.sbjk.TjpgsbDTO;
 import com.onemap.apply.domain.sbjk.XzczghcgsbDTO;
 import com.onemap.apply.mapper.cggl.EscalationMapper;
 import com.onemap.apply.mapper.cggl.ScmxMapper;
 import com.onemap.apply.mapper.cggl.ScmxRecordMapper;
 import com.onemap.apply.mapper.sbjk.XzczghcgsbMapper;
 import com.onemap.apply.service.sbjk.XzczghcgsbService;
-import com.onemap.apply.utils.HttpClientUtil;
 import com.onemap.apply.utils.Md5Utils;
 import com.onemap.apply.utils.RInterfaceUtil;
-import com.onemap.apply.utils.UnPackageUtils;
 import com.onemap.common.core.utils.StringUtils;
+import com.onemap.common.core.utils.UnPackageUtils;
 import com.onemap.common.core.utils.uuid.IdUtils;
 import com.onemap.common.core.web.domain.AjaxResult;
 import com.onemap.common.core.web.domain.RequestResult;

+ 0 - 81
onemap-modules/onemap-apply/src/main/java/com/onemap/apply/utils/UnPackageUtils.java

@@ -1,81 +0,0 @@
-package com.onemap.apply.utils;
-
-
-import com.github.junrar.Archive;
-import com.github.junrar.rarfile.FileHeader;
-import net.lingala.zip4j.core.ZipFile;
-
-import java.io.File;
-import java.io.FileOutputStream;
-
-/**
- * @author : wangping
- * @createDate: 2021/7/12
- * @description:解压缩工具
- **/
-
-
-public class UnPackageUtils {
-
-    /**
-     * zip文件解压
-     *
-     * @param destPath 解压文件路径
-     * @param zipFile  压缩文件
-     *                 //* @param password 解压密码(如果有)
-     */
-    public static void unPackZip(File zipFile, String destPath) {
-
-        try {
-            ZipFile zip = new ZipFile(zipFile);
-            /*zip4j默认用GBK编码去解压,这里设置编码为GBK的*/
-            zip.setFileNameCharset("GBK");
-            zip.extractAll(destPath);
-
-            // 如果解压需要密码
-//            if (zip.isEncrypted()) {
-//                zip.setPassword(password);
-//            }
-        } catch (Exception e) {
-        }
-    }
-
-    /**
-     * rar文件解压(不支持有密码的压缩包)
-     *
-     * @param rarFile  rar压缩包
-     * @param destPath 解压保存路径
-     */
-    public static void unPackRar(File rarFile, String destPath) {
-        try (Archive archive = new Archive(rarFile)) {
-            if (null != archive) {
-                FileHeader fileHeader = archive.nextFileHeader();
-                File file = null;
-                while (null != fileHeader) {
-                    // 防止文件名中文乱码问题的处理
-                    String fileName = fileHeader.getFileNameW().isEmpty() ? fileHeader.getFileNameString() : fileHeader.getFileNameW();
-                    if (fileHeader.isDirectory()) {
-                        //是文件夹
-                        file = new File(destPath + File.separator + fileName);
-                        file.mkdirs();
-                    } else {
-                        //不是文件夹
-                        file = new File(destPath + File.separator + fileName.trim());
-                        if (!file.exists()) {
-                            if (!file.getParentFile().exists()) {
-                                // 相对路径可能多级,可能需要创建父目录.
-                                file.getParentFile().mkdirs();
-                            }
-                            file.createNewFile();
-                        }
-                        FileOutputStream os = new FileOutputStream(file);
-                        archive.extractFile(fileHeader, os);
-                        os.close();
-                    }
-                    fileHeader = archive.nextFileHeader();
-                }
-            }
-        } catch (Exception e) {
-        }
-    }
-}

+ 1 - 0
onemap-modules/onemap-file/src/main/java/com/onemap/file/service/LocalSysFileServiceImpl.java

@@ -5,6 +5,7 @@ import com.onemap.common.core.domain.TgclTypeDTO;
 import com.onemap.common.core.domain.UploadZipDTO;
 import com.onemap.common.core.domain.WordToPdfDTO;
 import com.onemap.common.core.utils.StringUtils;
+import com.onemap.common.core.utils.UnPackageUtils;
 import com.onemap.common.core.web.domain.RequestResult;
 import com.onemap.file.domain.ShpFileDTO;
 import com.onemap.file.domain.ZhxzFileDTO;

+ 0 - 80
onemap-modules/onemap-file/src/main/java/com/onemap/file/utils/UnPackageUtils.java

@@ -1,80 +0,0 @@
-package com.onemap.file.utils;
-
-import com.github.junrar.Archive;
-import com.github.junrar.rarfile.FileHeader;
-import net.lingala.zip4j.core.ZipFile;
-
-import java.io.File;
-import java.io.FileOutputStream;
-
-/**
- * @author : wangping
- * @createDate: 2021/7/12
- * @description:解压缩工具
- **/
-
-
-public class UnPackageUtils {
-
-    /**
-     * zip文件解压
-     *
-     * @param destPath 解压文件路径
-     * @param zipFile  压缩文件
-     *                 //* @param password 解压密码(如果有)
-     */
-    public static void unPackZip(File zipFile, String destPath) {
-
-        try {
-            ZipFile zip = new ZipFile(zipFile);
-            /*zip4j默认用GBK编码去解压,这里设置编码为GBK的*/
-            zip.setFileNameCharset("GBK");
-            zip.extractAll(destPath);
-
-            // 如果解压需要密码
-//            if (zip.isEncrypted()) {
-//                zip.setPassword(password);
-//            }
-        } catch (Exception e) {
-        }
-    }
-
-    /**
-     * rar文件解压(不支持有密码的压缩包)
-     *
-     * @param rarFile  rar压缩包
-     * @param destPath 解压保存路径
-     */
-    public static void unPackRar(File rarFile, String destPath) {
-        try (Archive archive = new Archive(rarFile)) {
-            if (null != archive) {
-                FileHeader fileHeader = archive.nextFileHeader();
-                File file = null;
-                while (null != fileHeader) {
-                    // 防止文件名中文乱码问题的处理
-                    String fileName = fileHeader.getFileNameW().isEmpty() ? fileHeader.getFileNameString() : fileHeader.getFileNameW();
-                    if (fileHeader.isDirectory()) {
-                        //是文件夹
-                        file = new File(destPath + File.separator + fileName);
-                        file.mkdirs();
-                    } else {
-                        //不是文件夹
-                        file = new File(destPath + File.separator + fileName.trim());
-                        if (!file.exists()) {
-                            if (!file.getParentFile().exists()) {
-                                // 相对路径可能多级,可能需要创建父目录.
-                                file.getParentFile().mkdirs();
-                            }
-                            file.createNewFile();
-                        }
-                        FileOutputStream os = new FileOutputStream(file);
-                        archive.extractFile(fileHeader, os);
-                        os.close();
-                    }
-                    fileHeader = archive.nextFileHeader();
-                }
-            }
-        } catch (Exception e) {
-        }
-    }
-}