提交 | 用户 | 时间
|
1ac2bc
|
1 |
package cn.stylefeng.guns.workflow.core.util; |
懒 |
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.io.FileInputStream; |
|
5 |
import java.io.FileOutputStream; |
|
6 |
import java.util.zip.ZipEntry; |
|
7 |
import java.util.zip.ZipOutputStream; |
|
8 |
|
|
9 |
/** |
|
10 |
* java压缩成zip |
|
11 |
* |
|
12 |
* @author fengshuonan |
|
13 |
* @Date 2019/8/12 21:59 |
|
14 |
*/ |
|
15 |
public class FileZip { |
|
16 |
|
|
17 |
/** |
|
18 |
* @param inputFileName 你要压缩的文件夹(整个完整路径) |
|
19 |
* @param zipFileName 压缩后的文件(整个完整路径) |
|
20 |
* @throws Exception |
|
21 |
*/ |
|
22 |
public static Boolean zip(String inputFileName, String zipFileName) throws Exception { |
|
23 |
zip(zipFileName, new File(inputFileName)); |
|
24 |
return true; |
|
25 |
} |
|
26 |
|
|
27 |
private static void zip(String zipFileName, File inputFile) throws Exception { |
|
28 |
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName)); |
|
29 |
zip(out, inputFile, ""); |
|
30 |
out.flush(); |
|
31 |
out.close(); |
|
32 |
} |
|
33 |
|
|
34 |
private static void zip(ZipOutputStream out, File f, String base) throws Exception { |
|
35 |
if (f.isDirectory()) { |
|
36 |
File[] fl = f.listFiles(); |
|
37 |
out.putNextEntry(new ZipEntry(base + "/")); |
|
38 |
base = base.length() == 0 ? "" : base + "/"; |
|
39 |
for (int i = 0; i < fl.length; i++) { |
|
40 |
zip(out, fl[i], base + fl[i].getName()); |
|
41 |
} |
|
42 |
} else { |
|
43 |
out.putNextEntry(new ZipEntry(base)); |
|
44 |
FileInputStream in = new FileInputStream(f); |
|
45 |
int b; |
|
46 |
while ((b = in.read()) != -1) { |
|
47 |
out.write(b); |
|
48 |
} |
|
49 |
in.close(); |
|
50 |
} |
|
51 |
} |
|
52 |
|
|
53 |
} |