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  package org.apache.commons.compress.archivers.sevenz;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.apache.commons.lang3.ArrayUtils;
25  import org.apache.commons.lang3.StringUtils;
26  
27  /**
28   * Usage: archive-name [list]
29   */
30  public class CLI {
31  
32      /**
33       * Enumerates modes.
34       */
35      private enum Mode {
36          LIST("Analysing") {
37              private String getContentMethods(final SevenZArchiveEntry entry) {
38                  final StringBuilder sb = new StringBuilder();
39                  boolean first = true;
40                  for (final SevenZMethodConfiguration m : entry.getContentMethods()) {
41                      if (!first) {
42                          sb.append(", ");
43                      }
44                      first = false;
45                      sb.append(m.getMethod());
46                      if (m.getOptions() != null) {
47                          sb.append("(").append(m.getOptions()).append(")");
48                      }
49                  }
50                  return sb.toString();
51              }
52  
53              @Override
54              public void takeAction(final SevenZFile archive, final SevenZArchiveEntry entry) {
55                  System.out.print(entry.getName());
56                  if (entry.isDirectory()) {
57                      System.out.print(" dir");
58                  } else {
59                      System.out.print(" " + entry.getCompressedSize() + "/" + entry.getSize());
60                  }
61                  if (entry.getHasLastModifiedDate()) {
62                      System.out.print(" " + entry.getLastModifiedDate());
63                  } else {
64                      System.out.print(" no last modified date");
65                  }
66                  if (!entry.isDirectory()) {
67                      System.out.println(" " + getContentMethods(entry));
68                  } else {
69                      System.out.println();
70                  }
71              }
72          };
73  
74          private final String message;
75  
76          Mode(final String message) {
77              this.message = message;
78          }
79  
80          public String getMessage() {
81              return message;
82          }
83  
84          public abstract void takeAction(SevenZFile archive, SevenZArchiveEntry entry) throws IOException;
85      }
86  
87      private static Mode grabMode(final String[] args) {
88          if (args.length < 2) {
89              return Mode.LIST;
90          }
91          return Enum.valueOf(Mode.class, StringUtils.toRootUpperCase(args[1]));
92      }
93  
94      /**
95       * Command line entry point.
96       *
97       * @param args Command line arguments.
98       * @throws IOException if an I/O problem occurs.
99       */
100     public static void main(final String[] args) throws IOException {
101         if (ArrayUtils.isEmpty(args)) {
102             usage();
103             return;
104         }
105         final Mode mode = grabMode(args);
106         System.out.println(mode.getMessage() + " " + args[0]);
107         final File file = new File(args[0]);
108         if (!file.isFile()) {
109             System.err.println(file + " doesn't exist or is a directory");
110         }
111         try (SevenZFile archive = SevenZFile.builder().setFile(file).get()) {
112             SevenZArchiveEntry ae;
113             while ((ae = archive.getNextEntry()) != null) {
114                 mode.takeAction(archive, ae);
115             }
116         }
117     }
118 
119     private static void usage() {
120         System.out.println("Parameters: archive-name [list]");
121     }
122 
123 }