1
- package cn .keking .utils ;
1
+ package cn .keking .service ;
2
2
3
3
import cn .keking .config .ConfigConstants ;
4
4
import cn .keking .model .FileType ;
5
- import cn .keking .service . FilePreviewCommonService ;
5
+ import cn .keking .utils . FileUtils ;
6
6
import cn .keking .web .filter .BaseUrlFilter ;
7
7
import com .fasterxml .jackson .core .JsonProcessingException ;
8
8
import com .fasterxml .jackson .databind .ObjectMapper ;
26
26
import java .util .regex .Pattern ;
27
27
28
28
/**
29
- *
30
29
* @author yudian-it
31
- * @date 2017/11/27
30
+ * create 2017/11/27
32
31
*/
33
32
@ Component
34
- public class ZipReader {
35
- static Pattern pattern = Pattern .compile ("^\\ d+" );
36
-
37
- private final FilePreviewCommonService filePreviewCommonService ;
33
+ public class CompressFileReader {
38
34
35
+ private static final Pattern pattern = Pattern .compile ("^\\ d+" );
36
+ private final FileHandlerService fileHandlerService ;
39
37
private final String fileDir = ConfigConstants .getFileDir ();
40
-
41
38
private final ExecutorService executors = Executors .newFixedThreadPool (Runtime .getRuntime ().availableProcessors ());
42
39
43
- public ZipReader ( FilePreviewCommonService filePreviewCommonService ) {
44
- this .filePreviewCommonService = filePreviewCommonService ;
40
+ public CompressFileReader ( FileHandlerService fileHandlerService ) {
41
+ this .fileHandlerService = fileHandlerService ;
45
42
}
46
43
47
- public String readZipFile (String filePath ,String fileKey ) {
44
+ public String readZipFile (String filePath , String fileKey ) {
48
45
String archiveSeparator = "/" ;
49
46
Map <String , FileNode > appender = new HashMap <>();
50
47
List <String > imgUrls = new LinkedList <>();
51
48
String baseUrl = BaseUrlFilter .getBaseUrl ();
52
- String archiveFileName = filePreviewCommonService .getFileNameFromPath (filePath );
49
+ String archiveFileName = fileHandlerService .getFileNameFromPath (filePath );
53
50
try {
54
- ZipFile zipFile = new ZipFile (filePath , filePreviewCommonService . getFileEncodeUTFGBK (filePath ));
51
+ ZipFile zipFile = new ZipFile (filePath , FileUtils . getFileEncode (filePath ));
55
52
Enumeration <ZipArchiveEntry > entries = zipFile .getEntries ();
56
53
// 排序
57
54
entries = sortZipEntries (entries );
58
55
List <Map <String , ZipArchiveEntry >> entriesToBeExtracted = new LinkedList <>();
59
- while (entries .hasMoreElements ()){
56
+ while (entries .hasMoreElements ()) {
60
57
ZipArchiveEntry entry = entries .nextElement ();
61
58
String fullName = entry .getName ();
62
59
int level = fullName .split (archiveSeparator ).length ;
@@ -69,18 +66,18 @@ public String readZipFile(String filePath,String fileKey) {
69
66
entriesToBeExtracted .add (Collections .singletonMap (childName , entry ));
70
67
}
71
68
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 );
76
73
}
77
74
FileNode node = new FileNode (originName , childName , parentName , new ArrayList <>(), directory , fileKey );
78
75
addNodes (appender , parentName , node );
79
76
appender .put (childName , node );
80
77
}
81
78
// 开启新的线程处理文件解压
82
79
executors .submit (new ZipExtractorWorker (entriesToBeExtracted , zipFile , filePath ));
83
- filePreviewCommonService .putImgCache (fileKey ,imgUrls );
80
+ fileHandlerService .putImgCache (fileKey , imgUrls );
84
81
return new ObjectMapper ().writeValueAsString (appender .get ("" ));
85
82
} catch (IOException e ) {
86
83
e .printStackTrace ();
@@ -90,28 +87,28 @@ public String readZipFile(String filePath,String fileKey) {
90
87
91
88
private Enumeration <ZipArchiveEntry > sortZipEntries (Enumeration <ZipArchiveEntry > entries ) {
92
89
List <ZipArchiveEntry > sortedEntries = new LinkedList <>();
93
- while (entries .hasMoreElements ()){
90
+ while (entries .hasMoreElements ()) {
94
91
sortedEntries .add (entries .nextElement ());
95
92
}
96
93
sortedEntries .sort (Comparator .comparingInt (o -> o .getName ().length ()));
97
94
return Collections .enumeration (sortedEntries );
98
95
}
99
96
100
- public String unRar (String filePath ,String fileKey ){
97
+ public String unRar (String filePath , String fileKey ) {
101
98
Map <String , FileNode > appender = new HashMap <>();
102
99
List <String > imgUrls = new ArrayList <>();
103
100
String baseUrl = BaseUrlFilter .getBaseUrl ();
104
101
try {
105
- Archive archive = new Archive (new FileInputStream (new File ( filePath ) ));
102
+ Archive archive = new Archive (new FileInputStream (filePath ));
106
103
List <FileHeader > headers = archive .getFileHeaders ();
107
104
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 <>();
110
107
for (FileHeader header : headers ) {
111
108
String fullName ;
112
109
if (header .isUnicode ()) {
113
110
fullName = header .getFileNameW ();
114
- }else {
111
+ } else {
115
112
fullName = header .getFileNameString ();
116
113
}
117
114
// 展示名
@@ -123,36 +120,36 @@ public String unRar(String filePath,String fileKey){
123
120
headersToBeExtracted .add (Collections .singletonMap (childName , header ));
124
121
}
125
122
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 );
129
126
}
130
127
FileNode node = new FileNode (originName , childName , parentName , new ArrayList <>(), directory , fileKey );
131
128
addNodes (appender , parentName , node );
132
129
appender .put (childName , node );
133
130
}
134
131
executors .submit (new RarExtractorWorker (headersToBeExtracted , archive , filePath ));
135
- filePreviewCommonService .putImgCache (fileKey ,imgUrls );
132
+ fileHandlerService .putImgCache (fileKey , imgUrls );
136
133
return new ObjectMapper ().writeValueAsString (appender .get ("" ));
137
134
} catch (RarException | IOException e ) {
138
135
e .printStackTrace ();
139
136
}
140
137
return null ;
141
138
}
142
139
143
- public String read7zFile (String filePath ,String fileKey ) {
140
+ public String read7zFile (String filePath , String fileKey ) {
144
141
String archiveSeparator = "/" ;
145
142
Map <String , FileNode > appender = new HashMap <>();
146
143
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 );
149
146
try {
150
147
SevenZFile zipFile = new SevenZFile (new File (filePath ));
151
148
Iterable <SevenZArchiveEntry > entries = zipFile .getEntries ();
152
149
// 排序
153
150
Enumeration <SevenZArchiveEntry > newEntries = sortSevenZEntries (entries );
154
151
List <Map <String , SevenZArchiveEntry >> entriesToBeExtracted = new ArrayList <>();
155
- while (newEntries .hasMoreElements ()){
152
+ while (newEntries .hasMoreElements ()) {
156
153
SevenZArchiveEntry entry = newEntries .nextElement ();
157
154
String fullName = entry .getName ();
158
155
int level = fullName .split (archiveSeparator ).length ;
@@ -165,18 +162,18 @@ public String read7zFile(String filePath,String fileKey) {
165
162
entriesToBeExtracted .add (Collections .singletonMap (childName , entry ));
166
163
}
167
164
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 );
172
169
}
173
170
FileNode node = new FileNode (originName , childName , parentName , new ArrayList <>(), directory , fileKey );
174
171
addNodes (appender , parentName , node );
175
172
appender .put (childName , node );
176
173
}
177
174
// 开启新的线程处理文件解压
178
175
executors .submit (new SevenZExtractorWorker (entriesToBeExtracted , filePath ));
179
- filePreviewCommonService .putImgCache (fileKey ,imgUrls );
176
+ fileHandlerService .putImgCache (fileKey , imgUrls );
180
177
return new ObjectMapper ().writeValueAsString (appender .get ("" ));
181
178
} catch (IOException e ) {
182
179
e .printStackTrace ();
@@ -210,7 +207,7 @@ private List<FileHeader> sortedHeaders(List<FileHeader> headers) {
210
207
List <FileHeader > sortedHeaders = new ArrayList <>();
211
208
Map <Integer , FileHeader > mapHeaders = new TreeMap <>();
212
209
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 ()) {
214
211
for (FileHeader header : headers ) {
215
212
if (entry .getKey ().equals (new Integer (0 ).equals (header .getFileNameW ().length ()) ? header .getFileNameString ().length () : header .getFileNameW ().length ())) {
216
213
sortedHeaders .add (header );
@@ -222,7 +219,7 @@ private List<FileHeader> sortedHeaders(List<FileHeader> headers) {
222
219
223
220
private static String getLast2FileName (String fullName , String seperator , String rootName ) {
224
221
if (fullName .endsWith (seperator )) {
225
- fullName = fullName .substring (0 , fullName .length ()- 1 );
222
+ fullName = fullName .substring (0 , fullName .length () - 1 );
226
223
}
227
224
// 1.获取剩余部分
228
225
int endIndex = fullName .lastIndexOf (seperator );
@@ -237,7 +234,7 @@ private static String getLast2FileName(String fullName, String seperator, String
237
234
238
235
private static String getLastFileName (String fullName , String seperator ) {
239
236
if (fullName .endsWith (seperator )) {
240
- fullName = fullName .substring (0 , fullName .length ()- 1 );
237
+ fullName = fullName .substring (0 , fullName .length () - 1 );
241
238
}
242
239
String newName = fullName ;
243
240
if (fullName .contains (seperator )) {
@@ -248,10 +245,11 @@ private static String getLastFileName(String fullName, String seperator) {
248
245
249
246
public static Comparator <FileNode > sortComparator = new Comparator <FileNode >() {
250
247
final Collator cmp = Collator .getInstance (Locale .US );
248
+
251
249
@ Override
252
250
public int compare (FileNode o1 , FileNode o2 ) {
253
251
// 判断两个对比对象是否是开头包含数字,如果包含数字则获取数字并按数字真正大小进行排序
254
- BigDecimal num1 ,num2 ;
252
+ BigDecimal num1 , num2 ;
255
253
if (null != (num1 = isStartNumber (o1 ))
256
254
&& null != (num2 = isStartNumber (o2 ))) {
257
255
return num1 .subtract (num2 ).intValue ();
@@ -287,14 +285,16 @@ public FileNode(String originName, String fileName, String parentFileName, List<
287
285
this .childList = childList ;
288
286
this .directory = directory ;
289
287
}
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 ) {
291
290
this .originName = originName ;
292
291
this .fileName = fileName ;
293
292
this .parentFileName = parentFileName ;
294
293
this .childList = childList ;
295
294
this .directory = directory ;
296
- this .fileKey = fileKey ;
295
+ this .fileKey = fileKey ;
297
296
}
297
+
298
298
public String getFileKey () {
299
299
return fileKey ;
300
300
}
@@ -382,17 +382,15 @@ public void run() {
382
382
} catch (IOException e ) {
383
383
e .printStackTrace ();
384
384
}
385
- if (new File (filePath ).exists ()) {
386
- new File (filePath ).delete ();
387
- }
385
+ FileUtils .deleteFileByPath (filePath );
388
386
}
389
387
390
388
private void extractZipFile (String childName , InputStream zipFile ) {
391
389
String outPath = fileDir + childName ;
392
- try (OutputStream ot = new FileOutputStream (outPath )){
390
+ try (OutputStream ot = new FileOutputStream (outPath )) {
393
391
byte [] inByte = new byte [1024 ];
394
392
int len ;
395
- while ((-1 != (len = zipFile .read (inByte )))){
393
+ while ((-1 != (len = zipFile .read (inByte )))) {
396
394
ot .write (inByte , 0 , len );
397
395
}
398
396
} catch (IOException e ) {
@@ -441,10 +439,7 @@ public void run() {
441
439
} catch (IOException e ) {
442
440
e .printStackTrace ();
443
441
}
444
-
445
- if (new File (filePath ).exists ()) {
446
- new File (filePath ).delete ();
447
- }
442
+ FileUtils .deleteFileByPath (filePath );
448
443
}
449
444
}
450
445
@@ -473,14 +468,12 @@ public void run() {
473
468
} catch (IOException e ) {
474
469
e .printStackTrace ();
475
470
}
476
- if (new File (filePath ).exists ()) {
477
- new File (filePath ).delete ();
478
- }
471
+ FileUtils .deleteFileByPath (filePath );
479
472
}
480
473
481
474
private void extractRarFile (String childName , FileHeader header , Archive archive ) {
482
475
String outPath = fileDir + childName ;
483
- try (OutputStream ot = new FileOutputStream (outPath )) {
476
+ try (OutputStream ot = new FileOutputStream (outPath )) {
484
477
archive .extractFile (header , ot );
485
478
} catch (IOException | RarException e ) {
486
479
e .printStackTrace ();
0 commit comments