第一步:建立压缩格式和压缩byte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
package
util;
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.util.Collection;
import
java.util.Enumeration;
import
java.util.zip.ZipFile;
import
org.apache.tools.zip.ZipEntry;
import
org.apache.tools.zip.ZipOutputStream;
/**
*
*@author huyongjian Oracle(Compus Solution Group)
* @Date 2013-8-2
* @version 2.0
* @since JDK1.6(建议)
Copy Right Information COMPUS SOLUTION GROUP
IDE:Eclipse
class:
*/
public
class
Compression {
private
static
final
int
BUFF_SIZE =
1024
*
1024
;
//1M Byte
/**
* 批量压缩文件(夹)
* @param resFileList 要压缩的文件(夹)列表
* @param zipFile 生成的压缩文件
* @throws IOException 当压缩过程出错时抛出
*/
public
static
void
zipFiles(Collection<File> resFileList, File zipFile)
throws
IOException {
ZipOutputStream zipout =
new
ZipOutputStream(
new
BufferedOutputStream(
new
FileOutputStream(zipFile), BUFF_SIZE));
for
(File resFile : resFileList) {
zipFile(resFile, zipout,
""
);
}
zipout.close();
}
/**
* 批量压缩文件(夹)
* @param resFileList 要压缩的文件(夹)列表
* @param zipFile 生成的压缩文件
* @param comment 压缩文件的注释
* @throws IOException 当压缩过程出错时抛出
*/
public
static
void
zipFiles(Collection<File> resFileList, File zipFile, String comment)
throws
IOException {
ZipOutputStream zipout =
new
ZipOutputStream(
new
BufferedOutputStream(
new
FileOutputStream(zipFile), BUFF_SIZE));
for
(File resFile : resFileList) {
zipFile(resFile, zipout,
""
);
}
zipout.setComment(comment);
zipout.close();
}
/**
* 解压缩一个文件
* @param zipFile 压缩文件
* @param folderPath 解压缩的目标目录
* @throws IOException 当压缩过程出错时抛出
*/
public
static
void
upZipFile(File zipFile, String folderPath)
throws
IOException {
ZipFile zf =
new
ZipFile(zipFile);
for
(Enumeration entries = zf.entries(); entries.hasMoreElements();) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
InputStream in = zf.getInputStream(entry);
OutputStream out =
new
FileOutputStream(folderPath + File.separator + entry.getName());
byte
buffer[] =
new
byte
[BUFF_SIZE];
int
realLength;
while
((realLength = in.read(buffer)) >
0
) {
out.write(buffer,
0
, realLength);
}
in.close();
out.close();
}
}
private
static
void
zipFile(File resFile, ZipOutputStream zipout, String rootpath)
throws
IOException {
rootpath = rootpath + (rootpath.trim().length() ==
0
?
""
: File.separator) + resFile.getName();
if
(resFile.isDirectory()) {
File[] fileList = resFile.listFiles();
for
(File file : fileList) {
zipFile(file, zipout, rootpath);
}
}
else
{
byte
buffer[] =
new
byte
[BUFF_SIZE];
BufferedInputStream in =
new
BufferedInputStream(
new
FileInputStream(resFile), BUFF_SIZE);
zipout.putNextEntry(
new
ZipEntry(rootpath));
int
realLength;
while
((realLength = in.read(buffer)) != -
1
) {
zipout.write(buffer,
0
, realLength);
}
in.close();
zipout.flush();
zipout.closeEntry();
}
}
}
|
第二步:简单测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package
test;
import
java.io.File;
import
java.io.IOException;
import
java.util.ArrayList;
import
java.util.Collection;
import
util.Compression;
/**
*
*@author huyongjian Oracle(Compus Solution Group)
* @Date 2013-7-25
* @version 2.0
* @since JDK1.6(建议)
Copy Right Information COMPUS SOLUTION GROUP
IDE:Eclipse
class:
*/
public
class
CompressionTest {
public
static
void
main(String[] args)
throws
IOException {
Collection<File> resFileList =
new
ArrayList<File>();
resFileList.add(
new
File(
"C:\\这是一个测试文件.doc"
));
File zipFile =
new
File(
"C:\\txxxt.zip"
);
Compression.zipFiles(resFileList, zipFile);
}
}
|
读者朋友在你的C盘建立一个------ 这是一个测试文件.doc------运行CompressionTest.java文件在你的C盘出现的这个txxxt.zip文件
但是打开我们的文件,却出现乱码了
这种问题很好解决:
使用下面的两个包代替上面的两个包
这个两个包我下面会上传进来的!
本文转自 小夜的传说 51CTO博客,原文链接:http://blog.51cto.com/1936625305/1282142,如需转载请自行联系原作者