1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.compress.archivers;
19
20 import java.io.BufferedInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26 import java.util.Enumeration;
27 import java.util.Locale;
28 import java.util.Objects;
29
30 import org.apache.commons.compress.archivers.sevenz.SevenZFile;
31 import org.apache.commons.compress.archivers.tar.TarFile;
32 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
33 import org.apache.commons.compress.archivers.zip.ZipFile;
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public final class Lister {
48
49 private static final ArchiveStreamFactory FACTORY = ArchiveStreamFactory.DEFAULT;
50
51 private static <T extends ArchiveInputStream<? extends E>, E extends ArchiveEntry> T createArchiveInputStream(final String[] args,
52 final InputStream inputStream) throws ArchiveException {
53 if (args.length > 1) {
54 return FACTORY.createArchiveInputStream(args[1], inputStream);
55 }
56 return FACTORY.createArchiveInputStream(inputStream);
57 }
58
59 private static String detectFormat(final Path file) throws ArchiveException, IOException {
60 try (InputStream inputStream = new BufferedInputStream(Files.newInputStream(file))) {
61 return ArchiveStreamFactory.detect(inputStream);
62 }
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 public static void main(final String... args) throws ArchiveException, IOException {
79 if (args == null || args.length == 0) {
80 usage();
81 return;
82 }
83 new Lister(false, args).go();
84 }
85
86 private static void usage() {
87 System.err.println("Parameters: archive-name [archive-type]\n");
88 System.err.println("The magic archive-type 'zipfile' prefers ZipFile over ZipArchiveInputStream");
89 System.err.println("The magic archive-type 'tarfile' prefers TarFile over TarArchiveInputStream");
90 }
91
92 private final boolean quiet;
93
94 private final String[] args;
95
96
97
98
99
100
101 @Deprecated
102 public Lister() {
103 this(false, "");
104 }
105
106 Lister(final boolean quiet, final String... args) {
107 this.quiet = quiet;
108 this.args = args.clone();
109 Objects.requireNonNull(args[0], "args[0]");
110 }
111
112 void go() throws ArchiveException, IOException {
113 list(Paths.get(args[0]), args);
114 }
115
116 private void list(final Path file, final String... args) throws ArchiveException, IOException {
117 println("Analyzing " + file);
118 if (!Files.isRegularFile(file)) {
119 System.err.println(file + " doesn't exist or is a directory");
120 }
121 final String format = (args.length > 1 ? args[1] : detectFormat(file)).toLowerCase(Locale.ROOT);
122 println("Detected format " + format);
123 switch (format) {
124 case ArchiveStreamFactory.SEVEN_Z:
125 list7z(file);
126 break;
127 case ArchiveStreamFactory.ZIP:
128 listZipUsingZipFile(file);
129 break;
130 case ArchiveStreamFactory.TAR:
131 listZipUsingTarFile(file);
132 break;
133 default:
134 listStream(file, args);
135 }
136 }
137
138 private void list7z(final Path file) throws IOException {
139 try (SevenZFile sevenZFile = SevenZFile.builder().setPath(file).get()) {
140 println("Created " + sevenZFile);
141 ArchiveEntry entry;
142 while ((entry = sevenZFile.getNextEntry()) != null) {
143 println(entry.getName() == null ? sevenZFile.getDefaultName() + " (entry name was null)" : entry.getName());
144 }
145 }
146 }
147
148 private void listStream(final Path file, final String[] args) throws ArchiveException, IOException {
149 try (InputStream inputStream = new BufferedInputStream(Files.newInputStream(file));
150 ArchiveInputStream<?> archiveInputStream = createArchiveInputStream(args, inputStream)) {
151 println("Created " + archiveInputStream.toString());
152 ArchiveEntry entry;
153 while ((entry = archiveInputStream.getNextEntry()) != null) {
154 println(entry);
155 }
156 }
157 }
158
159 private void listZipUsingTarFile(final Path file) throws IOException {
160 try (TarFile tarFile = new TarFile(file)) {
161 println("Created " + tarFile);
162 tarFile.getEntries().forEach(this::println);
163 }
164 }
165
166 private void listZipUsingZipFile(final Path file) throws IOException {
167 try (ZipFile zipFile = ZipFile.builder().setPath(file).get()) {
168 println("Created " + zipFile);
169 for (final Enumeration<ZipArchiveEntry> en = zipFile.getEntries(); en.hasMoreElements();) {
170 println(en.nextElement());
171 }
172 }
173 }
174
175 private void println(final ArchiveEntry entry) {
176 println(entry.getName());
177 }
178
179 private void println(final String line) {
180 if (!quiet) {
181 System.out.println(line);
182 }
183 }
184
185 }