| | |
| | | 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; |
| | |
| | | * @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); |
| | |
| | | * @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; |
| | |
| | | } |
| | | |
| | | } |
| | | |
| | | 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"); |