1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.archivers.examples;
20
21 import java.io.BufferedInputStream;
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.nio.channels.Channels;
27 import java.nio.channels.FileChannel;
28 import java.nio.channels.SeekableByteChannel;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.StandardOpenOption;
32 import java.util.Enumeration;
33 import java.util.Iterator;
34
35 import org.apache.commons.compress.archivers.ArchiveEntry;
36 import org.apache.commons.compress.archivers.ArchiveException;
37 import org.apache.commons.compress.archivers.ArchiveInputStream;
38 import org.apache.commons.compress.archivers.ArchiveStreamFactory;
39 import org.apache.commons.compress.archivers.sevenz.SevenZFile;
40 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
41 import org.apache.commons.compress.archivers.tar.TarFile;
42 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
43 import org.apache.commons.compress.archivers.zip.ZipFile;
44 import org.apache.commons.io.IOUtils;
45 import org.apache.commons.io.output.NullOutputStream;
46
47
48
49
50
51
52 public class Expander {
53
54 @FunctionalInterface
55 private interface ArchiveEntryBiConsumer<T extends ArchiveEntry> {
56 void accept(T entry, OutputStream out) throws IOException;
57 }
58
59 @FunctionalInterface
60 private interface ArchiveEntrySupplier<T extends ArchiveEntry> {
61 T get() throws IOException;
62 }
63
64
65
66
67 private <T extends ArchiveEntry> void expand(final ArchiveEntrySupplier<T> supplier, final ArchiveEntryBiConsumer<T> writer, final Path targetDirectory)
68 throws IOException {
69 final boolean nullTarget = targetDirectory == null;
70 final Path targetDirPath = nullTarget ? null : targetDirectory.normalize();
71 T nextEntry = supplier.get();
72 while (nextEntry != null) {
73 final Path targetPath = nullTarget ? null : nextEntry.resolveIn(targetDirPath);
74 if (nextEntry.isDirectory()) {
75 if (!nullTarget && !Files.isDirectory(targetPath) && Files.createDirectories(targetPath) == null) {
76 throw new IOException("Failed to create directory " + targetPath);
77 }
78 } else {
79 final Path parent = nullTarget ? null : targetPath.getParent();
80 if (!nullTarget && !Files.isDirectory(parent) && Files.createDirectories(parent) == null) {
81 throw new IOException("Failed to create directory " + parent);
82 }
83 if (nullTarget) {
84 writer.accept(nextEntry, NullOutputStream.INSTANCE);
85 } else {
86 try (OutputStream outputStream = Files.newOutputStream(targetPath)) {
87 writer.accept(nextEntry, outputStream);
88 }
89 }
90 }
91 nextEntry = supplier.get();
92 }
93 }
94
95
96
97
98
99
100
101
102 public void expand(final ArchiveInputStream<?> archive, final File targetDirectory) throws IOException {
103 expand(archive, toPath(targetDirectory));
104 }
105
106
107
108
109
110
111
112
113
114 public void expand(final ArchiveInputStream<?> archive, final Path targetDirectory) throws IOException {
115 expand(() -> {
116 ArchiveEntry next = archive.getNextEntry();
117 while (next != null && !archive.canReadEntryData(next)) {
118 next = archive.getNextEntry();
119 }
120 return next;
121 }, (entry, out) -> IOUtils.copy(archive, out), targetDirectory);
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136 public void expand(final File archive, final File targetDirectory) throws IOException, ArchiveException {
137 expand(archive.toPath(), toPath(targetDirectory));
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158 @Deprecated
159 public void expand(final InputStream archive, final File targetDirectory) throws IOException, ArchiveException {
160 expand(archive, targetDirectory, CloseableConsumer.NULL_CONSUMER);
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 public void expand(final InputStream archive, final File targetDirectory, final CloseableConsumer closeableConsumer) throws IOException, ArchiveException {
184 try (CloseableConsumerAdapter c = new CloseableConsumerAdapter(closeableConsumer)) {
185 expand(c.track(ArchiveStreamFactory.DEFAULT.createArchiveInputStream(archive)), targetDirectory);
186 }
187 }
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 public void expand(final Path archive, final Path targetDirectory) throws IOException, ArchiveException {
203 try (InputStream inputStream = new BufferedInputStream(Files.newInputStream(archive))) {
204 expand(ArchiveStreamFactory.detect(inputStream), archive, targetDirectory);
205 }
206 }
207
208
209
210
211
212
213
214
215 public void expand(final SevenZFile archive, final File targetDirectory) throws IOException {
216 expand(archive, toPath(targetDirectory));
217 }
218
219
220
221
222
223
224
225
226
227 public void expand(final SevenZFile archive, final Path targetDirectory) throws IOException {
228 expand(archive::getNextEntry, (entry, out) -> {
229 final byte[] buffer = new byte[8192];
230 int n;
231 while (-1 != (n = archive.read(buffer))) {
232 if (out != null) {
233 out.write(buffer, 0, n);
234 }
235 }
236 }, targetDirectory);
237 }
238
239
240
241
242
243
244
245
246
247
248 public void expand(final String format, final File archive, final File targetDirectory) throws IOException, ArchiveException {
249 expand(format, archive.toPath(), toPath(targetDirectory));
250 }
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267 @Deprecated
268 public void expand(final String format, final InputStream archive, final File targetDirectory) throws IOException, ArchiveException {
269 expand(format, archive, targetDirectory, CloseableConsumer.NULL_CONSUMER);
270 }
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289 public void expand(final String format, final InputStream archive, final File targetDirectory, final CloseableConsumer closeableConsumer)
290 throws IOException, ArchiveException {
291 expand(format, archive, toPath(targetDirectory), closeableConsumer);
292 }
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311 public void expand(final String format, final InputStream archive, final Path targetDirectory, final CloseableConsumer closeableConsumer)
312 throws IOException, ArchiveException {
313 try (CloseableConsumerAdapter c = new CloseableConsumerAdapter(closeableConsumer)) {
314 final ArchiveInputStream<?> archiveInputStream = ArchiveStreamFactory.DEFAULT.createArchiveInputStream(format, archive);
315 expand(c.track(archiveInputStream), targetDirectory);
316 }
317 }
318
319
320
321
322
323
324
325
326
327
328
329 public void expand(final String format, final Path archive, final Path targetDirectory) throws IOException, ArchiveException {
330 if (prefersSeekableByteChannel(format)) {
331 try (SeekableByteChannel channel = FileChannel.open(archive, StandardOpenOption.READ)) {
332 expand(format, channel, targetDirectory, CloseableConsumer.CLOSING_CONSUMER);
333 }
334 return;
335 }
336 try (InputStream inputStream = new BufferedInputStream(Files.newInputStream(archive))) {
337 expand(format, inputStream, targetDirectory, CloseableConsumer.CLOSING_CONSUMER);
338 }
339 }
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356 @Deprecated
357 public void expand(final String format, final SeekableByteChannel archive, final File targetDirectory) throws IOException, ArchiveException {
358 expand(format, archive, targetDirectory, CloseableConsumer.NULL_CONSUMER);
359 }
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378 public void expand(final String format, final SeekableByteChannel archive, final File targetDirectory, final CloseableConsumer closeableConsumer)
379 throws IOException, ArchiveException {
380 expand(format, archive, toPath(targetDirectory), closeableConsumer);
381 }
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400 public void expand(final String format, final SeekableByteChannel archive, final Path targetDirectory, final CloseableConsumer closeableConsumer)
401 throws IOException, ArchiveException {
402 try (CloseableConsumerAdapter c = new CloseableConsumerAdapter(closeableConsumer)) {
403 if (!prefersSeekableByteChannel(format)) {
404 expand(format, c.track(Channels.newInputStream(archive)), targetDirectory, CloseableConsumer.NULL_CONSUMER);
405 } else if (ArchiveStreamFactory.TAR.equalsIgnoreCase(format)) {
406 expand(c.track(new TarFile(archive)), targetDirectory);
407 } else if (ArchiveStreamFactory.ZIP.equalsIgnoreCase(format)) {
408 expand(c.track(ZipFile.builder().setSeekableByteChannel(archive).get()), targetDirectory);
409 } else if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format)) {
410 expand(c.track(SevenZFile.builder().setSeekableByteChannel(archive).get()), targetDirectory);
411 } else {
412
413 throw new ArchiveException("Don't know how to handle format " + format);
414 }
415 }
416 }
417
418
419
420
421
422
423
424
425
426 public void expand(final TarFile archive, final File targetDirectory) throws IOException {
427 expand(archive, toPath(targetDirectory));
428 }
429
430
431
432
433
434
435
436
437
438 public void expand(final TarFile archive, final Path targetDirectory) throws IOException {
439 final Iterator<TarArchiveEntry> entryIterator = archive.getEntries().iterator();
440 expand(() -> entryIterator.hasNext() ? entryIterator.next() : null, (entry, out) -> {
441 try (InputStream in = archive.getInputStream(entry)) {
442 IOUtils.copy(in, out);
443 }
444 }, targetDirectory);
445 }
446
447
448
449
450
451
452
453
454 public void expand(final ZipFile archive, final File targetDirectory) throws IOException {
455 expand(archive, toPath(targetDirectory));
456 }
457
458
459
460
461
462
463
464
465
466 public void expand(final ZipFile archive, final Path targetDirectory) throws IOException {
467 final Enumeration<ZipArchiveEntry> entries = archive.getEntries();
468 expand(() -> {
469 ZipArchiveEntry next = entries.hasMoreElements() ? entries.nextElement() : null;
470 while (next != null && !archive.canReadEntryData(next)) {
471 next = entries.hasMoreElements() ? entries.nextElement() : null;
472 }
473 return next;
474 }, (entry, out) -> {
475 try (InputStream in = archive.getInputStream(entry)) {
476 IOUtils.copy(in, out);
477 }
478 }, targetDirectory);
479 }
480
481 private boolean prefersSeekableByteChannel(final String format) {
482 return ArchiveStreamFactory.TAR.equalsIgnoreCase(format) || ArchiveStreamFactory.ZIP.equalsIgnoreCase(format)
483 || ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format);
484 }
485
486 private Path toPath(final File targetDirectory) {
487 return targetDirectory != null ? targetDirectory.toPath() : null;
488 }
489
490 }