View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.commons.compress.archivers.tar;
21  
22  import java.io.BufferedInputStream;
23  import java.io.File;
24  import java.io.InputStream;
25  import java.nio.file.Files;
26  
27  /**
28   * Simple command line application that lists the contents of a tar archive.
29   *
30   * <p>
31   * The name of the archive must be given as a command line argument.
32   * </p>
33   * <p>
34   * The optional second argument specifies the encoding to assume for file names.
35   * </p>
36   *
37   * @since 1.11
38   */
39  public final class TarLister {
40  
41      private static void log(final TarArchiveEntry ae) {
42          final StringBuilder sb = new StringBuilder(Integer.toOctalString(ae.getMode())).append(" ");
43          String name = ae.getUserName();
44          if (name != null && !name.isEmpty()) {
45              sb.append(name);
46          } else {
47              sb.append(ae.getLongUserId());
48          }
49          sb.append("/");
50          name = ae.getGroupName();
51          if (name != null && !name.isEmpty()) {
52              sb.append(name);
53          } else {
54              sb.append(ae.getLongGroupId());
55          }
56          sb.append(" ");
57          if (ae.isSparse()) {
58              sb.append(ae.getRealSize());
59          } else if (ae.isCharacterDevice() || ae.isBlockDevice()) {
60              sb.append(ae.getDevMajor()).append(",").append(ae.getDevMinor());
61          } else {
62              sb.append(ae.getSize());
63          }
64          sb.append(" ").append(ae.getLastModifiedDate()).append(" ");
65          sb.append(ae.getName());
66          if (ae.isSymbolicLink() || ae.isLink()) {
67              if (ae.isSymbolicLink()) {
68                  sb.append(" -> ");
69              } else {
70                  sb.append(" link to ");
71              }
72              sb.append(ae.getLinkName());
73          }
74          if (ae.isSparse()) {
75              sb.append(" (sparse)");
76          }
77          System.out.println(sb);
78      }
79  
80      public static void main(final String[] args) throws Exception {
81          if (args.length == 0) {
82              usage();
83              return;
84          }
85          System.out.println("Analysing " + args[0]);
86          final File f = new File(args[0]);
87          if (!f.isFile()) {
88              System.err.println(f + " doesn't exist or is a directory");
89          }
90          try (InputStream fis = new BufferedInputStream(Files.newInputStream(f.toPath()));
91                  TarArchiveInputStream ais = args.length > 1 ? new TarArchiveInputStream(fis, args[1]) : new TarArchiveInputStream(fis)) {
92              System.out.println("Created " + ais);
93              TarArchiveEntry ae;
94              while ((ae = ais.getNextTarEntry()) != null) {
95                  log(ae);
96              }
97          }
98      }
99  
100     private static void usage() {
101         System.out.println("Parameters: archive-name [encoding]");
102     }
103 
104 }