whyclxw
2024-07-23 91369d43eb19c44259391feb5087b261f8e8c5a5
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){
    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);
@@ -174,8 +171,6 @@
            if(tempFileName.endsWith("zip") || tempFileName.endsWith("rar")){
                decompressOne(tempFileName);
                File file = new File(tempFileName);
                boolean b = file.canExecute();
                System.out.println(b);
                file.delete();
                existCompressedFile = true;
            }
@@ -197,7 +192,7 @@
     * @param compressedFileUrl doc_file/xxx/xxx.zip 或者 rar
     * @return
     */
    public static List<Object> decompressOne(String compressedFileUrl){
    public static List<Object> decompressOne(String compressedFileUrl) throws IOException, ArchiveException, InterruptedException {
        List<Object> resList = new LinkedList<>();
        String projectDir = CommonUtil.getProjectDir() + File.separator;
        String fullFilePath;
@@ -233,84 +228,69 @@
        return resList;
    }
    public static String decompressT(File file){
        String fileName = file.getName();
        String outputFolder = "C:\\Users\\29550\\Desktop\\decompress" + File.separator + fileName;
        File outputFolderFile = new File(outputFolder);
        if(!outputFolderFile.exists()){
            outputFolderFile.mkdirs();
        }
        if(fileName.endsWith("zip")){
            decompressZip(file, outputFolder);
        }else { //rar
            decompressRar(file, outputFolder);
        }
        return null;
    }
    private static void decompressZip(File file, String outputFolder) {
        try (ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream("zip", new FileInputStream(file))) {
            ArchiveEntry entry;
            while ((entry = ais.getNextEntry()) != null) {
                if (!ais.canReadEntryData(entry) || entry.isDirectory()) {
                    continue;
                }
                byte[] buffer = new byte[4096];
                int bytesRead;
                String entryName = entry.getName();
                //if(entryName.contains(File.separator)){
                if(entryName.contains("/")){
                    String entryNameDir = entryName.substring(0, entryName.lastIndexOf("/"));
                    String entryDirStr = outputFolder + File.separator + entryNameDir;
                    File entryDir = new File(entryDirStr);
                    if(!entryDir.exists()){
                        entryDir.mkdirs();
                    }
                }
                OutputStream outputStream = new FileOutputStream(new File(outputFolder + File.separator + entryName));
                while ((bytesRead = ais.read(buffer)) > -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
                outputStream.close();
    private static void decompressZip(File file, String outputFolder) throws IOException, ArchiveException {
        ArchiveInputStream ais = new ArchiveStreamFactory("GBK").createArchiveInputStream("zip", new FileInputStream(file));
        ArchiveEntry entry;
        while ((entry = ais.getNextEntry()) != null) {
            if (!ais.canReadEntryData(entry) || entry.isDirectory()) {
                continue;
            }
        } catch (IOException | ArchiveException e) {
            e.printStackTrace();
        }
    }
    private static void decompressRar(File file, String outputFolder) {
        try {
            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();
            byte[] buffer = new byte[4096];
            int bytesRead;
            String entryName = entry.getName();
            //if(entryName.contains(File.separator)){
            if(entryName.contains("/")){
                String entryNameDir = entryName.substring(0, entryName.lastIndexOf("/"));
                String entryDirStr = outputFolder + File.separator + entryNameDir;
                File entryDir = new File(entryDirStr);
                if(!entryDir.exists()){
                    entryDir.mkdirs();
                }
            }
            archive.close();
        } catch (IOException | RarException e) {
            e.printStackTrace();
            OutputStream outputStream = new FileOutputStream(new File(outputFolder + File.separator + entryName));
            while ((bytesRead = ais.read(buffer)) > -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            outputStream.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");