whycxzp
2024-01-03 f7733dc8aaca5590f7194ff4bd491e80d8c92ab3
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");