whycxzp
2024-01-03 f7733dc8aaca5590f7194ff4bd491e80d8c92ab3
解压rar
4个文件已修改
76 ■■■■ 已修改文件
pom.xml 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/controller/ZipAndRarController.java 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/ZipAndRarService.java 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/util/FileUtil.java 65 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pom.xml
@@ -229,11 +229,6 @@
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.junrar</groupId>
            <artifactId>junrar</artifactId>
            <version>7.4.1</version>
        </dependency>
    </dependencies>
    <build>
src/main/java/com/whyc/controller/ZipAndRarController.java
@@ -1,6 +1,5 @@
package com.whyc.controller;
import com.github.junrar.exception.RarException;
import com.whyc.dto.Response;
import com.whyc.service.ZipAndRarService;
import io.swagger.annotations.Api;
@@ -29,7 +28,7 @@
    @ApiOperation("解压")
    @GetMapping("decompress")
    public Response decompress(@RequestParam String compressedFileUrl) throws ArchiveException, IOException, RarException {
    public Response decompress(@RequestParam String compressedFileUrl) throws ArchiveException, IOException, InterruptedException {
        return service.decompress(compressedFileUrl);
    }
src/main/java/com/whyc/service/ZipAndRarService.java
@@ -1,6 +1,5 @@
package com.whyc.service;
import com.github.junrar.exception.RarException;
import com.whyc.dto.FileDirPath;
import com.whyc.dto.Response;
import com.whyc.pojo.FileMessage;
@@ -35,7 +34,7 @@
        return new Response().setII(1,list!=null,list,"压缩文件信息");
    }
    public Response decompress(String compressedFileUrl) throws ArchiveException, IOException, RarException {
    public Response decompress(String compressedFileUrl) throws ArchiveException, IOException, InterruptedException {
        String fileSuffix = (compressedFileUrl.substring(compressedFileUrl.lastIndexOf(".")+1));
        List<String> resList = new LinkedList<>();
        if(fileSuffix.equals("zip")||fileSuffix.equals("rar")) {
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;
@@ -164,7 +161,7 @@
     * @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);
@@ -195,7 +192,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;
@@ -261,34 +258,40 @@
        }
    }
    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();
    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;
    //            }
    //            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");