java压缩/解压缩zip格式文件
因为项目要用到压缩、解压缩zip格式压缩包,只好自己封装一个,对于网上流行的中文乱码的问题,本文的解决方法是用apache的包代替jdk里的。基本上还是比较好用的。
废话少说,直接上代码。
1
package com.resoft.util;
2![]()
3
import java.io.BufferedOutputStream;
4
import java.io.File;
5
import java.io.FileInputStream;
6
import java.io.FileNotFoundException;
7
import java.io.FileOutputStream;
8
import java.io.IOException;
9
import java.io.InputStream;
10
import java.io.OutputStream;
11
import java.util.Enumeration;
12![]()
13
import org.apache.tools.zip.ZipEntry;
14
import org.apache.tools.zip.ZipFile;
15
import org.apache.tools.zip.ZipOutputStream;
16![]()
17
/**
18
* 压缩/解压缩zip包处理类
19
*
20
* @author yayagepei
21
* @date 2008-8-25
22
*/
23
public class ZipUtil {
24![]()
25
/**
26
* 压缩
27
*
28
* @param zipFileName
29
* 压缩产生的zip包文件名--带路径,如果为null或空则默认按文件名生产压缩文件名
30
* @param relativePath
31
* 相对路径,默认为空
32
* @param directory
33
* 文件或目录的绝对路径
34
* @throws FileNotFoundException
35
* @throws IOException
36
* @author yayagepei
37
* @date 2008-8-26
38
*/
39
public static void zip(String zipFileName, String relativePath,
40
String directory) throws FileNotFoundException, IOException {
41
String fileName = zipFileName;
42
if (fileName == null || fileName.trim().equals("")) {
43
File temp = new File(directory);
44
if (temp.isDirectory()) {
45
fileName = directory + ".zip";
46
} else {
47
if (directory.indexOf(".") > 0) {
48
fileName = directory.substring(0, directory
49
.lastIndexOf("."))
50
+ "zip";
51
} else {
52
fileName = directory + ".zip";
53
}
54
}
55
}
56
ZipOutputStream zos = new ZipOutputStream(
57
new FileOutputStream(fileName));
58
try {
59
zip(zos, relativePath, directory);
60
} catch (IOException ex) {
61
throw ex;
62
} finally {
63
if (null != zos) {
64
zos.close();
65
}
66
}
67
}
68![]()
69
/**
70
* 压缩
71
*
72
* @param zos
73
* 压缩输出流
74
* @param relativePath
75
* 相对路径
76
* @param absolutPath
77
* 文件或文件夹绝对路径
78
* @throws IOException
79
* @author yayagepei
80
* @date 2008-8-26
81
*/
82
private static void zip(ZipOutputStream zos, String relativePath,
83
String absolutPath) throws IOException {
84
File file = new File(absolutPath);
85
if (file.isDirectory()) {
86
File[] files = file.listFiles();
87
for (int i = 0; i < files.length; i++) {
88
File tempFile = files[i];
89
if (tempFile.isDirectory()) {
90
String newRelativePath = relativePath + tempFile.getName()
91
+ File.separator;
92
createZipNode(zos, newRelativePath);
93
zip(zos, newRelativePath, tempFile.getPath());
94
} else {
95
zipFile(zos, tempFile, relativePath);
96
}
97
}
98
} else {
99
zipFile(zos, file, relativePath);
100
}
101
}
102![]()
103
/**
104
* 压缩文件
105
*
106
* @param zos
107
* 压缩输出流
108
* @param file
109
* 文件对象
110
* @param relativePath
111
* 相对路径
112
* @throws IOException
113
* @author yayagepei
114
* @date 2008-8-26
115
*/
116
private static void zipFile(ZipOutputStream zos, File file,
117
String relativePath) throws IOException {
118
ZipEntry entry = new ZipEntry(relativePath + file.getName());
119
zos.putNextEntry(entry);
120
InputStream is = null;
121
try {
122
is = new FileInputStream(file);
123
int BUFFERSIZE = 2 << 10;
124
int length = 0;
125
byte[] buffer = new byte[BUFFERSIZE];
126
while ((length = is.read(buffer, 0, BUFFERSIZE)) >= 0) {
127
zos.write(buffer, 0, length);
128
}
129
zos.flush();
130
zos.closeEntry();
131
} catch (IOException ex) {
132
throw ex;
133
} finally {
134
if (null != is) {
135
is.close();
136
}
137
}
138
}
139![]()
140
/**
141
* 创建目录
142
*
143
* @param zos
144
* zip输出流
145
* @param relativePath
146
* 相对路径
147
* @throws IOException
148
* @author yayagepei
149
* @date 2008-8-26
150
*/
151
private static void createZipNode(ZipOutputStream zos, String relativePath)
152
throws IOException {
153
ZipEntry zipEntry = new ZipEntry(relativePath);
154
zos.putNextEntry(zipEntry);
155
zos.closeEntry();
156
}
157![]()
158
/**
159
* 解压缩zip包
160
*
161
* @param zipFilePath
162
* zip文件路径
163
* @param targetPath
164
* 解压缩到的位置,如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下
165
* @throws IOException
166
* @author yayagepei
167
* @date 2008-9-28
168
*/
169
public static void unzip(String zipFilePath, String targetPath)
170
throws IOException {
171
OutputStream os = null;
172
InputStream is = null;
173
ZipFile zipFile = null;
174
try {
175
zipFile = new ZipFile(zipFilePath);
176
String directoryPath = "";
177
if (null == targetPath || "".equals(targetPath)) {
178
directoryPath = zipFilePath.substring(0, zipFilePath
179
.lastIndexOf("."));
180
} else {
181
directoryPath = targetPath;
182
}
183
Enumeration entryEnum = zipFile.getEntries();
184
if (null != entryEnum) {
185
ZipEntry zipEntry = null;
186
while (entryEnum.hasMoreElements()) {
187
zipEntry = (ZipEntry) entryEnum.nextElement();
188
if (zipEntry.isDirectory()) {
189
directoryPath = directoryPath + File.separator
190
+ zipEntry.getName();
191
System.out.println(directoryPath);
192
continue;
193
}
194
if (zipEntry.getSize() > 0) {
195
// 文件
196
File targetFile = FileUtil.buildFile(directoryPath
197
+ File.separator + zipEntry.getName(), false);
198
os = new BufferedOutputStream(new FileOutputStream(
199
targetFile));
200
is = zipFile.getInputStream(zipEntry);
201
byte[] buffer = new byte[4096];
202
int readLen = 0;
203
while ((readLen = is.read(buffer, 0, 4096)) >= 0) {
204
os.write(buffer, 0, readLen);
205
}
206![]()
207
os.flush();
208
os.close();
209
} else {
210
// 空目录
211
FileUtil.buildFile(directoryPath + File.separator
212
+ zipEntry.getName(), true);
213
}
214
}
215
}
216
} catch (IOException ex) {
217
throw ex;
218
} finally {
219
if(null != zipFile){
220
zipFile = null;
221
}
222
if (null != is) {
223
is.close();
224
}
225
if (null != os) {
226
os.close();
227
}
228
}
229
}
230
}
package com.resoft.util;2

