| | |
| | | * @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, RarException { |
| | | 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){ |
| | | public static List<Object> decompressOne(String compressedFileUrl) throws IOException, ArchiveException, RarException { |
| | | List<Object> resList = new LinkedList<>(); |
| | | String projectDir = CommonUtil.getProjectDir() + File.separator; |
| | | String fullFilePath; |
| | |
| | | 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); |
| | | private static void decompressZip(File file, String outputFolder) throws IOException, ArchiveException { |
| | | 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(); |
| | | } |
| | | |
| | | 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()) { |
| | | |
| | | 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; |
| | | } |
| | | 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(); |
| | | String tempFilePath = fileHeader.getFileName(); |
| | | File out = new File(outputFolder + File.separator + tempFilePath); |
| | | if (!out.exists()) { |
| | | if (!out.getParentFile().exists()) { |
| | | out.getParentFile().mkdirs(); |
| | | } |
| | | out.createNewFile(); |
| | | } |
| | | |
| | | OutputStream outputStream = new FileOutputStream(new File(outputFolder + File.separator + entryName)); |
| | | |
| | | while ((bytesRead = ais.read(buffer)) > -1) { |
| | | outputStream.write(buffer, 0, bytesRead); |
| | | } |
| | | |
| | | outputStream.close(); |
| | | FileOutputStream os = new FileOutputStream(out); |
| | | archive.extractFile(fileHeader, os); |
| | | os.close(); |
| | | fileHeader = archive.nextFileHeader(); |
| | | } |
| | | } catch (IOException | ArchiveException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | archive.close(); |
| | | |
| | | 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(); |
| | | } |
| | | } |
| | | archive.close(); |
| | | } catch (IOException | RarException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | public static void main(String[] args) { |