Skip to content

Commit 342c391

Browse files
committed
引入cpdetector解决文件编码识别问题
1 parent f2d929e commit 342c391

23 files changed

+240
-400
lines changed

server/lib/cpdetector-1.04.jar

507 KB
Binary file not shown.

server/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@
193193
<scope>system</scope>
194194
<systemPath>${basedir}/lib/aspose-cad-19.9.jar</systemPath>
195195
</dependency>
196+
<!-- 编码识别 -->
197+
<dependency>
198+
<groupId>cpdetector</groupId>
199+
<artifactId>cpdetector</artifactId>
200+
<version>1.04</version>
201+
<scope>system</scope>
202+
<systemPath>${basedir}/lib/cpdetector-1.04.jar</systemPath>
203+
</dependency>
196204
</dependencies>
197205
<build>
198206
<resources>

server/src/main/java/cn/keking/utils/ZipReader.java renamed to server/src/main/java/cn/keking/service/CompressFileReader.java

Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package cn.keking.utils;
1+
package cn.keking.service;
22

33
import cn.keking.config.ConfigConstants;
44
import cn.keking.model.FileType;
5-
import cn.keking.service.FilePreviewCommonService;
5+
import cn.keking.utils.FileUtils;
66
import cn.keking.web.filter.BaseUrlFilter;
77
import com.fasterxml.jackson.core.JsonProcessingException;
88
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -26,37 +26,34 @@
2626
import java.util.regex.Pattern;
2727

