本文是用Java语言实现,使用的是java的类库。
实现的代码片段如下:
|
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
|
import java.util.zip.ZipEntry;import java.util.zip.ZipInputStream;import java.util.zip.ZipOutputStream;/**?????* 把一组文件压缩到指定的文件?????*?????* @param files?????* 文件组?????* @param zipFileName?????* 要压缩成的文件名(包括路径)?????* @param sPath?????* ZIP中的路径?????* @return 压缩文件?????*/????public static File compressFiles(File[] files,String zipFileName,String sPath)????{????????String sUploadPath = "test";????????File zipFile = new File(zipFileName);????????FileOutputStream fout = null;????????ZipOutputStream zipOut = null;????????ZipInputStream zipIn = null;????????FileInputStream fin = null;????????byte[] buff = new byte[1024];????????int rb;????????try????????{????????????if (zipFile.exists())????????????{????????????????File fileBak =?????????????????new File(zipFile.getAbsolutePath() + ".bak");????????????????if (zipFile.renameTo(fileBak))????????????????{????????????????????logger.debug("重命名文件成功:" + fileBak.getName());????????????????}????????????????fin = new FileInputStream(fileBak);????????????????zipIn = new ZipInputStream(fin);????????????????fout = new FileOutputStream(zipFile);????????????????zipOut = new ZipOutputStream(fout);????????????????ZipEntry ze = null;????????????????try????????????????{????????????????????while ((ze = zipIn.getNextEntry()) != null)????????????????????{????????????????????????zipOut.putNextEntry(ze);????????????????????????while ((rb = zipIn.read(buff)) != -1)????????????????????????{????????????????????????????zipOut.write(buff, 0, rb);????????????????????????}????????????????????}????????????????}????????????????catch (Exception e)????????????????{????????????????????logger.error("aa", e);????????????????}????????????????finally????????????????{????????????????????safeCloseInputStream(fin);????????????????????safeCloseInputStream(zipIn);????????????????????safeDeleteFile(fileBak);????????????????}????????????}????????????else????????????{????????????????fout = new FileOutputStream(zipFile);????????????????zipOut = new ZipOutputStream(fout);????????????}????????????ZipEntry ze = null;????????????for (int i = 0; i < files.length; i++)????????????{????????????????if (files[i] != null)????????????????{????????????????????try????????????????????{????????????????????????if (!files[i].exists())????????????????????????{????????????????????????????logger.error(?????????????????????????????files[i].getAbsolutePath() + "不存在");????????????????????????????continue;????????????????????????}????????????????????????fin = new FileInputStream(files[i]);????????????????????????if (StringUtils.isBlank(sPath))????????????????????????{????????????????????????????ze = new ZipEntry(files[i].getName());????????????????????????}????????????????????????else????????????????????????{?????????????????????????StringBuffer sbZipEntry = new StringBuffer();?????????????????????????sbZipEntry?????????????????????????????.append(sPath)?????????????????????????????.append(File.separator)?????????????????????????????.append(files[i].getAbsolutePath()?????????????????????????????.substring(sUploadPath.length()));?????????????????????????ze = new ZipEntry(sbZipEntry.toString());????????????????????????}????????????????????????zipOut.putNextEntry(ze);????????????????????????while ((rb = fin.read(buff)) != -1)????????????????????????{????????????????????????????zipOut.write(buff, 0, rb);????????????????????????}????????????????????}????????????????????finally????????????????????{????????????????????????safeCloseInputStream(fin);????????????????????}????????????????}????????????}????????????logger.info("文件压缩成功:" + zipFile.getName());????????????return zipFile;????????}????????catch (IOException e)????????{????????????logger.error(e);????????????safeDeleteFile(zipFile);????????????return null;????????}????????finally????????{????????????safeCloseOutStream(zipOut);????????????safeCloseOutStream(fout);????????}????} |