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