3
import java.io.BufferedOutputStream;4
import java.io.File;5
import java.io.FileInputStream;6
import java.io.FileNotFoundException;7
import java.io.FileOutputStream;8
import java.io.IOException;9
import java.io.InputStream;10
import java.io.OutputStream;11
import java.util.Enumeration;12

13
import org.apache.tools.zip.ZipEntry;14
import org.apache.tools.zip.ZipFile;15
import org.apache.tools.zip.ZipOutputStream;16

17
/**18
* 压缩/解压缩zip包处理类19
* 20
* @author yayagepei21
* @date 2008-8-2522
*/23
public class ZipUtil {24

25
/**26
* 压缩27
* 28
* @param zipFileName29
* 压缩产生的zip包文件名--带路径,如果为null或空则默认按文件名生产压缩文件名30
* @param relativePath31
* 相对路径,默认为空32
* @param directory33
* 文件或目录的绝对路径34
* @throws FileNotFoundException35
* @throws IOException36
* @author yayagepei37
* @date 2008-8-2638
*/39
public static void zip(String zipFileName, String relativePath,40
String directory) throws FileNotFoundException, IOException {41
String fileName = zipFileName;42
if (fileName == null || fileName.trim().equals("")) {43
File temp = new File(directory);44
if (temp.isDirectory()) {45
fileName = directory + ".zip";46
} else {47
if (directory.indexOf(".") > 0) {48
fileName = directory.substring(0, directory49
.lastIndexOf("."))50
+ "zip";51
} else {52
fileName = directory + ".zip";53
}54
}55
}56
ZipOutputStream zos = new ZipOutputStream(57
new FileOutputStream(fileName));58
try {59
zip(zos, relativePath, directory);60
} catch (IOException ex) {61
throw ex;62
} finally {63
if (null != zos) {64
zos.close();65
}66
}67
}68

69
/**70
* 压缩71
* 72
* @param zos73
* 压缩输出流74
* @param relativePath75
* 相对路径76
* @param absolutPath77
* 文件或文件夹绝对路径78
* @throws IOException79
* @author yayagepei80
* @date 2008-8-2681
*/82
private static void zip(ZipOutputStream zos, String relativePath,83
String absolutPath) throws IOException {84
File file = new File(absolutPath);85
if (file.isDirectory()) {86
File[] files = file.listFiles();87
for (int i = 0; i < files.length; i++) {88
File tempFile = files[i];89
if (tempFile.isDirectory()) {90
String newRelativePath = relativePath + tempFile.getName()91
+ File.separator;92
createZipNode(zos, newRelativePath);93
zip(zos, newRelativePath, tempFile.getPath());94
} else {95
zipFile(zos, tempFile, relativePath);96
}97
}98
} else {99
zipFile(zos, file, relativePath);100
}101
}102

103
/**104
* 压缩文件105
* 106
* @param zos107
* 压缩输出流108
* @param file109
* 文件对象110
* @param relativePath111
* 相对路径112
* @throws IOException113
* @author yayagepei114
* @date 2008-8-26115
*/116
private static void zipFile(ZipOutputStream zos, File file,117
String relativePath) throws IOException {118
ZipEntry entry = new ZipEntry(relativePath + file.getName());119
zos.putNextEntry(entry);120
InputStream is = null;121
try {122
is = new FileInputStream(file);123
int BUFFERSIZE = 2 << 10;124
int length = 0;125
byte[] buffer = new byte[BUFFERSIZE];126
while ((length = is.read(buffer, 0, BUFFERSIZE)) >= 0) {127
zos.write(buffer, 0, length);128
}129
zos.flush();130
zos.closeEntry();131
} catch (IOException ex) {132
throw ex;133
} finally {134
if (null != is) {135
is.close();136
}137
}138
}139

140
/**141
* 创建目录142
* 143
* @param zos144
* zip输出流145
* @param relativePath146
* 相对路径147
* @throws IOException148
* @author yayagepei149
* @date 2008-8-26150
*/151
private static void createZipNode(ZipOutputStream zos, String relativePath)152
throws IOException {153
ZipEntry zipEntry = new ZipEntry(relativePath);154
zos.putNextEntry(zipEntry);155
zos.closeEntry();156
}157

158
/**159
* 解压缩zip包160
* 161
* @param zipFilePath162
* zip文件路径163
* @param targetPath164
* 解压缩到的位置,如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下165
* @throws IOException166
* @author yayagepei167
* @date 2008-9-28168
*/169
public static void unzip(String zipFilePath, String targetPath)170
throws IOException {171
OutputStream os = null;172
InputStream is = null;173
ZipFile zipFile = null;174
try {175
zipFile = new ZipFile(zipFilePath);176
String directoryPath = "";177
if (null == targetPath || "".equals(targetPath)) {178
directoryPath = zipFilePath.substring(0, zipFilePath179
.lastIndexOf("."));180
} else {181
directoryPath = targetPath;182
}183
Enumeration entryEnum = zipFile.getEntries();184
if (null != entryEnum) {185
ZipEntry zipEntry = null;186
while (entryEnum.hasMoreElements()) {187
zipEntry = (ZipEntry) entryEnum.nextElement();188
if (zipEntry.isDirectory()) {189
directoryPath = directoryPath + File.separator190
+ zipEntry.getName();191
System.out.println(directoryPath);192
continue;193
}194
if (zipEntry.getSize() > 0) {195
// 文件196
File targetFile = FileUtil.buildFile(directoryPath197
+ File.separator + zipEntry.getName(), false);198
os = new BufferedOutputStream(new FileOutputStream(199
targetFile));200
is = zipFile.getInputStream(zipEntry);201
byte[] buffer = new byte[4096];202
int readLen = 0;203
while ((readLen = is.read(buffer, 0, 4096)) >= 0) {204
os.write(buffer, 0, readLen);205
}206

207
os.flush();208
os.close();209
} else {210
// 空目录211
FileUtil.buildFile(directoryPath + File.separator212
+ zipEntry.getName(), true);213
}214
}215
}216
} catch (IOException ex) {217
throw ex;218
} finally {219
if(null != zipFile){220
zipFile = null;221
}222
if (null != is) {223
is.close();224
}225
if (null != os) {226
os.close();227
}228
}229
}230
}231
补充一下里面用到的一个自己写的FileUtil的一个方法
/**
* 生产文件 如果文件所在路径不存在则生成路径
*
* @param fileName
* 文件名 带路径
* @param isDirectory 是否为路径
* @return
* @author yayagepei
* @date 2008-8-27
*/
public static File buildFile(String fileName, boolean isDirectory) {
File target = new File(fileName);
if (isDirectory) {
target.mkdirs();
} else {
if (!target.getParentFile().exists()) {
target.getParentFile().mkdirs();
target = new File(target.getAbsolutePath());
}
}
return target;
}



浙公网安备 33010602011771号