CLI.java

  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. import java.io.File;
  19. import java.io.IOException;
  20. import java.util.Locale;

  21. /**
  22.  * Usage: archive-name [list]
  23.  */
  24. public class CLI {

  25.     private enum Mode {
  26.         LIST("Analysing") {
  27.             private String getContentMethods(final SevenZArchiveEntry entry) {
  28.                 final StringBuilder sb = new StringBuilder();
  29.                 boolean first = true;
  30.                 for (final SevenZMethodConfiguration m : entry.getContentMethods()) {
  31.                     if (!first) {
  32.                         sb.append(", ");
  33.                     }
  34.                     first = false;
  35.                     sb.append(m.getMethod());
  36.                     if (m.getOptions() != null) {
  37.                         sb.append("(").append(m.getOptions()).append(")");
  38.                     }
  39.                 }
  40.                 return sb.toString();
  41.             }

  42.             @Override
  43.             public void takeAction(final SevenZFile archive, final SevenZArchiveEntry entry) {
  44.                 System.out.print(entry.getName());
  45.                 if (entry.isDirectory()) {
  46.                     System.out.print(" dir");
  47.                 } else {
  48.                     System.out.print(" " + entry.getCompressedSize() + "/" + entry.getSize());
  49.                 }
  50.                 if (entry.getHasLastModifiedDate()) {
  51.                     System.out.print(" " + entry.getLastModifiedDate());
  52.                 } else {
  53.                     System.out.print(" no last modified date");
  54.                 }
  55.                 if (!entry.isDirectory()) {
  56.                     System.out.println(" " + getContentMethods(entry));
  57.                 } else {
  58.                     System.out.println();
  59.                 }
  60.             }
  61.         };

  62.         private final String message;

  63.         Mode(final String message) {
  64.             this.message = message;
  65.         }

  66.         public String getMessage() {
  67.             return message;
  68.         }

  69.         public abstract void takeAction(SevenZFile archive, SevenZArchiveEntry entry) throws IOException;
  70.     }

  71.     private static Mode grabMode(final String[] args) {
  72.         if (args.length < 2) {
  73.             return Mode.LIST;
  74.         }
  75.         return Enum.valueOf(Mode.class, args[1].toUpperCase(Locale.ROOT));
  76.     }

  77.     public static void main(final String[] args) throws Exception {
  78.         if (args.length == 0) {
  79.             usage();
  80.             return;
  81.         }
  82.         final Mode mode = grabMode(args);
  83.         System.out.println(mode.getMessage() + " " + args[0]);
  84.         final File file = new File(args[0]);
  85.         if (!file.isFile()) {
  86.             System.err.println(file + " doesn't exist or is a directory");
  87.         }
  88.         try (SevenZFile archive = SevenZFile.builder().setFile(file).get()) {
  89.             SevenZArchiveEntry ae;
  90.             while ((ae = archive.getNextEntry()) != null) {
  91.                 mode.takeAction(archive, ae);
  92.             }
  93.         }
  94.     }

  95.     private static void usage() {
  96.         System.out.println("Parameters: archive-name [list]");
  97.     }

  98. }