Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
slm-fileview
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
jenkins
slm-fileview
Commits
3f40b60c
Commit
3f40b60c
authored
May 16, 2019
by
陈精华
Committed by
kl
May 23, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
支持7z文件预览
parent
f2440544
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
129 additions
and
0 deletions
+129
-0
pom.xml
jodconverter-web/pom.xml
+6
-0
CompressFilePreviewImpl.java
.../java/cn/keking/service/impl/CompressFilePreviewImpl.java
+2
-0
ZipReader.java
...onverter-web/src/main/java/cn/keking/utils/ZipReader.java
+121
-0
No files found.
jodconverter-web/pom.xml
View file @
3f40b60c
...
...
@@ -114,6 +114,12 @@
<artifactId>
junrar
</artifactId>
<version>
4.0.0
</version>
</dependency>
<!-- 解压(7z)-->
<dependency>
<groupId>
org.tukaani
</groupId>
<artifactId>
xz
</artifactId>
<version>
1.8
</version>
</dependency>
<dependency>
<groupId>
net.sourceforge.jchardet
</groupId>
<artifactId>
jchardet
</artifactId>
...
...
jodconverter-web/src/main/java/cn/keking/service/impl/CompressFilePreviewImpl.java
View file @
3f40b60c
...
...
@@ -47,6 +47,8 @@ public class CompressFilePreviewImpl implements FilePreview{
fileTree
=
zipReader
.
readZipFile
(
filePath
,
fileName
);
}
else
if
(
"rar"
.
equalsIgnoreCase
(
suffix
))
{
fileTree
=
zipReader
.
unRar
(
filePath
,
fileName
);
}
else
if
(
"7z"
.
equalsIgnoreCase
(
suffix
))
{
fileTree
=
zipReader
.
read7zFile
(
filePath
,
fileName
);
}
if
(
fileTree
!=
null
&&
!
"null"
.
equals
(
fileTree
))
{
fileUtils
.
addConvertedFile
(
fileName
,
fileTree
);
...
...
jodconverter-web/src/main/java/cn/keking/utils/ZipReader.java
View file @
3f40b60c
...
...
@@ -9,6 +9,8 @@ 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
org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry
;
import
org.apache.commons.compress.archivers.sevenz.SevenZFile
;
import
org.apache.commons.compress.archivers.zip.ZipArchiveEntry
;
import
org.apache.commons.compress.archivers.zip.ZipFile
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -160,6 +162,71 @@ public class ZipReader {
return
null
;
}
/**
* 解压7z文件
* @param filePath
* @param fileKey
* @return
*/
public
String
read7zFile
(
String
filePath
,
String
fileKey
)
{
String
archiveSeparator
=
"/"
;
Map
<
String
,
FileNode
>
appender
=
Maps
.
newHashMap
();
List
imgUrls
=
Lists
.
newArrayList
();
String
baseUrl
=
(
String
)
RequestContextHolder
.
currentRequestAttributes
().
getAttribute
(
"baseUrl"
,
0
);
String
archiveFileName
=
fileUtils
.
getFileNameFromPath
(
filePath
);
try
{
SevenZFile
zipFile
=
new
SevenZFile
(
new
File
(
filePath
));
Iterable
<
SevenZArchiveEntry
>
entries
=
zipFile
.
getEntries
();
// 排序
Enumeration
<
SevenZArchiveEntry
>
newEntries
=
sortSevenZEntries
(
entries
);
List
<
Map
<
String
,
SevenZArchiveEntry
>>
entriesToBeExtracted
=
Lists
.
newArrayList
();
while
(
newEntries
.
hasMoreElements
()){
SevenZArchiveEntry
entry
=
newEntries
.
nextElement
();
String
fullName
=
entry
.
getName
();
int
level
=
fullName
.
split
(
archiveSeparator
).
length
;
// 展示名
String
originName
=
getLastFileName
(
fullName
,
archiveSeparator
);
String
childName
=
level
+
"_"
+
originName
;
boolean
directory
=
entry
.
isDirectory
();
if
(!
directory
)
{
childName
=
archiveFileName
+
"_"
+
originName
;
entriesToBeExtracted
.
add
(
Collections
.
singletonMap
(
childName
,
entry
));
}
String
parentName
=
getLast2FileName
(
fullName
,
archiveSeparator
,
archiveFileName
);
parentName
=
(
level
-
1
)
+
"_"
+
parentName
;
FileType
type
=
fileUtils
.
typeFromUrl
(
childName
);
if
(
type
.
equals
(
FileType
.
picture
)){
//添加图片文件到图片列表
imgUrls
.
add
(
baseUrl
+
childName
);
}
FileNode
node
=
new
FileNode
(
originName
,
childName
,
parentName
,
new
ArrayList
<>(),
directory
,
fileKey
);
addNodes
(
appender
,
parentName
,
node
);
appender
.
put
(
childName
,
node
);
}
// 开启新的线程处理文件解压
executors
.
submit
(
new
SevenZExtractorWorker
(
entriesToBeExtracted
,
filePath
));
fileUtils
.
setRedisImgUrls
(
fileKey
,
imgUrls
);
return
new
ObjectMapper
().
writeValueAsString
(
appender
.
get
(
""
));
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
return
null
;
}
}
/**
* 排序7ZEntries(对原来列表倒序)
* @param entries
*/
private
Enumeration
<
SevenZArchiveEntry
>
sortSevenZEntries
(
Iterable
<
SevenZArchiveEntry
>
entries
)
{
List
<
SevenZArchiveEntry
>
sortedEntries
=
Lists
.
newArrayList
();
Iterator
<
SevenZArchiveEntry
>
iterator
=
entries
.
iterator
();
while
(
iterator
.
hasNext
()){
sortedEntries
.
add
(
iterator
.
next
());
}
// Collections.sort(sortedEntries, Comparator.comparingInt(o -> o.getName().length()));
return
Collections
.
enumeration
(
sortedEntries
);
}
private
void
addNodes
(
Map
<
String
,
FileNode
>
appender
,
String
parentName
,
FileNode
node
)
{
if
(
appender
.
containsKey
(
parentName
))
{
appender
.
get
(
parentName
).
getChildList
().
add
(
node
);
...
...
@@ -402,6 +469,60 @@ public class ZipReader {
}
}
/**
* 7z文件抽取线程
*/
class
SevenZExtractorWorker
implements
Runnable
{
private
List
<
Map
<
String
,
SevenZArchiveEntry
>>
entriesToBeExtracted
;
private
String
filePath
;
public
SevenZExtractorWorker
(
List
<
Map
<
String
,
SevenZArchiveEntry
>>
entriesToBeExtracted
,
String
filePath
)
{
this
.
entriesToBeExtracted
=
entriesToBeExtracted
;
this
.
filePath
=
filePath
;
}
@Override
public
void
run
()
{
System
.
out
.
println
(
"解析压缩文件开始《《《《《《《《《《《《《《《《《《《《《《《"
);
try
{
SevenZFile
sevenZFile
=
new
SevenZFile
(
new
File
(
filePath
));
SevenZArchiveEntry
entry
=
sevenZFile
.
getNextEntry
();
while
(
entry
!=
null
)
{
if
(
entry
.
isDirectory
())
{
entry
=
sevenZFile
.
getNextEntry
();
continue
;
}
String
childName
=
"default_file"
;
SevenZArchiveEntry
entry1
=
null
;
for
(
Map
<
String
,
SevenZArchiveEntry
>
entryMap
:
entriesToBeExtracted
)
{
childName
=
entryMap
.
keySet
().
iterator
().
next
();
entry1
=
entryMap
.
values
().
iterator
().
next
();
if
(
entry
.
getName
().
equals
(
entry1
.
getName
()))
{
break
;
}
}
FileOutputStream
out
=
new
FileOutputStream
(
fileDir
+
childName
);
byte
[]
content
=
new
byte
[(
int
)
entry
.
getSize
()];
sevenZFile
.
read
(
content
,
0
,
content
.
length
);
out
.
write
(
content
);
out
.
close
();
entry
=
sevenZFile
.
getNextEntry
();
}
sevenZFile
.
close
();
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
if
(
new
File
(
filePath
).
exists
())
{
new
File
(
filePath
).
delete
();
}
System
.
out
.
println
(
"解析压缩文件结束《《《《《《《《《《《《《《《《《《《《《《《"
);
}
}
/**
* Rar文件抽取
*/
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment