Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Y
YGOMobile
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fallenstardust
YGOMobile
Commits
03c339ed
Commit
03c339ed
authored
Nov 29, 2021
by
fallenstardust
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add UnzipUtils
parent
a878e354
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
159 additions
and
0 deletions
+159
-0
mobile/src/main/java/cn/garymb/ygomobile/utils/UnzipUtils.java
...e/src/main/java/cn/garymb/ygomobile/utils/UnzipUtils.java
+159
-0
No files found.
mobile/src/main/java/cn/garymb/ygomobile/utils/UnzipUtils.java
0 → 100644
View file @
03c339ed
package
cn.garymb.ygomobile.utils
;
import
com.file.zip.ZipEntry
;
import
com.file.zip.ZipFile
;
import
com.file.zip.ZipOutputStream
;
import
com.yuyh.library.imgsel.utils.LogUtils
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.InputStream
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.zip.ZipInputStream
;
public
class
UnzipUtils
{
public
UnzipUtils
(){
}
/**
* DeCompress the ZIP to the path
* @param zipFileString name of ZIP
* @param outPathString path to be unZIP
* @throws Exception
*/
public
static
void
unZipFolder
(
String
zipFileString
,
String
outPathString
)
throws
Exception
{
ZipInputStream
inZip
=
new
ZipInputStream
(
new
FileInputStream
(
zipFileString
));
ZipEntry
zipEntry
;
String
szName
=
""
;
while
((
zipEntry
=
(
ZipEntry
)
inZip
.
getNextEntry
())
!=
null
)
{
szName
=
zipEntry
.
getName
();
if
(
zipEntry
.
isDirectory
())
{
// get the folder name of the widget
szName
=
szName
.
substring
(
0
,
szName
.
length
()
-
1
);
File
folder
=
new
File
(
outPathString
+
File
.
separator
+
szName
);
folder
.
mkdirs
();
}
else
{
File
file
=
new
File
(
outPathString
+
File
.
separator
+
szName
);
file
.
createNewFile
();
// get the output stream of the file
FileOutputStream
out
=
new
FileOutputStream
(
file
);
int
len
;
byte
[]
buffer
=
new
byte
[
1024
];
// read (len) bytes into buffer
while
((
len
=
inZip
.
read
(
buffer
))
!=
-
1
)
{
// write (len) byte from buffer at the position 0
out
.
write
(
buffer
,
0
,
len
);
out
.
flush
();
}
out
.
close
();
}
}
LogUtils
.
e
(
"解压成功!"
);
inZip
.
close
();
}
/**
* Compress file and folder
* @param srcFileString file or folder to be Compress
* @param zipFileString the path name of result ZIP
* @throws Exception
*/
public
static
void
ZipFolder
(
String
srcFileString
,
String
zipFileString
)
throws
Exception
{
//create ZIP
ZipOutputStream
outZip
=
new
ZipOutputStream
(
new
FileOutputStream
(
zipFileString
));
//create the file
File
file
=
new
File
(
srcFileString
);
//compress
ZipFiles
(
file
.
getParent
()+
File
.
separator
,
file
.
getName
(),
outZip
);
//finish and close
outZip
.
finish
();
outZip
.
close
();
}
/**
* compress files
* @param folderString
* @param fileString
* @param zipOutputSteam
* @throws Exception
*/
private
static
void
ZipFiles
(
String
folderString
,
String
fileString
,
ZipOutputStream
zipOutputSteam
)
throws
Exception
{
if
(
zipOutputSteam
==
null
)
return
;
File
file
=
new
File
(
folderString
+
fileString
);
if
(
file
.
isFile
())
{
ZipEntry
zipEntry
=
new
ZipEntry
(
fileString
);
FileInputStream
inputStream
=
new
FileInputStream
(
file
);
zipOutputSteam
.
putNextEntry
(
zipEntry
);
int
len
;
byte
[]
buffer
=
new
byte
[
4096
];
while
((
len
=
inputStream
.
read
(
buffer
))
!=
-
1
)
{
zipOutputSteam
.
write
(
buffer
,
0
,
len
);
}
zipOutputSteam
.
closeEntry
();
}
else
{
//folder
String
fileList
[]
=
file
.
list
();
//no child file and compress
if
(
fileList
.
length
<=
0
)
{
ZipEntry
zipEntry
=
new
ZipEntry
(
fileString
+
File
.
separator
);
zipOutputSteam
.
putNextEntry
(
zipEntry
);
zipOutputSteam
.
closeEntry
();
}
//child files and recursion
for
(
int
i
=
0
;
i
<
fileList
.
length
;
i
++)
{
ZipFiles
(
folderString
,
fileString
+
java
.
io
.
File
.
separator
+
fileList
[
i
],
zipOutputSteam
);
}
//end of for
}
}
/**
* return the InputStream of file in the ZIP
* @param zipFileString name of ZIP
* @param fileString name of file in the ZIP
* @return InputStream
* @throws Exception
*/
public
static
InputStream
UpZip
(
String
zipFileString
,
String
fileString
)
throws
Exception
{
ZipFile
zipFile
=
new
ZipFile
(
zipFileString
);
ZipEntry
zipEntry
=
zipFile
.
getEntry
(
fileString
);
return
zipFile
.
getInputStream
(
zipEntry
);
}
/**
* return files list(file and folder) in the ZIP
* @param zipFileString ZIP name
* @param bContainFolder contain folder or not
* @param bContainFile contain file or not
* @return
* @throws Exception
*/
public
static
List
<
File
>
GetFileList
(
String
zipFileString
,
boolean
bContainFolder
,
boolean
bContainFile
)
throws
Exception
{
List
<
File
>
fileList
=
new
ArrayList
<
File
>();
ZipInputStream
inZip
=
new
ZipInputStream
(
new
FileInputStream
(
zipFileString
));
ZipEntry
zipEntry
;
String
szName
=
""
;
while
((
zipEntry
=
(
ZipEntry
)
inZip
.
getNextEntry
())
!=
null
)
{
szName
=
zipEntry
.
getName
();
if
(
zipEntry
.
isDirectory
())
{
// get the folder name of the widget
szName
=
szName
.
substring
(
0
,
szName
.
length
()
-
1
);
File
folder
=
new
File
(
szName
);
if
(
bContainFolder
)
{
fileList
.
add
(
folder
);
}
}
else
{
File
file
=
new
File
(
szName
);
if
(
bContainFile
)
{
fileList
.
add
(
file
);
}
}
}
inZip
.
close
();
return
fileList
;
}
}
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