1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.opencastproject.util;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.util.Vector;
30 import java.util.zip.Deflater;
31
32 import de.schlichtherle.io.ArchiveDetector;
33 import de.schlichtherle.io.ArchiveException;
34 import de.schlichtherle.io.ArchiveWarningException;
35 import de.schlichtherle.io.DefaultArchiveDetector;
36 import de.schlichtherle.io.File;
37 import de.schlichtherle.io.archive.zip.ZipDriver;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public final class ZipUtil {
60
61 private static final Logger logger = LoggerFactory.getLogger(ZipUtil.class);
62
63 public static final int BEST_SPEED = Deflater.BEST_SPEED;
64 public static final int BEST_COMPRESSION = Deflater.BEST_COMPRESSION;
65 public static final int DEFAULT_COMPRESSION = Deflater.DEFAULT_COMPRESSION;
66 public static final int NO_COMPRESSION = Deflater.NO_COMPRESSION;
67
68
69 private ZipUtil() {
70 }
71
72
73
74
75
76
77
78
79
80 private static void umount(File zipFile) throws IOException {
81 try {
82 File.umount(zipFile);
83 } catch (ArchiveWarningException awe) {
84 logger.warn("Umounting {} threw the following warning: {}", zipFile.getCanonicalPath(), awe.getMessage());
85 } catch (ArchiveException ae) {
86 logger.error("Unable to umount zip file: {}", zipFile.getCanonicalPath());
87 throw new IOException("Unable to umount zip file: " + zipFile.getCanonicalPath(), ae);
88 }
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 public static java.io.File zip(java.io.File[] sourceFiles, java.io.File destination, boolean recursive, int level)
111 throws IOException {
112
113 if (sourceFiles == null) {
114 logger.error("The array with files to zip cannot be null");
115 throw new IllegalArgumentException("The array with files to zip cannot be null");
116 }
117
118 if (sourceFiles.length <= 0) {
119 logger.error("The array with files to zip cannot be empty");
120 throw new IllegalArgumentException("The array with files to zip cannot be empty");
121 }
122
123 if (destination == null) {
124 logger.error("The destination file cannot be null");
125 throw new IllegalArgumentException("The destination file cannot be null");
126 }
127
128 if (destination.exists()) {
129 logger.error("The destination file {} already exists", destination.getCanonicalPath());
130 throw new IllegalArgumentException("The destination file already exists");
131 }
132
133 if (level < -1) {
134 logger.warn("Compression level cannot be less than 0 (or -1 for default)");
135 logger.warn("Reverting to default...");
136 level = -1;
137 } else if (level > 9) {
138 logger.warn("Compression level cannot be greater than 9");
139 logger.warn("Reverting to default...");
140 level = -1;
141 }
142
143
144 ZipDriver zd = new ZipDriver(level);
145 ArchiveDetector ad = new DefaultArchiveDetector(ArchiveDetector.NULL, "zip", zd);
146 File zipFile;
147 try {
148 zipFile = new File(destination.getCanonicalFile(), ad);
149 } catch (IOException ioe) {
150 logger.error("Unable to create the zip file: {}", destination.getAbsolutePath());
151 throw new IOException("Unable to create the zip file: {}" + destination.getAbsolutePath(), ioe);
152 }
153
154 try {
155 if (!zipFile.isArchive()) {
156 logger.error("The destination file does not represent a valid zip archive (.zip extension is required)");
157 zipFile.deleteAll();
158 throw new IllegalArgumentException(
159 "The destination file does not represent a valid zip archive (.zip extension is required)");
160 }
161
162 if (!zipFile.mkdirs()) {
163 throw new IOException("Couldn't create the destination file");
164 }
165
166 for (java.io.File f : sourceFiles) {
167
168 if (f == null) {
169 logger.error("Null inputfile in array");
170 zipFile.deleteAll();
171 throw new IllegalArgumentException("Null inputfile in array");
172 }
173
174 logger.debug("Attempting to zip file {}...", f.getAbsolutePath());
175
176
177
178
179
180
181 boolean success = false;
182 if (f.exists()) {
183 if (!f.isDirectory() || recursive) {
184 success = new File(zipFile, f.getName()).copyAllFrom(f);
185 if (success) {
186 logger.debug("File {} zipped successfuly", f.getAbsolutePath());
187 }
188 else {
189 logger.error("File {} not zipped", f.getAbsolutePath());
190 zipFile.deleteAll();
191 throw new IOException("Failed to zip one of the input files: " + f.getAbsolutePath());
192 }
193 }
194 } else {
195 logger.error("Input file {} doesn't exist", f.getAbsolutePath());
196 zipFile.deleteAll();
197 throw new FileNotFoundException("One of the input files does not exist: " + f.getAbsolutePath());
198 }
199 }
200 } catch (IOException e) {
201 throw e;
202 } finally {
203 umount(zipFile);
204 }
205
206 return destination;
207 }
208
209
210
211
212
213
214
215
216
217
218
219
220
221 public static void unzip(java.io.File zipFile, java.io.File destination) throws IOException {
222
223 boolean success;
224
225 if (zipFile == null) {
226 logger.error("The zip file cannot be null");
227 throw new IllegalArgumentException("The zip file must be set");
228 }
229
230 if (!zipFile.exists()) {
231 logger.error("The zip file does not exist: {}", zipFile.getCanonicalPath());
232 throw new FileNotFoundException("The zip file does not exist: " + zipFile.getCanonicalPath());
233 }
234
235 if (destination == null) {
236 logger.error("The destination file cannot be null");
237 throw new IllegalArgumentException("Destination file cannot be null");
238 }
239
240
241
242
243 File f;
244 try {
245 f = new File(zipFile.getCanonicalFile());
246 } catch (IOException ioe) {
247 logger.error("Unable to create the zip file: {}", destination.getAbsolutePath());
248 throw new IOException("Unable to create the zip file: {}" + destination.getAbsolutePath(), ioe);
249 }
250
251 try {
252 if (f.isArchive() && f.isDirectory()) {
253 if (destination.exists()) {
254 if (!destination.isDirectory()) {
255 logger.error("Destination file must be a directory");
256 throw new IllegalArgumentException("Destination file must be a directory");
257 }
258 }
259
260 try {
261 destination.mkdirs();
262 } catch (SecurityException e) {
263 logger.error("Cannot create destination directory: {}", e.getMessage());
264 throw new IOException("Cannot create destination directory", e);
265 }
266
267 success = f.copyAllTo(destination);
268
269 if (success) {
270 logger.debug("File {} unzipped successfully", zipFile.getCanonicalPath());
271 } else {
272 logger.warn("File {} was not correctly unzipped", zipFile.getCanonicalPath());
273 throw new IOException("File " + zipFile.getCanonicalPath() + " was not correctly unzipped");
274 }
275 } else {
276 logger.error("The input file is not a valid zip file");
277 throw new IllegalArgumentException("The input file is not a valid zip file");
278 }
279 } catch (IOException e) {
280 throw (e);
281 } finally {
282 umount(f);
283 }
284
285 }
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306 public static java.io.File zip(String[] sourceFiles, String destination, boolean recursive, int level)
307 throws IOException {
308
309 if (sourceFiles == null) {
310 logger.error("The input String array cannot be null");
311 throw new IllegalArgumentException("The input String array cannot be null");
312 }
313
314 if (destination == null) {
315 logger.error("Destination file cannot be null");
316 throw new IllegalArgumentException("Destination file cannot be null");
317 }
318
319 if ("".equals(destination)) {
320 logger.error("Destination file name must be set");
321 throw new IllegalArgumentException("Destination file name must be set");
322 }
323
324 Vector<java.io.File> files = new Vector<java.io.File>();
325 for (String name : sourceFiles) {
326 if (name == null) {
327 logger.error("One of the input file names is null");
328 throw new IllegalArgumentException("One of the input file names is null");
329 } else if ("".equals(name)) {
330 logger.error("One of the input file names is blank");
331 throw new IllegalArgumentException("One of the input file names is blank");
332 }
333 files.add(new java.io.File(name));
334 }
335
336 return zip(files.toArray(new java.io.File[files.size()]), new java.io.File(destination), recursive, level);
337
338 }
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355 public static java.io.File zip(String[] sourceFiles, java.io.File destination, boolean recursive, int level)
356 throws IOException {
357
358 if (sourceFiles == null) {
359 logger.error("The input String array cannot be null");
360 throw new IllegalArgumentException("The input String array cannot be null");
361 }
362
363 Vector<java.io.File> files = new Vector<java.io.File>();
364 for (String name : sourceFiles) {
365 if (name == null) {
366 logger.error("One of the input file names is null");
367 throw new IllegalArgumentException("One of the input file names is null");
368 } else if ("".equals(name)) {
369 logger.error("One of the input file names is blank");
370 throw new IllegalArgumentException("One of the input file names is blank");
371 }
372 files.add(new java.io.File(name));
373 }
374
375 return zip(files.toArray(new java.io.File[files.size()]), destination, recursive, level);
376
377 }
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394 public static java.io.File zip(java.io.File[] sourceFiles, String destination, boolean recursive, int level)
395 throws IOException {
396
397 if (destination == null) {
398 logger.error("Destination file cannot be null");
399 throw new IllegalArgumentException("Destination file cannot be null");
400 }
401
402 if ("".equals(destination)) {
403 logger.error("Destination file name must be set");
404 throw new IllegalArgumentException("Destination file name must be set");
405 }
406
407 return zip(sourceFiles, new java.io.File(destination), recursive, level);
408
409 }
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424 public static java.io.File zip(java.io.File[] sourceFiles, java.io.File destination, int level) throws IOException {
425 return zip(sourceFiles, destination, false, level);
426 }
427
428
429
430
431
432
433
434
435
436
437
438
439
440 public static void unzip(String zipFile, String destination) throws IOException {
441
442 if (zipFile == null) {
443 logger.error("Input filename cannot be null");
444 throw new IllegalArgumentException("Input filename cannot be null");
445 }
446
447 if ("".equals(zipFile)) {
448 logger.error("Input filename cannot be empty");
449 throw new IllegalArgumentException("Input filename cannot be empty");
450 }
451
452 if (destination == null) {
453 logger.error("Output filename cannot be null");
454 throw new IllegalArgumentException("Output filename cannot be null");
455 }
456
457 if ("".equals(destination)) {
458 logger.error("Output filename cannot be empty");
459 throw new IllegalArgumentException("Output filename cannot be empty");
460 }
461
462 unzip(new java.io.File(zipFile), new java.io.File(destination));
463
464 }
465
466
467
468
469
470
471
472
473
474
475
476
477 public static void unzip(java.io.File zipFile, String destination) throws IOException {
478
479 if (destination == null) {
480 logger.error("Output filename cannot be null");
481 throw new IllegalArgumentException("Output filename cannot be null");
482 }
483
484 if ("".equals(destination)) {
485 logger.error("Output filename cannot be empty");
486 throw new IllegalArgumentException("Output filename cannot be empty");
487 }
488
489 unzip(zipFile, new java.io.File(destination));
490
491 }
492
493
494
495
496
497
498
499
500
501
502
503
504 public static void unzip(String zipFile, java.io.File destination) throws IOException {
505
506 if (zipFile == null) {
507 logger.error("Input filename cannot be null");
508 throw new IllegalArgumentException("Input filename cannot be null");
509 }
510
511 if ("".equals(zipFile)) {
512 logger.error("Input filename cannot be empty");
513 throw new IllegalArgumentException("Input filename cannot be empty");
514 }
515
516 unzip(new java.io.File(zipFile), destination);
517
518 }
519
520 }