2
2
3
3
4
4
import java .io .BufferedInputStream ;
5
- import java .io .BufferedOutputStream ;
6
5
import java .io .File ;
7
6
import java .io .FileInputStream ;
8
7
import java .io .FileOutputStream ;
18
17
import java .util .zip .ZipFile ;
19
18
import java .util .zip .ZipOutputStream ;
20
19
21
- import static com .blankj .utilcode .utils .ConstUtils .* ;
20
+ import static com .blankj .utilcode .utils .ConstUtils .MB ;
22
21
23
22
/**
24
23
* <pre>
@@ -37,62 +36,152 @@ private ZipUtils() {
37
36
/**
38
37
* 批量压缩文件
39
38
*
40
- * @param resFileList 要压缩的文件列表
41
- * @param zipFile 生成的压缩文件
39
+ * @param resFiles 要压缩的文件列表
40
+ * @param zipFilePath 压缩文件路径
41
+ * @throws IOException IO错误时抛出
42
42
*/
43
- public static void zipFiles (Collection <File > resFileList , File zipFile )
43
+ public static void zipFiles (Collection <File > resFiles , String zipFilePath )
44
44
throws IOException {
45
- zipFiles (resFileList , zipFile , null );
45
+ zipFiles (resFiles , zipFilePath , null );
46
46
}
47
47
48
48
/**
49
49
* 批量压缩文件
50
50
*
51
- * @param resFileList 要压缩的文件列表
52
- * @param zipFile 生成的压缩文件
51
+ * @param resFiles 要压缩的文件列表
52
+ * @param zipFilePath 压缩文件路径
53
53
* @param comment 压缩文件的注释
54
+ * @throws IOException IO错误时抛出
54
55
*/
55
- public static void zipFiles (Collection <File > resFileList , File zipFile , String comment ) {
56
- try {
57
- for (File resFile : resFileList ) {
58
- zipFile (resFile , "" , zipFile , comment );
59
- }
60
- } catch (IOException e ) {
61
- e .printStackTrace ();
56
+ public static void zipFiles (Collection <File > resFiles , String zipFilePath , String comment )
57
+ throws IOException {
58
+ zipFiles (resFiles , FileUtils .getFileByPath (zipFilePath ), comment );
59
+ }
60
+
61
+ /**
62
+ * 批量压缩文件
63
+ *
64
+ * @param resFiles 要压缩的文件列表
65
+ * @param zipFile 生成的压缩文件
66
+ * @throws IOException IO错误时抛出
67
+ */
68
+ public static void zipFiles (Collection <File > resFiles , File zipFile )
69
+ throws IOException {
70
+ zipFiles (resFiles , zipFile , null );
71
+ }
72
+
73
+ /**
74
+ * 批量压缩文件
75
+ *
76
+ * @param resFiles 要压缩的文件列表
77
+ * @param zipFile 压缩文件
78
+ * @param comment 压缩文件的注释
79
+ * @throws IOException IO错误时抛出
80
+ */
81
+ public static void zipFiles (Collection <File > resFiles , File zipFile , String comment )
82
+ throws IOException {
83
+ if (resFiles == null || zipFile == null ) return ;
84
+ zipFiles (resFiles , new ZipOutputStream (new FileOutputStream (zipFile )), comment );
85
+ }
86
+
87
+ /**
88
+ * 批量压缩文件
89
+ *
90
+ * @param resFiles 要压缩的文件列表
91
+ * @param zos 压缩文件输出流
92
+ * @param comment 压缩文件的注释
93
+ * @throws IOException IO错误时抛出
94
+ */
95
+ public static void zipFiles (Collection <File > resFiles , ZipOutputStream zos , String comment )
96
+ throws IOException {
97
+ for (File resFile : resFiles ) {
98
+ zipFile (resFile , "" , zos , comment );
62
99
}
63
100
}
64
101
65
102
/**
66
103
* 压缩文件
67
104
*
68
- * @param resDirPath 需要压缩的文件
69
- * @param rootPath 相对于压缩文件的路径
70
- * @param zipFile 压缩的目的文件
71
- * @throws IOException 当压缩过程出错时抛出
105
+ * @param resFilePath 需要压缩的文件路径
106
+ * @param zipFilePath 压缩文件路径
107
+ * @throws IOException IO错误时抛出
108
+ */
109
+ public static void zipFile (String resFilePath , String zipFilePath )
110
+ throws IOException {
111
+ zipFile (resFilePath , zipFilePath , null );
112
+ }
113
+
114
+ /**
115
+ * 压缩文件
116
+ *
117
+ * @param resFilePath 需要压缩的文件路径
118
+ * @param zipFilePath 压缩文件路径
119
+ * @param comment 压缩文件的注释
120
+ * @throws IOException IO错误时抛出
121
+ */
122
+ public static void zipFile (String resFilePath , String zipFilePath , String comment )
123
+ throws IOException {
124
+ zipFile (FileUtils .getFileByPath (resFilePath ), FileUtils .getFileByPath (zipFilePath ), comment );
125
+ }
126
+
127
+ /**
128
+ * 压缩文件
129
+ *
130
+ * @param resFile 需要压缩的文件
131
+ * @param zipFile 压缩文件
132
+ * @throws IOException IO错误时抛出
133
+ */
134
+ public static void zipFile (File resFile , File zipFile )
135
+ throws IOException {
136
+ zipFile (resFile , zipFile , null );
137
+ }
138
+
139
+ /**
140
+ * 压缩文件
141
+ *
142
+ * @param resFile 需要压缩的文件
143
+ * @param zipFile 压缩文件
144
+ * @param comment 压缩文件的注释
145
+ * @throws IOException IO错误时抛出
146
+ */
147
+ public static void zipFile (File resFile , File zipFile , String comment )
148
+ throws IOException {
149
+ if (resFile == null || zipFile == null ) return ;
150
+ zipFile (resFile , "" , new ZipOutputStream (new FileOutputStream (zipFile )), comment );
151
+ }
152
+
153
+ /**
154
+ * 压缩文件
155
+ *
156
+ * @param resFile 需要压缩的文件
157
+ * @param rootPath 相对于压缩文件的路径
158
+ * @param zos 压缩文件输出流
159
+ * @param comment 压缩文件的注释
160
+ * @throws IOException IO错误时抛出
72
161
*/
73
- public static void zipFile (File resDirPath , String rootPath , File zipFile , String comment )
162
+ public static void zipFile (File resFile , String rootPath , ZipOutputStream zos , String comment )
74
163
throws IOException {
75
- rootPath = rootPath + (rootPath . trim (). length () == 0 ? "" : File .separator ) + resDirPath .getName ();
76
- if (resDirPath .isDirectory ()) {
77
- File [] fileList = resDirPath .listFiles ();
164
+ rootPath = rootPath + (StringUtils . isSpace ( rootPath ) ? "" : File .separator ) + resFile .getName ();
165
+ if (resFile .isDirectory ()) {
166
+ File [] fileList = resFile .listFiles ();
78
167
for (File file : fileList ) {
79
- zipFile (file , rootPath , file , comment );
168
+ zipFile (file , rootPath , zos , comment );
80
169
}
81
170
} else {
82
171
InputStream is = null ;
83
- ZipOutputStream zos = null ;
84
172
try {
85
- is = new BufferedInputStream (new FileInputStream (resDirPath ));
86
- zos = new ZipOutputStream (new BufferedOutputStream (new FileOutputStream (zipFile )));
173
+ is = new BufferedInputStream (new FileInputStream (resFile ));
87
174
zos .putNextEntry (new ZipEntry (rootPath ));
88
175
byte buffer [] = new byte [MB ];
89
176
int len ;
90
177
while ((len = is .read (buffer )) != -1 ) {
91
178
zos .write (buffer , 0 , len );
92
179
}
93
- if (comment != null ) zos .setComment (comment );
180
+ if (StringUtils . isEmpty ( comment ) ) zos .setComment (comment );
94
181
} finally {
95
- FileUtils .closeIO (is , zos );
182
+ FileUtils .closeIO (is );
183
+ zos .flush ();
184
+ zos .closeEntry ();
96
185
}
97
186
}
98
187
}
@@ -104,7 +193,7 @@ public static void zipFile(File resDirPath, String rootPath, File zipFile, Strin
104
193
* @param destDirPath 解压缩的目标目录
105
194
* @throws IOException IO错误时抛出
106
195
*/
107
- public static void upZipFile (String zipFilePath , String destDirPath )
196
+ public static void unzipFile (String zipFilePath , String destDirPath )
108
197
throws IOException {
109
198
if (!FileUtils .createOrExistsDir (destDirPath )) return ;
110
199
ZipFile zf = new ZipFile (zipFilePath );
@@ -142,7 +231,7 @@ public static void upZipFile(String zipFilePath, String destDirPath)
142
231
* @param targetFile 目标文件名
143
232
* @throws IOException IO错误时抛出
144
233
*/
145
- public static List <File > upZipSelectedFile (File zipFile , String destDirPath , String targetFile )
234
+ public static List <File > unzipSelectedFile (File zipFile , String destDirPath , String targetFile )
146
235
throws IOException {
147
236
if (!FileUtils .createOrExistsDir (destDirPath )) return null ;
148
237
List <File > fileList = new ArrayList <>();
0 commit comments