|
|
@@ -2,6 +2,10 @@ package com.izouma.wenlvju.utils;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import com.github.junrar.Archive;
|
|
|
+import com.github.junrar.exception.RarException;
|
|
|
+import com.github.junrar.rarfile.FileHeader;
|
|
|
+import com.izouma.wenlvju.exception.BusinessException;
|
|
|
import org.apache.commons.io.FilenameUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
@@ -13,6 +17,7 @@ import java.nio.file.attribute.PosixFileAttributes;
|
|
|
import java.nio.file.attribute.PosixFilePermission;
|
|
|
import java.nio.file.attribute.PosixFilePermissions;
|
|
|
import java.util.*;
|
|
|
+import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
public class FileUtils {
|
|
|
@@ -204,45 +209,6 @@ public class FileUtils {
|
|
|
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- public static Map<String, File> findExcel(File dir) {
|
|
|
- if (!(dir.exists() && dir.isDirectory())) return null;
|
|
|
- for (File file : dir.listFiles()) {
|
|
|
- String name = file.getName().toLowerCase();
|
|
|
- if ((name.endsWith(".xlsx") || name.endsWith(".xls")) && !name.startsWith(".") && !file.isHidden()) {
|
|
|
- Map<String, File> fileMap = new HashMap<>();
|
|
|
- fileMap.put("file", file);
|
|
|
- fileMap.put("destDir", dir);
|
|
|
- return fileMap;
|
|
|
- } else if (file.isDirectory() && !file.isHidden()) {
|
|
|
- return findExcel(file);
|
|
|
- }
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- public static File findByName(File dir, String fileName) {
|
|
|
- if (!(dir.exists() && dir.isDirectory())) return null;
|
|
|
- for (File file : dir.listFiles()) {
|
|
|
- String name = file.getName().toLowerCase();
|
|
|
- if (name.contains(fileName)) {
|
|
|
- return file;
|
|
|
- }
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- public static File findExcel1(File dir) {
|
|
|
- if (!(dir.exists() && dir.isDirectory())) return null;
|
|
|
- for (File file : dir.listFiles()) {
|
|
|
- String name = file.getName().toLowerCase();
|
|
|
- if ((name.endsWith(".xlsx") || name.endsWith(".xls")) && !name.startsWith(".") && !file.isHidden()) {
|
|
|
- return file;
|
|
|
- }
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
public static File findInDir(File dir, String fileName) {
|
|
|
if (dir.isDirectory()) {
|
|
|
for (File file : dir.listFiles()) {
|
|
|
@@ -309,4 +275,64 @@ public class FileUtils {
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
+ public static void unrar(InputStream sourceRar, File destDir) throws Exception {
|
|
|
+ Archive archive = null;
|
|
|
+ FileOutputStream fos = null;
|
|
|
+ System.out.println("Starting 开始解压...");
|
|
|
+ try {
|
|
|
+ archive = new Archive(sourceRar);
|
|
|
+ FileHeader fh = archive.nextFileHeader();
|
|
|
+ File destFileName;
|
|
|
+ while (fh != null) {
|
|
|
+
|
|
|
+ //中文名称乱码
|
|
|
+ String fileName = fh.getFileNameW().replaceAll("/", File.separator).replaceAll("\\\\", File.separator);
|
|
|
+ if (StringUtils.isBlank(fileName)) {
|
|
|
+ fileName = fh.getFileNameString()
|
|
|
+ .replaceAll("/", File.separator)
|
|
|
+ .replaceAll("\\\\", File.separator);
|
|
|
+ }
|
|
|
+
|
|
|
+// String compressFileName = fh.getFileNameString().trim();
|
|
|
+ destFileName = new File(destDir.getAbsolutePath() + "/" + fileName);
|
|
|
+ if (fh.isDirectory()) {
|
|
|
+ if (!destFileName.exists()) {
|
|
|
+ destFileName.mkdirs();
|
|
|
+ }
|
|
|
+ fh = archive.nextFileHeader();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (!destFileName.getParentFile().exists()) {
|
|
|
+ destFileName.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ fos = new FileOutputStream(destFileName);
|
|
|
+ archive.extractFile(fh, fos);
|
|
|
+ fos.close();
|
|
|
+ fos = null;
|
|
|
+ fh = archive.nextFileHeader();
|
|
|
+ }
|
|
|
+
|
|
|
+ archive.close();
|
|
|
+ archive = null;
|
|
|
+ System.out.println("解压完毕...");
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw e;
|
|
|
+ } finally {
|
|
|
+ if (fos != null) {
|
|
|
+ try {
|
|
|
+ fos.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (archive != null) {
|
|
|
+ try {
|
|
|
+ archive.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|