2020-12-26 19:13:50 +08:00
|
|
|
package cn.keking.service;
|
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;
|
2020-05-13 19:40:31 +08:00
|
|
|
import cn.keking.web.filter.BaseUrlFilter;
|
2023-04-03 07:07:30 +00:00
|
|
|
import net.sf.sevenzipjbinding.ExtractOperationResult;
|
|
|
|
|
import net.sf.sevenzipjbinding.IInArchive;
|
|
|
|
|
import net.sf.sevenzipjbinding.SevenZip;
|
|
|
|
|
import net.sf.sevenzipjbinding.SevenZipException;
|
2021-12-17 09:23:32 +08:00
|
|
|
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
|
|
|
|
|
import net.sf.sevenzipjbinding.simple.ISimpleInArchive;
|
2023-04-03 07:07:30 +00:00
|
|
|
import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem;
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
2018-01-17 14:10:40 +08:00
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
2022-12-12 16:49:41 +08:00
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2023-04-03 07:07:30 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
2018-01-17 14:10:40 +08:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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 {
|
|
|
|
|
private final FileHandlerService fileHandlerService;
|
|
|
|
|
public CompressFileReader(FileHandlerService fileHandlerService) {
|
|
|
|
|
this.fileHandlerService = fileHandlerService;
|
2020-05-15 18:09:19 +08:00
|
|
|
}
|
2023-04-03 07:07:30 +00:00
|
|
|
private static final String fileDir = ConfigConstants.getFileDir();
|
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);
|
2023-04-03 07:07:30 +00:00
|
|
|
return ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|
2022-12-12 16:49:41 +08:00
|
|
|
|| 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
|
2023-04-03 07:07:30 +00:00
|
|
|
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS;
|
2022-12-12 16:49:41 +08:00
|
|
|
}
|
|
|
|
|
public static boolean judge(char c){
|
2023-04-03 07:07:30 +00:00
|
|
|
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z');
|
2022-12-12 16:49:41 +08:00
|
|
|
}
|
|
|
|
|
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();
|
2023-04-03 07:07:30 +00:00
|
|
|
for (char c : ch) {
|
2022-12-12 16:49:41 +08:00
|
|
|
//判断是否是数字或者英文字符
|
|
|
|
|
if (!judge(c)) {
|
|
|
|
|
//判断是否是中日韩文
|
|
|
|
|
if (!isChinese(c)) {
|
|
|
|
|
//如果不是数字或者英文字符也不是中日韩文则表示是乱码返回true
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//表示不是乱码 返回false
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-04-03 07:07:30 +00:00
|
|
|
public String unRar(String paths, String passWord, String fileName) {
|
2020-12-26 01:52:52 +08:00
|
|
|
List<String> imgUrls = new ArrayList<>();
|
2020-05-13 19:40:31 +08:00
|
|
|
String baseUrl = BaseUrlFilter.getBaseUrl();
|
2023-04-03 07:07:30 +00:00
|
|
|
String archiveFileName = fileHandlerService.getFileNameFromPath(paths);
|
2021-12-17 09:23:32 +08:00
|
|
|
RandomAccessFile randomAccessFile = null;
|
|
|
|
|
IInArchive inArchive = 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));
|
2023-04-03 07:07:30 +00:00
|
|
|
ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();
|
|
|
|
|
final String[] str = {null};
|
|
|
|
|
for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
|
|
|
|
|
if (!item.isFolder()) {
|
|
|
|
|
ExtractOperationResult result;
|
|
|
|
|
result = item.extractSlow(data -> {
|
|
|
|
|
try {
|
|
|
|
|
str[0] = getUtf8String(item.getPath());
|
|
|
|
|
if (isMessyCode(str[0])){
|
|
|
|
|
str[0] = new String(item.getPath().getBytes(StandardCharsets.ISO_8859_1), "gbk");
|
|
|
|
|
}
|
|
|
|
|
str[0] = str[0].replace("\\", File.separator); //Linux 下路径错误
|
|
|
|
|
String str1 = str[0].substring(0, str[0].lastIndexOf(File.separator)+ 1);
|
|
|
|
|
File file = new File(extractPath, folderName + "_" + File.separator + str1);
|
|
|
|
|
if (!file.exists()) {
|
|
|
|
|
file.mkdirs();
|
|
|
|
|
}
|
|
|
|
|
OutputStream out = new FileOutputStream( extractPath+ folderName + "_" + File.separator + str[0], true);
|
|
|
|
|
IOUtils.write(data, out);
|
|
|
|
|
out.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return data.length;
|
|
|
|
|
}, passWord);
|
|
|
|
|
if (result == ExtractOperationResult.OK) {
|
|
|
|
|
FileType type = FileType.typeFromUrl(str[0]);
|
|
|
|
|
if (type.equals(FileType.PICTURE)) {
|
|
|
|
|
// System.out.println( baseUrl +folderName + "_" + str[0]);
|
|
|
|
|
imgUrls.add(baseUrl +folderName + "_/" + str[0].replace("\\", "/"));
|
|
|
|
|
}
|
|
|
|
|
fileHandlerService.putImgCache(fileName, imgUrls);
|
|
|
|
|
} else {
|
|
|
|
|
return "error";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return archiveFileName + "_";
|
2021-12-17 09:23:32 +08:00
|
|
|
} catch (Exception e) {
|
2023-04-03 07:07:30 +00:00
|
|
|
String Str1 = String.valueOf(e);
|
|
|
|
|
if (Str1.contains("Password")) {
|
|
|
|
|
return "Password";
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2021-12-17 09:23:32 +08:00
|
|
|
} 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-03 07:07:30 +00:00
|
|
|
/**
|
|
|
|
|
* 读取文件目录树
|
|
|
|
|
*/
|
|
|
|
|
public static List<ZtreeNodeVo> getTree(String rootPath) {
|
|
|
|
|
List<ZtreeNodeVo> nodes = new ArrayList<>();
|
|
|
|
|
File file = new File(fileDir+rootPath);
|
|
|
|
|
ZtreeNodeVo node = traverse(file);
|
|
|
|
|
nodes.add(node);
|
|
|
|
|
return nodes;
|
2018-01-17 14:10:40 +08:00
|
|
|
}
|
2023-04-03 07:07:30 +00:00
|
|
|
private static ZtreeNodeVo traverse(File file) {
|
|
|
|
|
ZtreeNodeVo pathNodeVo = new ZtreeNodeVo();
|
|
|
|
|
pathNodeVo.setId(file.getAbsolutePath().replace(fileDir, "").replace("\\", "/"));
|
|
|
|
|
pathNodeVo.setName(file.getName());
|
|
|
|
|
pathNodeVo.setPid(file.getParent().replace(fileDir, "").replace("\\", "/"));
|
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
|
List<ZtreeNodeVo> subNodeVos = new ArrayList<>();
|
|
|
|
|
File[] subFiles = file.listFiles();
|
|
|
|
|
if (subFiles == null) {
|
|
|
|
|
return pathNodeVo;
|
2018-01-17 14:10:40 +08:00
|
|
|
}
|
2023-04-03 07:07:30 +00:00
|
|
|
for (File subFile : subFiles) {
|
|
|
|
|
ZtreeNodeVo subNodeVo = traverse(subFile);
|
|
|
|
|
subNodeVos.add(subNodeVo);
|
2018-01-17 14:10:40 +08:00
|
|
|
}
|
2023-04-03 07:07:30 +00:00
|
|
|
pathNodeVo.setChildren(subNodeVos);
|
2018-01-17 14:10:40 +08:00
|
|
|
}
|
2023-04-03 07:07:30 +00:00
|
|
|
return pathNodeVo;
|
2021-12-17 09:23:32 +08:00
|
|
|
}
|
2018-01-17 14:10:40 +08:00
|
|
|
}
|