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.tar;
19  
20  import java.io.BufferedInputStream;
21  import java.io.File;
22  import java.io.InputStream;
23  import java.nio.file.Files;
24  
25  /**
26   * Simple command line application that lists the contents of a tar archive.
27   *
28   * <p>
29   * The name of the archive must be given as a command line argument.
30   * </p>
31   * <p>
32   * The optional second argument specifies the encoding to assume for file names.
33   * </p>
34   *
35   * @since 1.11
36   */
37  public final class TarLister {
38  
39      private static void log(final TarArchiveEntry ae) {
40          final StringBuilder sb = new StringBuilder(Integer.toOctalString(ae.getMode())).append(" ");
41          String name = ae.getUserName();
42          if (name != null && !name.isEmpty()) {
43              sb.append(name);
44          } else {
45              sb.append(ae.getLongUserId());
46          }
47          sb.append("/");
48          name = ae.getGroupName();
49          if (name != null && !name.isEmpty()) {
50              sb.append(name);
51          } else {
52              sb.append(ae.getLongGroupId());
53          }
54          sb.append(" ");
55          if (ae.isSparse()) {
56              sb.append(ae.getRealSize());
57          } else if (ae.isCharacterDevice() || ae.isBlockDevice()) {
58              sb.append(ae.getDevMajor()).append(",").append(ae.getDevMinor());
59          } else {
60              sb.append(ae.getSize());
61          }
62          sb.append(" ").append(ae.getLastModifiedDate()).append(" ");
63          sb.append(ae.getName());
64          if (ae.isSymbolicLink() || ae.isLink()) {
65              if (ae.isSymbolicLink()) {
66                  sb.append(" -> ");
67              } else {
68                  sb.append(" link to ");
69              }
70              sb.append(ae.getLinkName());
71          }
72          if (ae.isSparse()) {
73              sb.append(" (sparse)");
74          }
75          System.out.println(sb);
76      }
77  
78      public static void main(final String[] args) throws Exception {
79          if (args.length == 0) {
80              usage();
81              return;
82          }
83          System.out.println("Analysing " + args[0]);
84          final File f = new File(args[0]);
85          if (!f.isFile()) {
86              System.err.println(f + " doesn't exist or is a directory");
87          }
88          try (InputStream fis = new BufferedInputStream(Files.newInputStream(f.toPath()));
89                  TarArchiveInputStream ais = args.length > 1 ? new TarArchiveInputStream(fis, args[1]) : new TarArchiveInputStream(fis)) {
90              System.out.println("Created " + ais);
91              TarArchiveEntry ae;
92              while ((ae = ais.getNextTarEntry()) != null) {
93                  log(ae);
94              }
95          }
96      }
97  
98      private static void usage() {
99          System.out.println("Parameters: archive-name [encoding]");
100     }
101 
102 }