| | |
| | | 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 javax.servlet.http.HttpServletResponse; |
| | | import java.io.*; |
| | | import java.net.URLEncoder; |
| | | import java.nio.file.Files; |
| | | import java.util.ArrayList; |
| | | import java.util.LinkedList; |
| | | import java.util.List; |
| | | |
| | | public class FileUtil { |
| | | |
| | | |
| | | public static List<String> getStaticFilePath(File file, List<String> list){ |
| | | |
| | |
| | | System.out.println("文件删除失败,请检查文件是否存在以及文件路径是否正确"); |
| | | return false; |
| | | } |
| | | //获取目录下子文件 |
| | | File[] files = file.listFiles(); |
| | | //遍历该目录下的文件对象 |
| | | for (File f : files) { |
| | | //判断子目录是否存在子目录,如果是文件则删除 |
| | | if (f.isDirectory()) { |
| | | //递归删除目录下的文件 |
| | | deleteFile(f); |
| | | } else { |
| | | //文件删除 |
| | | f.delete(); |
| | | //打印文件名 |
| | | if(file.isDirectory()){ |
| | | //获取目录下子文件 |
| | | File[] files = file.listFiles(); |
| | | //遍历该目录下的文件对象 |
| | | for (File f : files) { |
| | | //判断子目录是否存在子目录,如果是文件则删除 |
| | | if (f.isDirectory()) { |
| | | //递归删除目录下的文件 |
| | | deleteFile(f); |
| | | } else { |
| | | //文件删除 |
| | | f.delete(); |
| | | //打印文件名 |
| | | } |
| | | } |
| | | //文件夹删除 |
| | | file.delete(); |
| | | }else{ |
| | | file.delete(); |
| | | } |
| | | //文件夹删除 |
| | | file.delete(); |
| | | return true; |
| | | } |
| | | |
| | |
| | | * @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); |
| | | 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(); |
| | | //解除文件占用并删除文件 |
| | | //boolean delete = file.delete(); |
| | | Files.delete(file.toPath()); |
| | | existCompressedFile = true; |
| | | } |
| | | } |
| | | //如果存在压缩文件并已解压,则需要检查一次是否还有压缩文件 |
| | | if(existCompressedFile){ |
| | | checkDecompress ++; |
| | | } |
| | |
| | | * @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 decompressZip(File file, String outputFolder) throws IOException, ArchiveException { |
| | | ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream("zip", new FileInputStream(file)); |
| | | ArchiveInputStream ais = new ArchiveStreamFactory("GBK").createArchiveInputStream("zip", new FileInputStream(file)); |
| | | ArchiveEntry entry; |
| | | while ((entry = ais.getNextEntry()) != null) { |
| | | if (!ais.canReadEntryData(entry) || entry.isDirectory()) { |
| | |
| | | outputStream.write(buffer, 0, bytesRead); |
| | | } |
| | | |
| | | //关流 |
| | | outputStream.close(); |
| | | } |
| | | //关流 |
| | | ais.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; |
| | | //读取文件夹下的所有文件(不读取文件夹内的文件) |
| | | public static List getFileNameWithOutDirectory(String filePath) { |
| | | File folder = new File(filePath); // 文件夹路径 |
| | | List nameList=new ArrayList(); |
| | | File[] listOfFiles = folder.listFiles(); |
| | | if (listOfFiles != null) { |
| | | for (File file : listOfFiles) { |
| | | if (file.isFile()) { |
| | | nameList.add(file.getName()); |
| | | } |
| | | 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(); |
| | | |
| | | return nameList; |
| | | } |
| | | |
| | | //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"); |