2828
/**
29-
*
3029
* @author yudian-it
31-
* @date 2017/11/27
30+
* create 2017/11/27
3231
*/
3332
@Component
34-
public class ZipReader {
35-
static Pattern pattern = Pattern.compile("^\\d+");
36-
37-
private final FilePreviewCommonService filePreviewCommonService;
33+
public class CompressFileReader {
3834

35+
private static final Pattern pattern = Pattern.compile("^\\d+");
36+
private final FileHandlerService fileHandlerService;
3937
private final String fileDir = ConfigConstants.getFileDir();
40-
4138
private final ExecutorService executors = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
4239

43-
public ZipReader(FilePreviewCommonService filePreviewCommonService) {
44-
this.filePreviewCommonService = filePreviewCommonService;
40+
public CompressFileReader(FileHandlerService fileHandlerService) {
41+
this.fileHandlerService = fileHandlerService;
4542
}
4643

47-
public String readZipFile(String filePath,String fileKey) {
44+
public String readZipFile(String filePath, String fileKey) {
4845
String archiveSeparator = "/";
4946
Map<String, FileNode> appender = new HashMap<>();
5047
List<String> imgUrls = new LinkedList<>();
5148
String baseUrl = BaseUrlFilter.getBaseUrl();
52-
String archiveFileName = filePreviewCommonService.getFileNameFromPath(filePath);
49+
String archiveFileName = fileHandlerService.getFileNameFromPath(filePath);
5350
try {
54-
ZipFile zipFile = new ZipFile(filePath, filePreviewCommonService.getFileEncodeUTFGBK(filePath));
51+
ZipFile zipFile = new ZipFile(filePath, FileUtils.getFileEncode(filePath));
5552
Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
5653
// 排序
5754
entries = sortZipEntries(entries);
5855
List<Map<String, ZipArchiveEntry>> entriesToBeExtracted = new LinkedList<>();
59-
while (entries.hasMoreElements()){
56+
while (entries.hasMoreElements()) {
6057
ZipArchiveEntry entry = entries.nextElement();
6158
String fullName = entry.getName();
6259
int level = fullName.split(archiveSeparator).length;
@@ -69,18 +66,18 @@ public String readZipFile(String filePath,String fileKey) {
6966
entriesToBeExtracted.add(Collections.singletonMap(childName, entry));
7067
}
7168
String parentName = getLast2FileName(fullName, archiveSeparator, archiveFileName);
72-
parentName = (level-1) + "_" + parentName;
73-
FileType type= filePreviewCommonService.typeFromUrl(childName);
74-
if (type.equals(FileType.picture)){//添加图片文件到图片列表
75-
imgUrls.add(baseUrl+childName);
69+
parentName = (level - 1) + "_" + parentName;
70+
FileType type = fileHandlerService.typeFromUrl(childName);
71+
if (type.equals(FileType.picture)) {//添加图片文件到图片列表
72+
imgUrls.add(baseUrl + childName);
7673
}
7774
FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey);
7875
addNodes(appender, parentName, node);
7976
appender.put(childName, node);
8077
}
8178
// 开启新的线程处理文件解压
8279
executors.submit(new ZipExtractorWorker(entriesToBeExtracted, zipFile, filePath));
83-
filePreviewCommonService.putImgCache(fileKey,imgUrls);
80+
fileHandlerService.putImgCache(fileKey, imgUrls);
8481
return new ObjectMapper().writeValueAsString(appender.get(""));
8582
} catch (IOException e) {
8683
e.printStackTrace();
@@ -90,28 +87,28 @@ public String readZipFile(String filePath,String fileKey) {
9087

9188
private Enumeration<ZipArchiveEntry> sortZipEntries(Enumeration<ZipArchiveEntry> entries) {
9289
List<ZipArchiveEntry> sortedEntries = new LinkedList<>();
93-
while(entries.hasMoreElements()){
90+
while (entries.hasMoreElements()) {
9491
sortedEntries.add(entries.nextElement());
9592
}
9693
sortedEntries.sort(Comparator.comparingInt(o -> o.getName().length()));
9794
return Collections.enumeration(sortedEntries);
9895
}
9996

100-
public String unRar(String filePath,String fileKey){
97+
public String unRar(String filePath, String fileKey) {
10198
Map<String, FileNode> appender = new HashMap<>();
10299
List<String> imgUrls = new ArrayList<>();
103100
String baseUrl = BaseUrlFilter.getBaseUrl();
104101
try {
105-
Archive archive = new Archive(new FileInputStream(new File(filePath)));
102+
Archive archive = new Archive(new FileInputStream(filePath));
106103
List<FileHeader> headers = archive.getFileHeaders();
107104
headers = sortedHeaders(headers);
108-
String archiveFileName = filePreviewCommonService.getFileNameFromPath(filePath);
109-
List<Map<String, FileHeader>> headersToBeExtracted =new ArrayList<>();
105+
String archiveFileName = fileHandlerService.getFileNameFromPath(filePath);
106+
List<Map<String, FileHeader>> headersToBeExtracted = new ArrayList<>();
110107
for (FileHeader header : headers) {
111108
String fullName;
112109
if (header.isUnicode()) {
113110
fullName = header.getFileNameW();
114-
}else {
111+
} else {
115112
fullName = header.getFileNameString();
116113
}
117114
// 展示名
@@ -123,36 +120,36 @@ public String unRar(String filePath,String fileKey){
123120
headersToBeExtracted.add(Collections.singletonMap(childName, header));
124121
}
125122
String parentName = getLast2FileName(fullName, "\\", archiveFileName);
126-
FileType type = filePreviewCommonService.typeFromUrl(childName);
127-
if (type.equals(FileType.picture)){//添加图片文件到图片列表
128-
imgUrls.add(baseUrl+childName);
123+
FileType type = fileHandlerService.typeFromUrl(childName);
124+
if (type.equals(FileType.picture)) {//添加图片文件到图片列表
125+
imgUrls.add(baseUrl + childName);
129126
}
130127
FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey);
131128
addNodes(appender, parentName, node);
132129
appender.put(childName, node);
133130
}
134131
executors.submit(new RarExtractorWorker(headersToBeExtracted, archive, filePath));
135-
filePreviewCommonService.putImgCache(fileKey,imgUrls);
132+
fileHandlerService.putImgCache(fileKey, imgUrls);
136133
return new ObjectMapper().writeValueAsString(appender.get(""));
137134
} catch (RarException | IOException e) {
138135
e.printStackTrace();
139136
}
140137
return null;
141138
}
142139

143-
public String read7zFile(String filePath,String fileKey) {
140+
public String read7zFile(String filePath, String fileKey) {
144141
String archiveSeparator = "/";
145142
Map<String, FileNode> appender = new HashMap<>();
146143
List<String> imgUrls = new ArrayList<>();
147-
String baseUrl= BaseUrlFilter.getBaseUrl();
148-
String archiveFileName = filePreviewCommonService.getFileNameFromPath(filePath);
144+
String baseUrl = BaseUrlFilter.getBaseUrl();
145+
String archiveFileName = fileHandlerService.getFileNameFromPath(filePath);
149146
try {
150147
SevenZFile zipFile = new SevenZFile(new File(filePath));
151148
Iterable<SevenZArchiveEntry> entries = zipFile.getEntries();
152149
// 排序
153150
Enumeration<SevenZArchiveEntry> newEntries = sortSevenZEntries(entries);
154151
List<Map<String, SevenZArchiveEntry>> entriesToBeExtracted = new ArrayList<>();
155-
while (newEntries.hasMoreElements()){
152+
while (newEntries.hasMoreElements()) {
156153
SevenZArchiveEntry entry = newEntries.nextElement();
157154
String fullName = entry.getName();
158155
int level = fullName.split(archiveSeparator).length;
@@ -165,18 +162,18 @@ public String read7zFile(String filePath,String fileKey) {
165162
entriesToBeExtracted.add(Collections.singletonMap(childName, entry));
166163
}
167164
String parentName = getLast2FileName(fullName, archiveSeparator, archiveFileName);
168-
parentName = (level-1) + "_" + parentName;
169-
FileType type= filePreviewCommonService.typeFromUrl(childName);
170-
if (type.equals(FileType.picture)){//添加图片文件到图片列表
171-
imgUrls.add(baseUrl+childName);
165+
parentName = (level - 1) + "_" + parentName;
166+
FileType type = fileHandlerService.typeFromUrl(childName);
167+
if (type.equals(FileType.picture)) {//添加图片文件到图片列表
168+
imgUrls.add(baseUrl + childName);
172169
}
173170
FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey);
174171
addNodes(appender, parentName, node);
175172
appender.put(childName, node);
176173
}
177174
// 开启新的线程处理文件解压
178175
executors.submit(new SevenZExtractorWorker(entriesToBeExtracted, filePath));
179-
filePreviewCommonService.putImgCache(fileKey,imgUrls);
176+
fileHandlerService.putImgCache(fileKey, imgUrls);
180177
return new ObjectMapper().writeValueAsString(appender.get(""));
181178
} catch (IOException e) {
182179
e.printStackTrace();
@@ -210,7 +207,7 @@ private List<FileHeader> sortedHeaders(List<FileHeader> headers) {
210207
List<FileHeader> sortedHeaders = new ArrayList<>();
211208
Map<Integer, FileHeader> mapHeaders = new TreeMap<>();
212209
headers.forEach(header -> mapHeaders.put(new Integer(0).equals(header.getFileNameW().length()) ? header.getFileNameString().length() : header.getFileNameW().length(), header));
213-
for (Map.Entry<Integer, FileHeader> entry : mapHeaders.entrySet()){
210+
for (Map.Entry<Integer, FileHeader> entry : mapHeaders.entrySet()) {
214211
for (FileHeader header : headers) {
215212
if (entry.getKey().equals(new Integer(0).equals(header.getFileNameW().length()) ? header.getFileNameString().length() : header.getFileNameW().length())) {
216213
sortedHeaders.add(header);
@@ -222,7 +219,7 @@ private List<FileHeader> sortedHeaders(List<FileHeader> headers) {
222219

223220
private static String getLast2FileName(String fullName, String seperator, String rootName) {
224221
if (fullName.endsWith(seperator)) {
225-
fullName = fullName.substring(0, fullName.length()-1);
222+
fullName = fullName.substring(0, fullName.length() - 1);
226223
}
227224
// 1.获取剩余部分
228225
int endIndex = fullName.lastIndexOf(seperator);
@@ -237,7 +234,7 @@ private static String getLast2FileName(String fullName, String seperator, String
237234

238235
private static String getLastFileName(String fullName, String seperator) {
239236
if (fullName.endsWith(seperator)) {
240-
fullName = fullName.substring(0, fullName.length()-1);
237+
fullName = fullName.substring(0, fullName.length() - 1);
241238
}
242239
String newName = fullName;
243240
if (fullName.contains(seperator)) {
@@ -248,10 +245,11 @@ private static String getLastFileName(String fullName, String seperator) {
248245

249246
public static Comparator<FileNode> sortComparator = new Comparator<FileNode>() {
250247
final Collator cmp = Collator.getInstance(Locale.US);
248+
251249
@Override
252250
public int compare(FileNode o1, FileNode o2) {
253251
// 判断两个对比对象是否是开头包含数字,如果包含数字则获取数字并按数字真正大小进行排序
254-
BigDecimal num1,num2;
252+
BigDecimal num1, num2;
255253
if (null != (num1 = isStartNumber(o1))
256254
&& null != (num2 = isStartNumber(o2))) {
257255
return num1.subtract(num2).intValue();
@@ -287,14 +285,16 @@ public FileNode(String originName, String fileName, String parentFileName, List<
287285
this.childList = childList;
288286
this.directory = directory;
289287
}
290-
public FileNode(String originName, String fileName, String parentFileName, List<FileNode> childList, boolean directory,String fileKey) {
288+
289+
public FileNode(String originName, String fileName, String parentFileName, List<FileNode> childList, boolean directory, String fileKey) {
291290
this.originName = originName;
292291
this.fileName = fileName;
293292
this.parentFileName = parentFileName;
294293
this.childList = childList;
295294
this.directory = directory;
296-
this.fileKey=fileKey;
295+
this.fileKey = fileKey;
297296
}
297+
298298
public String getFileKey() {
299299
return fileKey;
300300
}
@@ -382,17 +382,15 @@ public void run() {
382382
} catch (IOException e) {
383383
e.printStackTrace();
384384
}
385-
if (new File(filePath).exists()) {
386-
new File(filePath).delete();
387-
}
385+
FileUtils.deleteFileByPath(filePath);
388386
}
389387

390388
private void extractZipFile(String childName, InputStream zipFile) {
391389
String outPath = fileDir + childName;
392-
try (OutputStream ot = new FileOutputStream(outPath)){
390+
try (OutputStream ot = new FileOutputStream(outPath)) {
393391
byte[] inByte = new byte[1024];
394392
int len;
395-
while ((-1 != (len = zipFile.read(inByte)))){
393+
while ((-1 != (len = zipFile.read(inByte)))) {
396394
ot.write(inByte, 0, len);
397395
}
398396
} catch (IOException e) {
@@ -441,10 +439,7 @@ public void run() {
441439
} catch (IOException e) {
442440
e.printStackTrace();
443441
}
444-
445-
if (new File(filePath).exists()) {
446-
new File(filePath).delete();
447-
}
442+
FileUtils.deleteFileByPath(filePath);
448443
}
449444
}
450445

@@ -473,14 +468,12 @@ public void run() {
473468
} catch (IOException e) {
474469
e.printStackTrace();
475470
}
476-
if (new File(filePath).exists()) {
477-
new File(filePath).delete();
478-
}
471+
FileUtils.deleteFileByPath(filePath);
479472
}
480473

481474
private void extractRarFile(String childName, FileHeader header, Archive archive) {
482475
String outPath = fileDir + childName;
483-
try(OutputStream ot = new FileOutputStream(outPath)) {
476+
try (OutputStream ot = new FileOutputStream(outPath)) {
484477
archive.extractFile(header, ot);
485478
} catch (IOException | RarException e) {
486479
e.printStackTrace();

server/src/main/java/cn/keking/service/FileConvertQueueTask.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ public class FileConvertQueueTask {
2121
private final Logger logger = LoggerFactory.getLogger(getClass());
2222
private final FilePreviewFactory previewFactory;
2323
private final CacheService cacheService;
24-
private final FilePreviewCommonService filePreviewCommonService;
24+
private final FileHandlerService fileHandlerService;
2525

26-
public FileConvertQueueTask(FilePreviewFactory previewFactory, CacheService cacheService, FilePreviewCommonService filePreviewCommonService) {
26+
public FileConvertQueueTask(FilePreviewFactory previewFactory, CacheService cacheService, FileHandlerService fileHandlerService) {
2727
this.previewFactory = previewFactory;
2828
this.cacheService = cacheService;
29-
this.filePreviewCommonService = filePreviewCommonService;
29+
this.fileHandlerService = fileHandlerService;
3030
}
3131

3232
@PostConstruct
3333
public void startTask(){
3434
ExecutorService executorService = Executors.newFixedThreadPool(1);
35-
executorService.submit(new ConvertTask(previewFactory, cacheService, filePreviewCommonService));
35+
executorService.submit(new ConvertTask(previewFactory, cacheService, fileHandlerService));
3636
logger.info("队列处理文件转换任务启动完成 ");
3737
}
3838

@@ -41,14 +41,14 @@ static class ConvertTask implements Runnable {
4141
private final Logger logger = LoggerFactory.getLogger(ConvertTask.class);
4242
private final FilePreviewFactory previewFactory;
4343
private final CacheService cacheService;
44-
private final FilePreviewCommonService filePreviewCommonService;
44+
private final FileHandlerService fileHandlerService;
4545

4646
public ConvertTask(FilePreviewFactory previewFactory,
4747
CacheService cacheService,
48-
FilePreviewCommonService filePreviewCommonService) {
48+
FileHandlerService fileHandlerService) {
4949
this.previewFactory = previewFactory;
5050
this.cacheService = cacheService;
51-
this.filePreviewCommonService = filePreviewCommonService;
51+
this.fileHandlerService = fileHandlerService;
5252
}
5353

5454
@Override
@@ -58,7 +58,7 @@ public void run() {
5858
try {
5959
url = cacheService.takeQueueTask();
6060
if(url != null){
61-
FileAttribute fileAttribute = filePreviewCommonService.getFileAttribute(url,null);
61+
FileAttribute fileAttribute = fileHandlerService.getFileAttribute(url,null);
6262
FileType fileType = fileAttribute.getType();
6363
logger.info("正在处理预览转换任务,url:{},预览类型:{}", url, fileType);
6464
if(fileType.equals(FileType.compress) || fileType.equals(FileType.office) || fileType.equals(FileType.cad)) {

0 commit comments

Comments
 (0)