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.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
|
|
|
|
import org.apache.commons.compress.archivers.zip.ZipFile;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
itemPath =
|
|
|
|
|
Arrays.stream(simpleInArchive.getArchiveItems())
|
|
|
|
|
.map(
|
|
|
|
|
o -> {
|
|
|
|
|
try {
|
|
|
|
|
return new FileHeaderRar(o.getPath(), o.isFolder());
|
|
|
|
|
} catch (SevenZipException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-16 17:44:34 +08:00
|
|
|
|
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 ZipExtractorWorker implements Runnable {
|
|
|
|
|
|
2020-05-15 18:09:19 +08:00
|
|
|
private final List<Map<String, ZipArchiveEntry>> entriesToBeExtracted;
|
|
|
|
|
private final ZipFile zipFile;
|
|
|
|
|
private final String filePath;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
|
|
|
|
public ZipExtractorWorker(List<Map<String, ZipArchiveEntry>> entriesToBeExtracted, ZipFile zipFile, String filePath) {
|
|
|
|
|
this.entriesToBeExtracted = entriesToBeExtracted;
|
|
|
|
|
this.zipFile = zipFile;
|
|
|
|
|
this.filePath = filePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
for (Map<String, ZipArchiveEntry> entryMap : entriesToBeExtracted) {
|
|
|
|
|
String childName = entryMap.keySet().iterator().next();
|
|
|
|
|
ZipArchiveEntry entry = entryMap.values().iterator().next();
|
|
|
|
|
try {
|
|
|
|
|
extractZipFile(childName, zipFile.getInputStream(entry));
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
zipFile.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 extractZipFile(String childName, InputStream zipFile) {
|
|
|
|
|
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
|
|
|
byte[] inByte = new byte[1024];
|
|
|
|
|
int len;
|
2020-12-26 19:13:50 +08:00
|
|
|
while ((-1 != (len = zipFile.read(inByte)))) {
|
2018-01-17 14:10:40 +08:00
|
|
|
ot.write(inByte, 0, len);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-16 17:44:34 +08:00
|
|
|
}
|
|
|
|
|
|
2018-01-17 14:10:40 +08:00
|
|
|
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
|
|
|
}
|