View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
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   * Simple command line application that lists the contents of an archive.
37   *
38   * <p>
39   * The name of the archive must be given as a command line argument.
40   * </p>
41   * <p>
42   * The optional second argument defines the archive type, in case the format is not recognized.
43   * </p>
44   *
45   * @since 1.1
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       * Runs this class from the command line.
67       * <p>
68       * The name of the archive must be given as a command line argument.
69       * </p>
70       * <p>
71       * The optional second argument defines the archive type, in case the format is not recognized.
72       * </p>
73       *
74       * @param args name of the archive and optional argument archive type.
75       * @throws ArchiveException Archiver related Exception.
76       * @throws IOException      an I/O exception.
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       * Constructs a new instance.
98       *
99       * @deprecated No replacement.
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 }