package com.hotent.file.util;
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;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hotent.file.model.FileType;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
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;
/**
* 压缩文件读取
*
* @company 广州宏天软件股份有限公司
* @author heyifan
* @email heyf@jee-soft.cn
* @date 2018年9月30日
*/
@SuppressWarnings(value = "unchecked")
@Component
public class ZipReader {
static Pattern pattern = Pattern.compile("^\\d+");
@Autowired
FileUtils fileUtils;
@Value("${file.file.dir}")
String fileDir;
ExecutorService executors = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
/**
* 读取压缩文件
* 文件压缩到统一目录fileDir下,并且命名使用压缩文件名+文件名因为文件名
* 可能会重复(在系统中对于同一种类型的材料压缩文件内的文件是一样的,如果文件名
* 重复,那么这里会被覆盖[同一个压缩文件中的不同目录中的相同文件名暂时不考虑])
* 注:
*
* 文件名命名中的参数的说明:
* 1.archiveName,为避免解压的文件中有重名的文件会彼此覆盖,所以加上了archiveName,因为在ufile中archiveName
* 是不会重复的。
* 2.level,这里层级结构的列表我是通过一个map来构造的,map的key是文件的名字,值是对应的文件,这样每次向map中
* 加入节点的时候都会获取父节点是否存在,存在则会获取父节点的value并将当前节点加入到父节点的childList中(这里利用
* 的是java语言的引用的特性)。
*
* @param filePath
*/
public String readZipFile(String filePath,String fileKey) {
fileDir=filePath.substring(0,filePath.lastIndexOf(File.separator)+1);
String archiveSeparator = "/";
Map appender = Maps.newHashMap();
List imgUrls=new ArrayList();
String archiveFileName = fileUtils.getFileNameFromPath(filePath);
try (ZipFile zipFile = new ZipFile(filePath, fileUtils.getFileEncodeUTFGBK(filePath));){
Enumeration entries = zipFile.getEntries();
// 排序
entries = sortZipEntries(entries);
List