| | |
| | | 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; |
| | | import org.apache.commons.compress.archivers.ArchiveStreamFactory; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.*; |
| | | import java.net.URLEncoder; |
| | | import java.util.ArrayList; |
| | | import java.util.LinkedList; |
| | | import java.util.List; |
| | | |
| | | public class FileUtil { |
| | |
| | | if (fileTemp.isDirectory()){ |
| | | //递归再次进行判断 |
| | | getStaticFilePath(fileTemp, list); |
| | | }else{ |
| | | //子级是文件 |
| | | String absolutePath = fileTemp.getAbsolutePath(); |
| | | list.add(absolutePath); |
| | | //System.out.println(temp + "文件 :" + fileTemp.getName() + "\t"); |
| | | } |
| | | } |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | public static List<String> getStaticFilePathII(File file, List<String> list){ |
| | | |
| | | //如果是文件的情况 |
| | | if (file.isFile()){ |
| | | list.add(file.getAbsolutePath()); |
| | | }else{ |
| | | //如果是目录的情况 |
| | | //创建一个File数组来存储当前目录下所有文件和目录的绝对路径 |
| | | File[] files = file.listFiles(); |
| | | //循环遍历files |
| | | for (File fileTemp : files){ |
| | | //子级是目录 |
| | | if (fileTemp.isDirectory()){ |
| | | //递归再次进行判断 |
| | | getStaticFilePathII(fileTemp, list); |
| | | }else{ |
| | | //子级是文件 |
| | | String absolutePath = fileTemp.getAbsolutePath(); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 解压N层 |
| | | * @param compressedFileUrl doc_file/xxx/xxx.zip 或者 rar |
| | | * @return |
| | | */ |
| | | public static List<String> decompress(String compressedFileUrl){ |
| | | List<Object> resList = decompressOne(compressedFileUrl); |
| | | File outputFolderFile = (File) resList.get(0); |
| | | List<String> fileList = (List<String>) resList.get(1); |
| | | boolean existCompressedFile = false; |
| | | int checkDecompress = 0; |
| | | for(String tempFileName:fileList){ |
| | | 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; |
| | | } |
| | | } |
| | | if(existCompressedFile){ |
| | | checkDecompress ++; |
| | | } |
| | | for (int i = 0; i < checkDecompress; i++) { |
| | | decompress(compressedFileUrl); |
| | | } |
| | | |
| | | List<String> finalList = new LinkedList<>(); |
| | | getStaticFilePathII(outputFolderFile,finalList); |
| | | return finalList; |
| | | } |
| | | |
| | | /** |
| | | * 解压一层 |
| | | * @param compressedFileUrl doc_file/xxx/xxx.zip 或者 rar |
| | | * @return |
| | | */ |
| | | public static List<Object> decompressOne(String compressedFileUrl){ |
| | | List<Object> resList = new LinkedList<>(); |
| | | String projectDir = CommonUtil.getProjectDir() + File.separator; |
| | | String fullFilePath; |
| | | String separator = File.separator; |
| | | String outputFolderSuffix; |
| | | if(compressedFileUrl.startsWith(projectDir)) { // 非第一层解压,已经是全路径 |
| | | fullFilePath = compressedFileUrl; |
| | | compressedFileUrl = compressedFileUrl.substring(compressedFileUrl.indexOf("decompress")+11); |
| | | //outputFolderSuffix = compressedFileUrl.substring(0,compressedFileUrl.lastIndexOf(separator)) + separator + "decompress_" + compressedFileUrl.substring(compressedFileUrl.lastIndexOf(separator)+1); |
| | | outputFolderSuffix = compressedFileUrl +"_decompress"; |
| | | }else{ //第一层解压 |
| | | fullFilePath = projectDir + compressedFileUrl; |
| | | outputFolderSuffix = compressedFileUrl.replace(separator, "@"); |
| | | } |
| | | File file = new File(fullFilePath); |
| | | |
| | | |
| | | String outputFolder = CommonUtil.getRootFile() + separator + "decompress" + separator + outputFolderSuffix; |
| | | File outputFolderFile = new File(outputFolder); |
| | | if(!outputFolderFile.exists()){ |
| | | outputFolderFile.mkdirs(); |
| | | if(compressedFileUrl.endsWith("zip")){ |
| | | decompressZip(file, outputFolder); |
| | | }else { //rar |
| | | decompressRar(file, outputFolder); |
| | | } |
| | | } |
| | | //返回文件夹所有文件 |
| | | LinkedList<String> list = new LinkedList<>(); |
| | | getStaticFilePathII(outputFolderFile,list); |
| | | resList.add(outputFolderFile); |
| | | resList.add(list); |
| | | 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(); |
| | | } |
| | | } 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(); |
| | | } |
| | | } |
| | | archive.close(); |
| | | } catch (IOException | RarException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | File file = new File("C:\\Users\\29550\\Desktop\\当前项目\\202207泰州平台"); |
| | | List list = new ArrayList<>(); |
| | | list = getStaticFilePath( file,list); |
| | | System.out.println(list); |
| | | //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"); |
| | | if(file.exists()) { |
| | | boolean delete = file.delete(); |
| | | System.out.println(delete); |
| | | } |
| | | } |
| | | } |