-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path105.java
More file actions
557 lines (492 loc) · 17.9 KB
/
Copy path105.java
File metadata and controls
557 lines (492 loc) · 17.9 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
package org.dstadler.commons.zip;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.NoSuchFileException;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.dstadler.commons.io.DeleteOnCloseInputStream;
/**
* Various utilities related to handling Zip-files.
*/
public class ZipUtils {
private final static Logger logger = Logger.getLogger(ZipUtils.class.getName());
private static final char ZIP_DELIMITER = '!';
/**
* Extensions for known ZIP files, need to be in lowercase to match below!
*/
private static final String[] ZIP_EXTENSIONS = {
// normal Zip
".zip",
// Java archives
".jar", ".war", ".ear",
// Axis2 Services archives
".aar",
// Jenkins/Hudson plugin bundle
".hpi",
// Microsoft Office files
".xlsx", ".docx", ".pptx"
};
/**
* Determines if the file has an extension known to be a ZIP file,
* currently this includes .zip, .jar, .war, .ear, .aar
*
* @param fileName The name of the file to check.
* @return True if the filename is of an extension that is a known zip file, false otherwise.
*/
public static boolean isZip(String fileName) {
if (fileName == null) {
return false;
}
String tl = fileName.toLowerCase();
for (String element : ZIP_EXTENSIONS) {
if (tl.endsWith(element)) {
return true;
}
}
return false;
}
/**
* Checks if the string denotes a file inside a ZIP file using the notation
* used for getZipContentsRecursive().
*
* @param name The name to check
* @return true if the name denotes a file inside a known zip file format.
*/
public static boolean isFileInZip(String name) {
if (name == null) {
return false;
}
for (String element : ZIP_EXTENSIONS) {
if (name.toLowerCase().contains(element + ZIP_DELIMITER)) {
return true;
}
}
return false;
}
/**
* Looks in the ZIP file available via zipInput for files matching the provided file-filter,
* recursing into sub-ZIP files.
*
* @param zipName Name of the file to read, mainly used for building the resulting pointer into the zip-file
* @param zipInput An InputStream which is positioned at the beginning of the zip-file contents
* @param searchFilter A {@link FileFilter} which determines if files in the zip-file are matched
* @param results A existing list where found matches are added to.
*
* @throws IOException
* If the ZIP file cannot be read, e.g. if it is corrupted.
*/
public static void findZip(String zipName, InputStream zipInput, FileFilter searchFilter, List<String> results)
throws IOException {
ZipInputStream zin = new ZipInputStream(zipInput);
while (true) {
final ZipEntry en;
try {
en = zin.getNextEntry();
} catch (IOException | IllegalArgumentException e) {
throw new IOException("While handling file " + zipName, e);
}
if(en == null) {
break;
}
if (searchFilter.accept(new File(en.getName()))) {
results.add(zipName + ZIP_DELIMITER + en);
}
if (ZipUtils.isZip(en.getName())) {
findZip(zipName + ZIP_DELIMITER + en, zin, searchFilter, results);
}
}
}
/**
* Get a stream of the noted file which potentially resides inside ZIP files. An exclamation mark '!'
* denotes a zip-entry. ZIP files can be nested inside one another.
*
* e.g.
*
* c:\temp\test.zip!sample.zip!my.zip!somefile.txt
*
* If there is no exclamation mark contained in the file-parameter, an input stream to this file is
* returned directly.
*
* means that there is a zip file c:\temp\test.zip which contains a file "sample.zip", which itself
* contains a file "my.zip" which finally contains a file "somefile.txt"
*
* @param file The name of the file to read, files inside zip files are denoted with '!'.
*
* @return A stream that points to the file inside the ZIP file.
*
* @throws IOException If the file cannot be found or an error occurs while opening the file.
*/
@SuppressWarnings("resource")
public static InputStream getZipContentsRecursive(final String file) throws IOException {
// return local file directly
int pos = file.indexOf('!');
if (pos == -1) {
if (!new File(file).exists()) {
throw new IOException("File " + file + " does not exist");
}
try {
return new FileInputStream(file);
} catch (IOException e) {
// filter out locked errors
if (e.getMessage().contains("because another process has locked")) {
logger.warning("Could not read file: " + file + " because it is locked.");
return new ByteArrayInputStream(new byte[] {});
}
throw e;
}
}
String zip = file.substring(0, pos);
String subfile = file.substring(pos + 1);
if (logger.isLoggable(Level.FINE)) {
logger.fine("Trying to read zipfile: " + zip + " subfile: " + subfile);
}
// open original zip
if (!new File(zip).exists() || !new File(zip).isFile() || !new File(zip).canRead() || new File(zip).length() == 0) {
throw new IOException("ZIP file: " + zip + " does not exist or is empty or not a readable file.");
}
ZipFile zipfile = new ZipFile(zip);
// is the target file in yet another ZIP file?
pos = subfile.indexOf('!');
if (pos != -1) {
// find out first ZIP file and remainder
String remainder = subfile.substring(pos + 1);
File subzipfile = File.createTempFile("ZipUtils", ".zip");
try {
readToTemporaryFile(pos, zip, subfile, zipfile, subzipfile);
// start another recursion with the temporary file and the remainder
return new DeleteOnCloseInputStream(
new ZipFileCloseInputStream(getZipContentsRecursive(subzipfile.getAbsolutePath() + ZIP_DELIMITER + remainder), zipfile),
subzipfile);
} catch (IOException e) {
// need to close the zipfile here as we do not put it into a ZipFileCloseInputStream
zipfile.close();
throw e;
} finally {
if (!subzipfile.delete()) {
logger.warning("Could not delete file " + subzipfile);
}
}
}
ZipEntry entry = zipfile.getEntry(subfile);
return new ZipFileCloseInputStream(zipfile.getInputStream(entry), zipfile);
}
private static void readToTemporaryFile(int pos, String zip, String subfile, ZipFile zipfile, File subzipfile)
throws IOException {
// open the inner-zip
ZipEntry entry = openInnerZip(pos, zip, subfile, zipfile);
// read the zipfile into a temporary file
try (InputStream zipstr = zipfile.getInputStream(entry)) {
FileUtils.copyInputStreamToFile(zipstr, subzipfile);
}
}
private static ZipEntry openInnerZip(int pos, String zip, String subfile, ZipFile zipfile) throws IOException {
String zipInner = subfile.substring(0, pos);
ZipEntry entry = zipfile.getEntry(zipInner);
if (entry == null) {
throw new IOException("Could not read inner ZIP file: '" + zipInner + "' from ZIP file '" + zip + "'");
}
return entry;
}
/**
* Get the text-contents of the noted file. An exclamation mark '!' denotes a zip-entry. ZIP files can
* be nested inside one another.
*
* e.g.
*
* c:\temp\test.zip!sample.zip!my.zip!somefile.txt
*
* If there is no exclamation mark contained in the file-parameter, an input stream to this file is
* returned directly.
*
* means that there is a zip file c:\temp\test.zip which contains a file "sample.zip", which itself
* contains a file "my.zip" which finally contains a file "somefile.txt"
*
* @param file The name of the file to read, files inside zip files are denoted with '!'.
*
* @return The text-contents of the file
*
* @throws IOException If the file cannot be found or an error occurs while opening the file.
*/
public static String getZipStringContentsRecursive(final String file) throws IOException {
// return local file directly
int pos = file.indexOf('!');
if (pos == -1) {
if (!new File(file).exists()) {
throw new IOException("File " + file + " does not exist");
}
try {
try (InputStream str = new FileInputStream(file)) {
if (str.available() > 0) {
return IOUtils.toString(str, "UTF-8");
}
return "";
}
} catch (IOException e) {
// filter out locked errors
if (e.getMessage().contains("because another process has locked")) {
logger.warning("Could not read file: " + file + " because it is locked.");
return "";
}
throw e;
}
}
String zip = file.substring(0, pos);
String subfile = file.substring(pos + 1);
if (logger.isLoggable(Level.FINE)) {
logger.fine("Trying to read zipfile: " + zip + " subfile: " + subfile);
}
// open original zip
if (!new File(zip).exists() || !new File(zip).isFile() || !new File(zip).canRead() || new File(zip).length() == 0) {
throw new IOException("ZIP file: " + zip + " does not exist or is empty or not a readable file.");
}
try (ZipFile zipfile = new ZipFile(zip)) {
// is the target file in yet another ZIP file?
pos = subfile.indexOf('!');
if (pos != -1) {
// find out first ZIP file and remainder
String remainder = subfile.substring(pos + 1);
File subzipfile = File.createTempFile("SearchZip", ".zip");
try {
readToTemporaryFile(pos, zip, subfile, zipfile, subzipfile);
// start another recursion with the temporary file and the remainder
return getZipStringContentsRecursive(subzipfile.getAbsolutePath() + ZIP_DELIMITER + remainder);
} finally {
if (!subzipfile.delete()) {
logger.warning("Could not delete file " + subzipfile);
}
}
}
ZipEntry entry = zipfile.getEntry(subfile);
try (InputStream str = zipfile.getInputStream(entry)) {
if (str.available() > 0) {
return IOUtils.toString(str, "UTF-8");
}
return "";
}
}
}
/**
* Extracts all files in the specified ZIP file and stores them in the
* denoted directory. The directory needs to exist before running this method.
*
* Note: nested ZIP files are not extracted here.
*
* @param zip The zip-file to process
* @param toDir Target directory, should already exist.
*
* @throws IOException Thrown if files can not be read or any other error occurs while handling the Zip-files
*/
public static void extractZip(File zip, File toDir) throws IOException{
if(!toDir.exists()) {
throw new IOException("Directory '" + toDir + "' does not exist.");
}
try (ZipFile zipFile = new ZipFile(zip)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
File target = new File(toDir, entry.getName());
if (entry.isDirectory()) {
// Assume directories are stored parents first then children.
//logger.info("Extracting directory: " + entry.getName());
// This is not robust, just for demonstration purposes.
if(!target.mkdirs()) {
logger.warning("Could not create directory " + target);
}
continue;
}
// zips can contain nested files in sub-dirs without separate entries for the directories
if(!target.getParentFile().exists() && !target.getParentFile().mkdirs()) {
logger.warning("Could not create directory " + target.getParentFile());
}
//logger.info("Extracting file: " + entry.getName());
try (InputStream inputStream = zipFile.getInputStream(entry)) {
try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(target))) {
IOUtils.copy(inputStream, outputStream);
}
}
}
} catch (FileNotFoundException | NoSuchFileException e) {
throw e;
} catch (IOException e) {
throw new IOException("While extracting file " + zip + " to " + toDir, e);
}
}
/**
* Extracts all files in the ZIP file passed as InputStream and stores them in the
* denoted directory. The directory needs to exist before running this method.
*
* Note: nested ZIP files are not extracted here.
*
* @param zip An {@link InputStream} to read zipped files from
* @param toDir Target directory, should already exist.
*
* @throws IOException Thrown if files can not be read or any other error occurs while handling the Zip-files
*/
public static void extractZip(InputStream zip, final File toDir) throws IOException{
if(!toDir.exists()) {
throw new IOException("Directory '" + toDir + "' does not exist.");
}
// Use the ZipFileVisitor to walk all the entries in the Zip-Stream and create
// directories and files accordingly
new ZipFileVisitor() {
@Override
public void visit(ZipEntry entry, InputStream data) throws IOException {
File target = new File(toDir, entry.getName());
if (entry.isDirectory()) {
// Assume directories are stored parents first then children.
//logger.info("Extracting directory: " + entry.getName() + " to " + target);
// This is not robust, just for demonstration purposes.
if(!target.mkdirs()) {
logger.warning("Could not create directory " + target);
}
return;
}
// zips can contain nested files in sub-dirs without separate entries for the directories
if(!target.getParentFile().exists() && !target.getParentFile().mkdirs()) {
logger.warning("Could not create directory " + target.getParentFile());
}
// it seems we cannot use IOUtils/FileUtils to copy as they close the stream
int size;
byte[] buffer = new byte[2048];
try (OutputStream fout = new BufferedOutputStream(new FileOutputStream(target), buffer.length)) {
while ((size = data.read(buffer, 0, buffer.length)) != -1) {
fout.write(buffer, 0, size);
}
}
}
}.walk(zip);
}
/**
* Replace the file denoted by the zipFile with the provided data. The zipFile specifies
* both the zip and the file inside the zip using '!' as separator.
*
* @param zipFile The zip-file to process
* @param data The string-data to replace
* @param encoding The encoding that should be used when writing the string data to the file
* @throws IOException Thrown if files can not be read or any other error occurs while handling the Zip-files
*/
public static void replaceInZip(String zipFile, String data, String encoding) throws IOException {
if(!isFileInZip(zipFile)) {
throw new IOException("Parameter should specify a file inside a ZIP file, but had: " + zipFile);
}
File zip = new File(zipFile.substring(0, zipFile.indexOf(ZIP_DELIMITER)));
String zipOut = zipFile.substring(zipFile.indexOf(ZIP_DELIMITER)+1);
logger.info("Updating containing Zip " + zip + " to " + zipOut);
// replace in zip
ZipUtils.replaceInZip(zip, zipOut, data, encoding);
}
/**
* Replaces the specified file in the provided ZIP file with the
* provided content.
*
* @param zip The zip-file to process
* @param file The file to look for
* @param data The string-data to replace
* @param encoding The encoding that should be used when writing the string data to the file
* @throws IOException Thrown if files can not be read or any other error occurs while handling the Zip-files
*/
public static void replaceInZip(File zip, String file, String data, String encoding) throws IOException {
// open the output side
File zipOutFile = File.createTempFile("ZipReplace", ".zip");
try {
FileOutputStream fos = new FileOutputStream(zipOutFile);
try (ZipOutputStream zos = new ZipOutputStream(fos)) {
// open the input side
try (ZipFile zipFile = new ZipFile(zip)) {
boolean found = false;
// walk all entries and copy them into the new file
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
try {
if (entry.getName().equals(file)) {
zos.putNextEntry(new ZipEntry(entry.getName()));
IOUtils.write(data, zos, encoding);
found = true;
} else {
zos.putNextEntry(entry);
IOUtils.copy(zipFile.getInputStream(entry), zos);
}
} finally {
zos.closeEntry();
}
}
if(!found) {
zos.putNextEntry(new ZipEntry(file));
try {
IOUtils.write(data, zos, "UTF-8");
} finally {
zos.closeEntry();
}
}
}
}
// copy over the data
FileUtils.copyFile(zipOutFile, zip);
} finally {
if(!zipOutFile.delete()) {
//noinspection ThrowFromFinallyBlock
throw new IOException("Error deleting file: " + zipOutFile);
}
}
}
/**
* A simple walker which will visit all Zip-entries in the given InputStream.
* The passed stream can be a normal InputStream, the Visitor will enclose it
* in a ZipInputStream itself.
*
* The stream will be closed after all entries are visited.
*
new ZipUtils.ZipFileVisitor() {
{@literal @}Override
public void visit(ZipEntry entry, InputStream data) throws IOException {
// process file data
}
}.walk(new ByteArrayInputStream(zip));
*
*/
public static abstract class ZipFileVisitor {
public void walk(InputStream zipFile) throws IOException {
try (ZipInputStream stream = new ZipInputStream(zipFile)) {
// while there are entries I process them
while (true) {
ZipEntry entry = stream.getNextEntry();
if(entry == null) {
break;
}
try {
visit(entry, stream);
} finally {
stream.closeEntry();
}
}
}
}
/**
* This method is called for each entry in the zip-file, this includes
* directories, use ZipEntry.isDirectory() to check which type of entry
* you see.
*
* @param entry The current {@link ZipEntry}
* @param data The InputStream which can be used to read the entry-data.
* @throws IOException If extracting the data fails.
*/
public abstract void visit(ZipEntry entry, InputStream data) throws IOException;
}
}