Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
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
YZG
yzg-util
Commits
f89af689
Commit
f89af689
authored
Apr 22, 2019
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
压缩处理
parent
dd6816b1
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
477 additions
and
35 deletions
+477
-35
HttpHelper.java
...src/main/java/com/yanzuoguang/util/helper/HttpHelper.java
+49
-4
ZipHelper.java
.../src/main/java/com/yanzuoguang/util/helper/ZipHelper.java
+319
-0
ProcessData.java
...rc/main/java/com/yanzuoguang/util/thread/ProcessData.java
+91
-0
RunProcess.java
...src/main/java/com/yanzuoguang/util/thread/RunProcess.java
+14
-0
HttpFileHelper.java
...ain/java/com/yanzuoguang/cloud/helper/HttpFileHelper.java
+4
-31
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/HttpHelper.java
View file @
f89af689
...
...
@@ -2,12 +2,14 @@ package com.yanzuoguang.util.helper;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.log.Log
;
import
com.yanzuoguang.util.thread.ProcessData
;
import
com.yanzuoguang.util.thread.RunProcess
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
java.io.PrintWriter
;
import
java.io.*
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.net.URLConnection
;
/**
* HTTP请求工具类
...
...
@@ -112,4 +114,47 @@ public class HttpHelper {
}
return
result
;
}
/**
* 下载文件
*
* @param serverFileName 下载的服务器文件路径
* @param localFileName 保存的文件路径
* @throws IOException
*/
public
static
void
downToLocal
(
String
serverFileName
,
String
localFileName
)
throws
IOException
{
downToLocal
(
serverFileName
,
localFileName
,
null
);
}
/**
* 下载文件
*
* @param serverFileName 下载的服务器文件路径
* @param localFileName 保存的文件路径
* @param runProcess 进度处理
* @throws IOException
*/
public
static
void
downToLocal
(
String
serverFileName
,
String
localFileName
,
RunProcess
runProcess
)
throws
IOException
{
int
byteRead
;
URL
url
=
new
URL
(
serverFileName
);
URLConnection
conn
=
url
.
openConnection
();
InputStream
inStream
=
conn
.
getInputStream
();
try
{
ProcessData
data
=
new
ProcessData
(
serverFileName
,
conn
.
getContentLengthLong
());
FileOutputStream
outputStream
=
new
FileOutputStream
(
localFileName
);
try
{
byte
[]
buffer
=
new
byte
[
1204
];
while
((
byteRead
=
inStream
.
read
(
buffer
))
!=
-
1
)
{
data
.
processAdd
(
runProcess
,
byteRead
);
outputStream
.
write
(
buffer
,
0
,
byteRead
);
}
}
finally
{
outputStream
.
close
();
}
}
finally
{
inStream
.
close
();
}
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/ZipHelper.java
0 → 100644
View file @
f89af689
package
com
.
yanzuoguang
.
util
.
helper
;
import
com.yanzuoguang.util.exception.CodeException
;
import
java.io.BufferedInputStream
;
import
java.io.BufferedOutputStream
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.nio.charset.Charset
;
import
java.util.ArrayList
;
import
java.util.Enumeration
;
import
java.util.List
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipFile
;
import
java.util.zip.ZipOutputStream
;
public
class
ZipHelper
{
/**
* @param dirFile
* @param zipFile
* @throws IOException
* @创建日期:2018年5月4日 下午3:07:55
* @作者: meter
* @描述:压缩指定目录下所有文件
* @Title: zipDirectory
*/
public
static
void
zipDirectory
(
File
dirFile
,
File
zipFile
)
throws
IOException
{
if
(
dirFile
==
null
||
!
dirFile
.
isDirectory
())
{
throw
new
CodeException
(
"["
+
dirFile
.
getName
()
+
"]不是一个文件夹,或者不存在。"
);
}
if
(
zipFile
==
null
)
{
zipFile
=
new
File
(
dirFile
.
getAbsolutePath
()
+
".zip"
);
}
String
dirName
=
dirFile
.
getName
()
+
File
.
separator
;
// 创建zip输出流
ZipOutputStream
zipOutStream
=
new
ZipOutputStream
(
new
FileOutputStream
(
zipFile
),
Charset
.
forName
(
"UTF-8"
));
try
{
// 创建缓冲输出流
BufferedOutputStream
bufferOutStream
=
new
BufferedOutputStream
(
zipOutStream
);
try
{
dealDirFile
(
dirFile
,
dirName
,
bufferOutStream
,
zipOutStream
);
}
finally
{
//最后关闭输出流
bufferOutStream
.
close
();
}
}
finally
{
zipOutStream
.
close
();
}
}
/**
* 讲文件添加到新目录,并且添加一个新文件
*
* @param zipTo 目标ZIP
* @param zipFrom 来源ZIP
* @param sourcePath 下载路径
* @param sourceFiles 需要添加的文件
* @throws Exception
*/
public
static
void
zipTo
(
File
zipTo
,
File
zipFrom
,
String
sourcePath
,
File
...
sourceFiles
)
throws
IOException
{
// 创建zip输出流
ZipOutputStream
zipOutStream
=
new
ZipOutputStream
(
new
FileOutputStream
(
zipTo
),
Charset
.
forName
(
"UTF-8"
));
try
{
// 创建缓冲输出流
BufferedOutputStream
bufferOutStream
=
new
BufferedOutputStream
(
zipOutStream
);
try
{
copyZip
(
zipFrom
,
zipOutStream
,
bufferOutStream
,
getExpand
(
sourcePath
,
sourceFiles
));
for
(
File
sourceFile
:
sourceFiles
)
{
zipFile
(
sourcePath
,
sourceFile
,
zipOutStream
,
bufferOutStream
);
}
}
finally
{
//最后关闭输出流
bufferOutStream
.
close
();
}
}
finally
{
zipOutStream
.
close
();
}
}
/**
* 讲文件添加到新目录,并且添加一个新文件
*
* @param zipTo 目标ZIP
* @param sourcePath 下载路径
* @param sourceFiles 需要添加的文件
* @throws Exception
*/
public
static
void
zip
(
File
zipTo
,
String
sourcePath
,
File
...
sourceFiles
)
throws
IOException
{
System
.
out
.
println
(
"待压缩文件:"
+
zipTo
.
getAbsolutePath
());
boolean
exints
=
zipTo
.
exists
();
// 添加到已经存在的压缩文件中
File
tempFile
=
new
File
(
zipTo
.
getAbsolutePath
()
+
".tmp"
);
// 创建zip输出流
ZipOutputStream
zipOutStream
;
if
(
exints
)
{
// 创建临时压缩文件
zipOutStream
=
new
ZipOutputStream
(
new
FileOutputStream
(
tempFile
),
Charset
.
forName
(
"UTF-8"
));
}
else
{
// 新创建压缩文件
zipOutStream
=
new
ZipOutputStream
(
new
FileOutputStream
(
zipTo
),
Charset
.
forName
(
"UTF-8"
));
}
try
{
// 创建缓冲输出流
BufferedOutputStream
bufferOutStream
=
new
BufferedOutputStream
(
zipOutStream
);
try
{
if
(
exints
)
{
copyZip
(
zipTo
,
zipOutStream
,
bufferOutStream
,
getExpand
(
sourcePath
,
sourceFiles
));
}
for
(
File
sourceFile
:
sourceFiles
)
{
zipFile
(
sourcePath
,
sourceFile
,
zipOutStream
,
bufferOutStream
);
}
}
finally
{
//最后关闭输出流
bufferOutStream
.
close
();
}
}
finally
{
zipOutStream
.
close
();
}
// 假如存在,则删除临时文件,并更改临时文件名称
if
(
tempFile
.
exists
())
{
boolean
flag
=
zipTo
.
delete
();
if
(
flag
)
{
tempFile
.
renameTo
(
zipTo
);
}
else
{
throw
new
CodeException
(
"删除文件失败"
);
}
}
}
/**
* 获取需要排除的文件名和文件路径
*
* @param sourcePath 文件路径
* @param sourceFiles 文件名
* @return
*/
private
static
List
<
String
>
getExpand
(
String
sourcePath
,
File
[]
sourceFiles
)
{
List
<
String
>
ret
=
new
ArrayList
<>();
for
(
File
sourceFile
:
sourceFiles
)
{
ret
.
add
(
sourcePath
+
sourceFile
.
getName
());
}
return
ret
;
}
/**
* 讲压缩文件保存到另外一个压缩文件
*
* @param fromFile 来源文件
* @param zipOutStream 目标文件
* @param bufferOutStream 目标文件流
* @param expandFiles 需要排除的文件
* @throws IOException
*/
private
static
void
copyZip
(
File
fromFile
,
ZipOutputStream
zipOutStream
,
BufferedOutputStream
bufferOutStream
,
List
<
String
>
expandFiles
)
throws
IOException
{
ZipFile
fromZipFile
=
new
ZipFile
(
fromFile
);
Enumeration
<?
extends
ZipEntry
>
fromEntries
=
fromZipFile
.
entries
();
while
(
fromEntries
.
hasMoreElements
())
{
ZipEntry
fromEntry
=
fromEntries
.
nextElement
();
if
(
expandFiles
.
indexOf
(
fromEntry
.
getName
())
>
-
1
)
{
continue
;
}
zipOutStream
.
putNextEntry
(
new
ZipEntry
(
fromEntry
.
getName
()));
if
(!
fromEntry
.
isDirectory
())
{
write
(
fromZipFile
.
getInputStream
(
fromEntry
),
bufferOutStream
);
}
zipOutStream
.
closeEntry
();
}
fromZipFile
.
close
();
}
/**
* 执行文件压缩
*
* @param file 需要压缩的文件
* @param zipOutStream 目标文件
* @param bufferOutStream 目标文件流
* @throws IOException
*/
private
static
void
zipFile
(
String
sourcePath
,
File
file
,
ZipOutputStream
zipOutStream
,
BufferedOutputStream
bufferOutStream
)
throws
IOException
{
if
(!
file
.
exists
())
{
throw
new
CodeException
(
"文件"
+
file
.
getAbsolutePath
()
+
"不存在"
);
}
// 创建压缩文件实体
ZipEntry
entry
=
new
ZipEntry
(
sourcePath
+
file
.
getName
());
// 添加实体
zipOutStream
.
putNextEntry
(
entry
);
// 创建输入流
BufferedInputStream
bufferInputStream
=
new
BufferedInputStream
(
new
FileInputStream
(
file
));
write
(
bufferInputStream
,
bufferOutStream
);
zipOutStream
.
closeEntry
();
}
/**
* @param inputStream
* @param outStream
* @throws IOException
* @创建日期:2018年5月4日 下午2:09:37
* @作者: meter
* @描述:读写zip文件
* @Title: write
*/
private
static
void
write
(
InputStream
inputStream
,
OutputStream
outStream
)
throws
IOException
{
try
{
byte
[]
data
=
new
byte
[
4096
];
int
length
=
0
;
while
((
length
=
inputStream
.
read
(
data
))
!=
-
1
)
{
outStream
.
write
(
data
,
0
,
length
);
}
outStream
.
flush
();
//刷新输出流
}
finally
{
inputStream
.
close
();
//关闭输入流
}
}
/**
* @param dirFile
* @param parentDir
* @param bufferOutStream
* @param zipOutStream
* @throws IOException
* @创建日期:2018年5月4日 下午4:38:46
* @作者: meter
* @描述:处理目录文件
* @Title: dealDirFile
*/
private
static
void
dealDirFile
(
File
dirFile
,
String
parentDir
,
BufferedOutputStream
bufferOutStream
,
ZipOutputStream
zipOutStream
)
throws
IOException
{
File
[]
fileList
=
dirFile
.
listFiles
();
for
(
File
file
:
fileList
)
{
if
(
file
.
isFile
())
{
// 创建压缩文件实体
ZipEntry
entry
=
new
ZipEntry
(
parentDir
+
file
.
getName
());
// 添加实体
zipOutStream
.
putNextEntry
(
entry
);
// 创建输入流
BufferedInputStream
bufferInputStream
=
new
BufferedInputStream
(
new
FileInputStream
(
file
));
write
(
bufferInputStream
,
bufferOutStream
);
}
else
{
dealDirFile
(
file
,
parentDir
+
file
.
getName
()
+
File
.
separator
,
bufferOutStream
,
zipOutStream
);
}
}
}
/**
* @param dirPath
* @param zipPath
* @throws IOException
* @创建日期:2018年5月4日 下午3:22:11
* @作者: meter
* @描述:重载zipDirectory
* @Title: zipDirectory
*/
public
static
void
zipDirectory
(
String
dirPath
,
String
zipPath
)
throws
IOException
{
if
(
zipPath
==
null
||
""
.
equals
(
zipPath
))
{
zipDirectory
(
new
File
(
dirPath
),
null
);
}
else
{
zipDirectory
(
new
File
(
dirPath
),
new
File
(
zipPath
));
}
}
//------------------------------------------------米特华丽的分割线----------------------------------------------------------------------------------------
/**
* @param zipFile
* @param destDir
* @throws IOException
* @创建日期:2018年5月4日 下午4:00:41
* @作者: meter
* @描述:解压文件
* @Title: unzip
*/
public
static
void
unzip
(
File
zipFile
,
File
destDir
)
throws
IOException
{
ZipFile
zipOutFile
=
new
ZipFile
(
zipFile
,
Charset
.
forName
(
"gbk"
));
Enumeration
<?
extends
ZipEntry
>
entries
=
zipOutFile
.
entries
();
while
(
entries
.
hasMoreElements
())
{
ZipEntry
entry
=
entries
.
nextElement
();
if
(
entry
.
isDirectory
())
{
File
tempFile
=
new
File
(
destDir
.
getAbsolutePath
()
+
File
.
separator
+
entry
.
getName
());
if
(!
tempFile
.
exists
())
{
tempFile
.
mkdirs
();
}
}
else
{
File
tempFile
=
new
File
(
destDir
.
getAbsolutePath
()
+
File
.
separator
+
entry
.
getName
());
checkParentDir
(
tempFile
);
FileOutputStream
fileOutStream
=
new
FileOutputStream
(
tempFile
);
BufferedOutputStream
bufferOutStream
=
new
BufferedOutputStream
(
fileOutStream
);
write
(
zipOutFile
.
getInputStream
(
entry
),
bufferOutStream
);
bufferOutStream
.
close
();
fileOutStream
.
close
();
}
}
zipOutFile
.
close
();
//记得关闭zip文件
}
/**
* @param file
* @创建日期:2018年5月4日 下午4:37:41
* @作者: meter
* @描述:验证父目录是否存在,否则创建
* @Title: checkParentDir
*/
private
static
void
checkParentDir
(
File
file
)
{
if
(!
file
.
getParentFile
().
exists
())
{
file
.
getParentFile
().
mkdirs
();
}
}
}
\ No newline at end of file
yzg-util-base/src/main/java/com/yanzuoguang/util/thread/ProcessData.java
0 → 100644
View file @
f89af689
package
com
.
yanzuoguang
.
util
.
thread
;
import
com.yanzuoguang.util.helper.HttpHelper
;
import
com.yanzuoguang.util.log.Log
;
/**
* 进度数据
*/
public
class
ProcessData
{
/**
* 目标
*/
private
String
target
;
/**
* 位置
*/
private
long
pos
;
/**
* 长度
*/
private
long
total
;
public
ProcessData
()
{
}
public
ProcessData
(
String
target
)
{
this
.
target
=
target
;
}
public
ProcessData
(
String
target
,
long
total
)
{
this
.
target
=
target
;
this
.
total
=
total
;
}
public
String
getTarget
()
{
return
target
;
}
public
void
setTarget
(
String
target
)
{
this
.
target
=
target
;
}
public
long
getPos
()
{
return
pos
;
}
public
void
setPos
(
long
pos
)
{
this
.
pos
=
pos
;
}
public
long
getTotal
()
{
return
total
;
}
public
void
setTotal
(
long
total
)
{
this
.
total
=
total
;
}
/**
* 进度调用
*
* @param runProcess
* @param pos
*/
public
void
processCall
(
RunProcess
runProcess
,
long
pos
)
{
this
.
pos
=
pos
;
if
(
runProcess
==
null
)
{
return
;
}
try
{
runProcess
.
execute
(
this
);
}
catch
(
Exception
ex
)
{
Log
.
error
(
HttpHelper
.
class
,
ex
);
}
}
/**
* 进度调用
*
* @param runProcess 运行进度
* @param add 需要添加的量
*/
public
void
processAdd
(
RunProcess
runProcess
,
long
add
)
{
this
.
pos
+=
add
;
this
.
processCall
(
runProcess
,
this
.
pos
);
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/thread/RunProcess.java
0 → 100644
View file @
f89af689
package
com
.
yanzuoguang
.
util
.
thread
;
/**
* 运行进度处理
*/
public
interface
RunProcess
{
/**
* 进度信息
*
* @param data
*/
void
execute
(
ProcessData
data
);
}
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/helper/HttpFileHelper.java
View file @
f89af689
package
com
.
yanzuoguang
.
cloud
.
helper
;
import
com.yanzuoguang.util.helper.HttpHelper
;
import
javax.servlet.ServletOutputStream
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.*
;
import
java.net.URL
;
import
java.net.URLConnection
;
/**
* HTTP文件
*/
public
class
HttpFileHelper
{
/**
* 下载文件
*
* @param serverFileName 下载的服务器文件路径
* @param localFileName 保存的文件路径
* @throws IOException
*/
public
static
void
downToLocal
(
String
serverFileName
,
String
localFileName
)
throws
IOException
{
int
byteRead
;
URL
url
=
new
URL
(
serverFileName
);
URLConnection
conn
=
url
.
openConnection
();
InputStream
inStream
=
conn
.
getInputStream
();
try
{
FileOutputStream
outputStream
=
new
FileOutputStream
(
localFileName
);
try
{
byte
[]
buffer
=
new
byte
[
1204
];
while
((
byteRead
=
inStream
.
read
(
buffer
))
!=
-
1
)
{
outputStream
.
write
(
buffer
,
0
,
byteRead
);
}
}
finally
{
outputStream
.
close
();
}
}
finally
{
inStream
.
close
();
}
}
public
class
HttpFileHelper
extends
HttpHelper
{
/**
* 下载文件
...
...
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