2020-12-26 19:13:50 +08:00
|
|
|
package cn.keking.service;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
2019-04-16 21:09:32 +08:00
|
|
|
import cn.keking.config.ConfigConstants;
|
2018-01-17 17:51:53 +08:00
|
|
|
import cn.keking.model.FileType;
|
2021-12-17 09:23:32 +08:00
|
|
|
import cn.keking.utils.FileHeaderRar;
|
2020-12-28 18:21:35 +08:00
|
|
|
import cn.keking.utils.KkFileUtils;
|
2020-05-13 19:40:31 +08:00
|
|
|
import cn.keking.web.filter.BaseUrlFilter;
|
2018-01-17 14:10:40 +08:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import com.github.junrar.Archive;
|
|
|
|
|
import com.github.junrar.exception.RarException;
|
|
|
|
|
import com.github.junrar.rarfile.FileHeader;
|
2021-12-17 09:23:32 +08:00
|
|
|
import net.sf.sevenzipjbinding.*;
|
|
|
|
|
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
|
|
|
|
|
import net.sf.sevenzipjbinding.simple.ISimpleInArchive;
|
2018-01-17 14:10:40 +08:00
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.math.BigDecimal;
|
2022-12-12 16:49:41 +08:00
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2018-01-17 14:10:40 +08:00
|
|
|
import java.text.CollationKey;
|
|
|
|
|
import java.text.Collator;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
2021-12-17 09:23:32 +08:00
|
|
|
import java.util.stream.Collectors;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author yudian-it
|
2020-12-26 19:13:50 +08:00
|
|
|
* create 2017/11/27
|
2018-01-17 14:10:40 +08:00
|
|
|
*/
|
|
|
|
|
@Component
|
2020-12-26 19:13:50 +08:00
|
|
|
public class CompressFileReader {
|
2020-05-15 18:09:19 +08:00
|
|
|
|
2020-12-26 19:13:50 +08:00
|
|
|
private static final Pattern pattern = Pattern.compile("^\\d+");
|
|
|
|
|
private final FileHandlerService fileHandlerService;
|
2020-05-15 18:09:19 +08:00
|
|
|
private final String fileDir = ConfigConstants.getFileDir();
|
|
|
|
|
private final ExecutorService executors = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
|
|
|
|
|
2020-12-26 19:13:50 +08:00
|
|
|
public CompressFileReader(FileHandlerService fileHandlerService) {
|
|
|
|
|
this.fileHandlerService = fileHandlerService;
|
2020-05-15 18:09:19 +08:00
|
|
|
}
|
|
|
|
|
|
2022-12-12 16:49:41 +08:00
|
|
|
public static byte[] getUTF8BytesFromGBKString(String gbkStr) {
|
|
|
|
|
int n = gbkStr.length();
|
|
|
|
|
byte[] utfBytes = new byte[3 * n];
|
|
|
|
|
int k = 0;
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
int m = gbkStr.charAt(i);
|
|
|
|
|
if (m < 128 && m >= 0) {
|
|
|
|
|
utfBytes[k++] = (byte) m;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
utfBytes[k++] = (byte) (0xe0 | (m >> 12));
|
|
|
|
|
utfBytes[k++] = (byte) (0x80 | ((m >> 6) & 0x3f));
|
|
|
|
|
utfBytes[k++] = (byte) (0x80 | (m & 0x3f));
|
|
|
|
|
}
|
|
|
|
|
if (k < utfBytes.length) {
|
|
|
|
|
byte[] tmp = new byte[k];
|
|
|
|
|
System.arraycopy(utfBytes, 0, tmp, 0, k);
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
return utfBytes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getUtf8String(String str) {
|
|
|
|
|
if (str != null && str.length() > 0) {
|
|
|
|
|
String needEncodeCode = "ISO-8859-1";
|
|
|
|
|
String neeEncodeCode = "ISO-8859-2";
|
|
|
|
|
String gbkEncodeCode = "GBK";
|
|
|
|
|
try {
|
|
|
|
|
if (Charset.forName(needEncodeCode).newEncoder().canEncode(str)) {
|
|
|
|
|
str = new String(str.getBytes(needEncodeCode), StandardCharsets.UTF_8);
|
|
|
|
|
}
|
|
|
|
|
if (Charset.forName(neeEncodeCode).newEncoder().canEncode(str)) {
|
|
|
|
|
str = new String(str.getBytes(neeEncodeCode), StandardCharsets.UTF_8);
|
|
|
|
|
}
|
|
|
|
|
if (Charset.forName(gbkEncodeCode).newEncoder().canEncode(str)) {
|
|
|
|
|
str = new String(getUTF8BytesFromGBKString(str), StandardCharsets.UTF_8);
|
|
|
|
|
}
|
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 判断是否是中日韩文字
|
|
|
|
|
*/
|
|
|
|
|
private static boolean isChinese(char c) {
|
|
|
|
|
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
|
|
|
|
|
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|
|
|
|
|
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|
|
|
|
|
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|
|
|
|
|
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|
|
|
|
|
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|
|
|
|
|
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
public static boolean judge(char c){
|
|
|
|
|
if((c >='0' && c<='9')||(c >='a' && c<='z' || c >='A' && c<='Z')){
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
public static boolean isMessyCode(String strName) {
|
|
|
|
|
//去除字符串中的空格 制表符 换行 回车
|
|
|
|
|
Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");
|
|
|
|
|
Matcher m = p.matcher(strName);
|
1,优化URL报错,2,更新OFD组件 3,美化Excel 4,文本方法关闭字节流 5,新增多种类型文件预览 (#419)
1,优化URL报错
2,更新OFD组件
3,美化Excel
4,文本方法关闭字节流
5,新增xmind、eml、epub、"obj", "3ds", "stl", "ply", "off", "3dm", "fbx", "dae", "wrl", "3mf", "ifc","glb","o3dv","gltf","stp","bim","fcstd","step","iges","brep"格式
Co-authored-by: gaoxiongzaq <admin@cxcp.com>
2022-12-28 10:17:06 +08:00
|
|
|
String after = m.replaceAll("").replaceAll("\\+", "").replaceAll("#", "").replaceAll("&", "");
|
2022-12-12 16:49:41 +08:00
|
|
|
//去除字符串中的标点符号
|
|
|
|
|
String temp = after.replaceAll("\\p{P}", "");
|
|
|
|
|
//处理之后转换成字符数组
|
|
|
|
|
char[] ch = temp.trim().toCharArray();
|
|
|
|
|
for (int i = 0; i < ch.length; i++) {
|
|
|
|
|
char c = ch[i];
|
|
|
|
|
//判断是否是数字或者英文字符
|
|
|
|
|
if (!judge(c)) {
|
|
|
|
|
//判断是否是中日韩文
|
|
|
|
|
if (!isChinese(c)) {
|
|
|
|
|
//如果不是数字或者英文字符也不是中日韩文则表示是乱码返回true
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//表示不是乱码 返回false
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-26 19:13:50 +08:00
|
|
|
public String unRar(String filePath, String fileKey) {
|
2020-12-26 01:52:52 +08:00
|
|
|
Map<String, FileNode> appender = new HashMap<>();
|
|
|
|
|
List<String> imgUrls = new ArrayList<>();
|
2020-05-13 19:40:31 +08:00
|
|
|
String baseUrl = BaseUrlFilter.getBaseUrl();
|
2018-01-17 14:10:40 +08:00
|
|
|
try {
|
2021-12-17 09:23:32 +08:00
|
|
|
List<FileHeaderRar> items = getRar4Paths(filePath);
|
2020-12-26 19:13:50 +08:00
|
|
|
String archiveFileName = fileHandlerService.getFileNameFromPath(filePath);
|
2021-12-17 09:23:32 +08:00
|
|
|
List<Map<String, FileHeaderRar>> headersToBeExtract = new ArrayList<>();
|
|
|
|
|
for (FileHeaderRar header : items) {
|
|
|
|
|
String fullName = header.getFileNameW();
|
2022-12-12 14:23:32 +08:00
|
|
|
String originName = getLastFileName(fullName);
|
2018-01-17 14:10:40 +08:00
|
|
|
String childName = originName;
|
2021-12-17 09:23:32 +08:00
|
|
|
boolean directory = header.getDirectory();
|
2018-01-17 14:10:40 +08:00
|
|
|
if (!directory) {
|
|
|
|
|
childName = archiveFileName + "_" + originName;
|
2021-12-17 09:23:32 +08:00
|
|
|
headersToBeExtract.add(Collections.singletonMap(childName, header));
|
2018-01-17 14:10:40 +08:00
|
|
|
}
|
2022-12-12 14:23:32 +08:00
|
|
|
String parentName = getLast2FileName(fullName, archiveFileName);
|
2020-12-27 14:06:06 +08:00
|
|
|
FileType type = FileType.typeFromUrl(childName);
|
2021-12-17 09:23:32 +08:00
|
|
|
if (type.equals(FileType.PICTURE)) {
|
2020-12-26 19:13:50 +08:00
|
|
|
imgUrls.add(baseUrl + childName);
|
2018-01-17 14:10:40 +08:00
|
|
|
}
|
2021-12-17 09:23:32 +08:00
|
|
|
FileNode node =
|
|
|
|
|
new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey);
|
2018-01-17 14:10:40 +08:00
|
|
|
addNodes(appender, parentName, node);
|
|
|
|
|
appender.put(childName, node);
|
|
|
|
|
}
|
2020-12-26 19:13:50 +08:00
|
|
|
fileHandlerService.putImgCache(fileKey, imgUrls);
|
2021-12-17 09:23:32 +08:00
|
|
|
executors.submit(new RarExtractorWorker(headersToBeExtract, filePath));
|
2018-01-17 14:10:40 +08:00
|
|
|
return new ObjectMapper().writeValueAsString(appender.get(""));
|
2021-12-17 09:23:32 +08:00
|
|
|
} catch (IOException e) {
|
2018-01-17 14:10:40 +08:00
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-17 09:23:32 +08:00
|
|
|
public List<FileHeaderRar> getRar4Paths(String paths) {
|
|
|
|
|
RandomAccessFile randomAccessFile = null;
|
|
|
|
|
IInArchive inArchive = null;
|
|
|
|
|
List<FileHeaderRar> itemPath = null;
|
|
|
|
|
try {
|
|
|
|
|
randomAccessFile = new RandomAccessFile(paths, "r");
|
|
|
|
|
inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile));
|
|
|
|
|
String folderName = paths.substring(paths.lastIndexOf(File.separator) + 1);
|
|
|
|
|
String extractPath = paths.substring(0, paths.lastIndexOf(folderName));
|
|
|
|
|
inArchive.extract(null, false, new ExtractCallback(inArchive, extractPath, folderName + "_"));
|
|
|
|
|
ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();
|
2022-12-12 16:49:41 +08:00
|
|
|
itemPath = Arrays.stream(simpleInArchive.getArchiveItems()).map(o -> {
|
2021-12-17 09:23:32 +08:00
|
|
|
try {
|
2022-12-12 16:49:41 +08:00
|
|
|
String path = getUtf8String(o.getPath());
|
|
|
|
|
if (isMessyCode(path)){
|
|
|
|
|
path = new String(o.getPath().getBytes(StandardCharsets.ISO_8859_1), "GBK");
|
|
|
|
|
}
|
|
|
|
|
return new FileHeaderRar(path, o.isFolder());
|
2021-12-17 09:23:32 +08:00
|
|
|
} catch (SevenZipException e) {
|
|
|
|
|
e.printStackTrace();
|
2022-12-12 16:49:41 +08:00
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
|
e.printStackTrace();
|
2021-12-17 09:23:32 +08:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
})
|
|
|
|
|
.collect(Collectors.toList())
|
|
|
|
|
.stream()
|
|
|
|
|
.sorted(Comparator.comparing(FileHeaderRar::getFileNameW))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.err.println("Error occurs: " + e);
|
|
|
|
|
} finally {
|
|
|
|
|
if (inArchive != null) {
|
|
|
|
|
try {
|
|
|
|
|
inArchive.close();
|
|
|
|
|
} catch (SevenZipException e) {
|
|
|
|
|
System.err.println("Error closing archive: " + e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (randomAccessFile != null) {
|
|
|
|
|
try {
|
|
|
|
|
randomAccessFile.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
System.err.println("Error closing file: " + e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return itemPath;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-17 14:10:40 +08:00
|
|
|
private void addNodes(Map<String, FileNode> appender, String parentName, FileNode node) {
|
|
|
|
|
if (appender.containsKey(parentName)) {
|
|
|
|
|
appender.get(parentName).getChildList().add(node);
|
2020-05-15 18:09:19 +08:00
|
|
|
appender.get(parentName).getChildList().sort(sortComparator);
|
|
|
|
|
} else {
|
|
|
|
|
// 根节点
|
2018-01-17 14:10:40 +08:00
|
|
|
FileNode nodeRoot = new FileNode(parentName, parentName, "", new ArrayList<>(), true);
|
|
|
|
|
nodeRoot.getChildList().add(node);
|
|
|
|
|
appender.put("", nodeRoot);
|
|
|
|
|
appender.put(parentName, nodeRoot);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-12 14:23:32 +08:00
|
|
|
private static String getLast2FileName(String fullName, String rootName) {
|
|
|
|
|
if (fullName.endsWith(File.separator)) {
|
2020-12-26 19:13:50 +08:00
|
|
|
fullName = fullName.substring(0, fullName.length() - 1);
|
2018-01-17 14:10:40 +08:00
|
|
|
}
|
|
|
|
|
// 1.获取剩余部分
|
2022-12-12 14:23:32 +08:00
|
|
|
int endIndex = fullName.lastIndexOf(File.separator);
|
2018-01-17 14:10:40 +08:00
|
|
|
String leftPath = fullName.substring(0, endIndex == -1 ? 0 : endIndex);
|
2020-05-15 18:09:19 +08:00
|
|
|
if (leftPath.length() > 1) {
|
2018-01-17 14:10:40 +08:00
|
|
|
// 2.获取倒数第二个
|
2022-12-12 14:23:32 +08:00
|
|
|
return getLastFileName(leftPath);
|
2020-05-15 18:09:19 +08:00
|
|
|
} else {
|
2018-01-17 14:10:40 +08:00
|
|
|
return rootName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-12 14:23:32 +08:00
|
|
|
private static String getLastFileName(String fullName) {
|
|
|
|
|
if (fullName.endsWith(File.separator)) {
|
2020-12-26 19:13:50 +08:00
|
|
|
fullName = fullName.substring(0, fullName.length() - 1);
|
2018-01-17 14:10:40 +08:00
|
|
|
}
|
|
|
|
|
String newName = fullName;
|
2022-12-12 14:23:32 +08:00
|
|
|
if (fullName.contains(File.separator)) {
|
|
|
|
|
newName = fullName.substring(fullName.lastIndexOf(File.separator) + 1);
|
2018-01-17 14:10:40 +08:00
|
|
|
}
|
|
|
|
|
return newName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Comparator<FileNode> sortComparator = new Comparator<FileNode>() {
|
2020-05-15 18:09:19 +08:00
|
|
|
final Collator cmp = Collator.getInstance(Locale.US);
|
2020-12-26 19:13:50 +08:00
|
|
|
|
2018-01-17 14:10:40 +08:00
|
|
|
@Override
|
|
|
|
|
public int compare(FileNode o1, FileNode o2) {
|
|
|
|
|
// 判断两个对比对象是否是开头包含数字,如果包含数字则获取数字并按数字真正大小进行排序
|
2020-12-26 19:13:50 +08:00
|
|
|
BigDecimal num1, num2;
|
2018-01-17 14:10:40 +08:00
|
|
|
if (null != (num1 = isStartNumber(o1))
|
|
|
|
|
&& null != (num2 = isStartNumber(o2))) {
|
|
|
|
|
return num1.subtract(num2).intValue();
|
|
|
|
|
}
|
|
|
|
|
CollationKey c1 = cmp.getCollationKey(o1.getOriginName());
|
|
|
|
|
CollationKey c2 = cmp.getCollationKey(o2.getOriginName());
|
|
|
|
|
return cmp.compare(c1.getSourceString(), c2.getSourceString());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private static BigDecimal isStartNumber(FileNode src) {
|
|
|
|
|
Matcher matcher = pattern.matcher(src.getOriginName());
|
|
|
|
|
if (matcher.find()) {
|
|
|
|
|
return new BigDecimal(matcher.group());
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 18:09:19 +08:00
|
|
|
public static class FileNode {
|
2018-01-17 14:10:40 +08:00
|
|
|
|
|
|
|
|
private String originName;
|
|
|
|
|
private String fileName;
|
|
|
|
|
private String parentFileName;
|
|
|
|
|
private boolean directory;
|
2020-05-15 18:09:19 +08:00
|
|
|
//用于图片预览时寻址
|
|
|
|
|
private String fileKey;
|
2018-01-17 14:10:40 +08:00
|
|
|
private List<FileNode> childList;
|
|
|
|
|
|
|
|
|
|
public FileNode(String originName, String fileName, String parentFileName, List<FileNode> childList, boolean directory) {
|
|
|
|
|
this.originName = originName;
|
|
|
|
|
this.fileName = fileName;
|
|
|
|
|
this.parentFileName = parentFileName;
|
|
|
|
|
this.childList = childList;
|
|
|
|
|
this.directory = directory;
|
|
|
|
|
}
|
2020-12-26 19:13:50 +08:00
|
|
|
|
|
|
|
|
public FileNode(String originName, String fileName, String parentFileName, List<FileNode> childList, boolean directory, String fileKey) {
|
2018-01-17 14:10:40 +08:00
|
|
|
this.originName = originName;
|
|
|
|
|
this.fileName = fileName;
|
|
|
|
|
this.parentFileName = parentFileName;
|
|
|
|
|
this.childList = childList;
|
|
|
|
|
this.directory = directory;
|
2020-12-26 19:13:50 +08:00
|
|
|
this.fileKey = fileKey;
|
2018-01-17 14:10:40 +08:00
|
|
|
}
|
2020-12-26 19:13:50 +08:00
|
|
|
|
2018-01-17 14:10:40 +08:00
|
|
|
public String getFileKey() {
|
|
|
|
|
return fileKey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setFileKey(String fileKey) {
|
|
|
|
|
this.fileKey = fileKey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getFileName() {
|
|
|
|
|
return fileName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setFileName(String fileName) {
|
|
|
|
|
this.fileName = fileName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getParentFileName() {
|
|
|
|
|
return parentFileName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setParentFileName(String parentFileName) {
|
|
|
|
|
this.parentFileName = parentFileName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<FileNode> getChildList() {
|
|
|
|
|
return childList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setChildList(List<FileNode> childList) {
|
|
|
|
|
this.childList = childList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
try {
|
|
|
|
|
return new ObjectMapper().writeValueAsString(this);
|
|
|
|
|
} catch (JsonProcessingException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getOriginName() {
|
|
|
|
|
return originName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setOriginName(String originName) {
|
|
|
|
|
this.originName = originName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isDirectory() {
|
|
|
|
|
return directory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setDirectory(boolean directory) {
|
|
|
|
|
this.directory = directory;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class RarExtractorWorker implements Runnable {
|
2020-05-15 18:09:19 +08:00
|
|
|
private final List<Map<String, FileHeader>> headersToBeExtracted;
|
2021-12-17 09:23:32 +08:00
|
|
|
|
|
|
|
|
private final List<Map<String, FileHeaderRar>> headersToBeExtract;
|
|
|
|
|
|
2020-05-15 18:09:19 +08:00
|
|
|
private final Archive archive;
|
2018-01-17 14:10:40 +08:00
|
|
|
/**
|
|
|
|
|
* 用以删除源文件
|
|
|
|
|
*/
|
2020-05-15 18:09:19 +08:00
|
|
|
private final String filePath;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
2021-12-17 09:23:32 +08:00
|
|
|
public RarExtractorWorker(
|
|
|
|
|
List<Map<String, FileHeader>> headersToBeExtracted, Archive archive, String filePath) {
|
2018-01-17 14:10:40 +08:00
|
|
|
this.headersToBeExtracted = headersToBeExtracted;
|
|
|
|
|
this.archive = archive;
|
|
|
|
|
this.filePath = filePath;
|
2021-12-17 09:23:32 +08:00
|
|
|
headersToBeExtract = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RarExtractorWorker(
|
|
|
|
|
List<Map<String, FileHeaderRar>> headersToBeExtract, String filePath) {
|
|
|
|
|
this.headersToBeExtract = headersToBeExtract;
|
|
|
|
|
this.filePath = filePath;
|
|
|
|
|
archive = null;
|
|
|
|
|
headersToBeExtracted = null;
|
2018-01-17 14:10:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
for (Map<String, FileHeader> entryMap : headersToBeExtracted) {
|
|
|
|
|
String childName = entryMap.keySet().iterator().next();
|
|
|
|
|
extractRarFile(childName, entryMap.values().iterator().next(), archive);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
archive.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
2020-12-28 18:21:35 +08:00
|
|
|
KkFileUtils.deleteFileByPath(filePath);
|
2018-01-17 14:10:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void extractRarFile(String childName, FileHeader header, Archive archive) {
|
|
|
|
|
String outPath = fileDir + childName;
|
2020-12-26 19:13:50 +08:00
|
|
|
try (OutputStream ot = new FileOutputStream(outPath)) {
|
2018-01-17 14:10:40 +08:00
|
|
|
archive.extractFile(header, ot);
|
2020-05-15 18:09:19 +08:00
|
|
|
} catch (IOException | RarException e) {
|
2018-01-17 14:10:40 +08:00
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-17 09:23:32 +08:00
|
|
|
|
|
|
|
|
private static class ExtractCallback implements IArchiveExtractCallback {
|
|
|
|
|
private final IInArchive inArchive;
|
|
|
|
|
|
|
|
|
|
private final String extractPath;
|
|
|
|
|
private final String folderName;
|
|
|
|
|
|
|
|
|
|
public ExtractCallback(IInArchive inArchive, String extractPath, String folderName) {
|
|
|
|
|
this.inArchive = inArchive;
|
|
|
|
|
if (!extractPath.endsWith("/") && !extractPath.endsWith("\\")) {
|
|
|
|
|
extractPath += File.separator;
|
|
|
|
|
}
|
|
|
|
|
this.extractPath = extractPath;
|
|
|
|
|
this.folderName = folderName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setTotal(long total) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setCompleted(long complete) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ISequentialOutStream getStream(int index, ExtractAskMode extractAskMode) throws SevenZipException {
|
|
|
|
|
String filePath = inArchive.getStringProperty(index, PropID.PATH);
|
|
|
|
|
String real = folderName + filePath.substring(filePath.lastIndexOf(File.separator) + 1);
|
|
|
|
|
File f = new File(extractPath + real);
|
|
|
|
|
f.delete();
|
|
|
|
|
return data -> {
|
|
|
|
|
FileOutputStream fos = null;
|
|
|
|
|
try {
|
|
|
|
|
File path = new File(extractPath + real);
|
|
|
|
|
if (!path.getParentFile().exists()) {
|
|
|
|
|
path.getParentFile().mkdirs();
|
|
|
|
|
}
|
|
|
|
|
if (!path.exists()) {
|
|
|
|
|
path.createNewFile();
|
|
|
|
|
}
|
|
|
|
|
fos = new FileOutputStream(path, true);
|
|
|
|
|
fos.write(data);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} finally {
|
|
|
|
|
try {
|
|
|
|
|
if (fos != null) {
|
|
|
|
|
fos.flush();
|
|
|
|
|
fos.close();
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return data.length;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void prepareOperation(ExtractAskMode extractAskMode) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setOperationResult(ExtractOperationResult extractOperationResult) {
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-17 14:10:40 +08:00
|
|
|
}
|