ZipUtil.java
7.5 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package com.myproject.util;
/**
* Created by sunke on 2017/11/9.
*/
import java.io.*;
import java.util.zip.*;
/**
* ZIP压缩工具
*/
public class ZipUtil {
public static final String EXT = ".zip";
private static final String BASE_DIR = "";
// 符号"/"用来作为目录标识判断符
private static final String PATH = "/";
private static final int BUFFER = 1024;
/**
* 压缩
*
* @param srcFile
* @throws Exception
*/
public static void compress(File srcFile) throws Exception {
String name = srcFile.getName();
File basePath = srcFile.getParentFile();
File dest = new File(basePath, name + EXT);
compress(srcFile, dest);
}
/**
* 压缩
*
* @param srcFile
* 源路径
* @param destFile
* 目标路径
* @throws Exception
*/
public static void compress(File srcFile, File destFile) throws Exception {
// 对输出文件做CRC32校验
CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(
destFile), new CRC32());
ZipOutputStream zos = new ZipOutputStream(cos);
compress(srcFile, zos, BASE_DIR);
zos.flush();
zos.close();
}
/**
* 压缩文件
*
* @param srcFile
* @param destPath
* @throws Exception
*/
public static void compress(File srcFile, String destPath) throws Exception {
compress(srcFile, new File(destPath));
}
/**
* 压缩
*
* @param srcFile
* 源路径
* @param zos
* ZipOutputStream
* @param basePath
* 压缩包内相对路径
* @throws Exception
*/
private static void compress(File srcFile, ZipOutputStream zos,
String basePath) throws Exception {
if (srcFile.isDirectory()) {
compressDir(srcFile, zos, basePath);
} else {
compressFile(srcFile, zos, basePath);
}
}
/**
* 压缩
*
* @param srcPath
* @throws Exception
*/
public static void compress(String srcPath) throws Exception {
File srcFile = new File(srcPath);
compress(srcFile);
}
/**
* 文件压缩
*
* @param srcPath
* 源文件路径
* @param destPath
* 目标文件路径
*
*/
public static void compress(String srcPath, String destPath)
throws Exception {
File srcFile = new File(srcPath);
compress(srcFile, destPath);
}
/**
* 压缩目录
*
* @param dir
* @param zos
* @param basePath
* @throws Exception
*/
private static void compressDir(File dir, ZipOutputStream zos,
String basePath) throws Exception {
File[] files = dir.listFiles();
// 构建空目录
if (files.length < 1) {
ZipEntry entry = new ZipEntry(basePath + dir.getName() + PATH);
zos.putNextEntry(entry);
zos.closeEntry();
}
for (File file : files) {
// 递归压缩
compress(file, zos, basePath + dir.getName() + PATH);
}
}
/**
* 文件压缩
*
* @param file
* 待压缩文件
* @param zos
* ZipOutputStream
* @param dir
* 压缩文件中的当前路径
* @throws Exception
*/
private static void compressFile(File file, ZipOutputStream zos, String dir)
throws Exception {
/**
* 压缩包内文件名定义
*
* <pre>
* 如果有多级目录,那么这里就需要给出包含目录的文件名
* 如果用WinRAR打开压缩包,中文名将显示为乱码
* </pre>
*/
ZipEntry entry = new ZipEntry(dir + file.getName());
zos.putNextEntry(entry);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
zos.write(data, 0, count);
}
bis.close();
zos.closeEntry();
}
/**
* 文件 解压缩
*
* @param srcPath
* 源文件路径
*
* @throws Exception
*/
public static void decompress(String srcPath) throws Exception {
File srcFile = new File(srcPath);
decompress(srcFile);
}
/**
* 解压缩
*
* @param srcFile
* @throws Exception
*/
public static void decompress(File srcFile) throws Exception {
String basePath = srcFile.getParent();
decompress(srcFile, basePath);
}
/**
* 解压缩
*
* @param srcFile
* @param destFile
* @throws Exception
*/
public static void decompress(File srcFile, File destFile) throws Exception {
CheckedInputStream cis = new CheckedInputStream(new FileInputStream(
srcFile), new CRC32());
ZipInputStream zis = new ZipInputStream(cis);
decompress(destFile, zis);
zis.close();
}
/**
* 解压缩
*
* @param srcFile
* @param destPath
* @throws Exception
*/
public static void decompress(File srcFile, String destPath)
throws Exception {
decompress(srcFile, new File(destPath));
}
/**
* 文件 解压缩
*
* @param srcPath
* 源文件路径
* @param destPath
* 目标文件路径
* @throws Exception
*/
public static void decompress(String srcPath, String destPath)
throws Exception {
File srcFile = new File(srcPath);
decompress(srcFile, destPath);
}
/**
* 文件 解压缩
*
* @param destFile
* 目标文件
* @param zis
* ZipInputStream
* @throws Exception
*/
private static void decompress(File destFile, ZipInputStream zis)
throws Exception {
ZipEntry entry = null;
while ((entry = zis.getNextEntry()) != null) {
// 文件
String dir = destFile.getPath() + File.separator + entry.getName();
File dirFile = new File(dir);
// 文件检查
fileProber(dirFile);
if (entry.isDirectory()) {
dirFile.mkdirs();
} else {
decompressFile(dirFile, zis);
}
zis.closeEntry();
}
}
/**
* 文件探针
*
*
* 当父目录不存在时,创建目录!
*
*
* @param dirFile
*/
private static void fileProber(File dirFile) {
File parentFile = dirFile.getParentFile();
if (!parentFile.exists()) {
// 递归寻找上级目录
fileProber(parentFile);
parentFile.mkdir();
}
}
/**
* 文件解压缩
*
* @param destFile
* 目标文件
* @param zis
* ZipInputStream
* @throws Exception
*/
private static void decompressFile(File destFile, ZipInputStream zis)
throws Exception {
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destFile));
int count;
byte data[] = new byte[BUFFER];
while ((count = zis.read(data, 0, BUFFER)) != -1) {
bos.write(data, 0, count);
}
bos.close();
}
}