    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one
    003 * or more contributor license agreements.  See the NOTICE file
    004 * distributed with this work for additional information
    005 * regarding copyright ownership.  The ASF licenses this file
    006 * to you under the Apache License, Version 2.0 (the
    007 * "License"); you may not use this file except in compliance
    008 * with the License.  You may obtain a copy of the License at
    009 *
    010 *   https://www.apache.org/licenses/LICENSE-2.0
    011 *
    012 * Unless required by applicable law or agreed to in writing,
    013 * software distributed under the License is distributed on an
    014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015 * KIND, either express or implied.  See the License for the
    016 * specific language governing permissions and limitations
    017 * under the License.
    018 */
    019package org.apache.commons.compress.archivers.sevenz;
    020
    021import java.io.File;
    022import java.io.IOException;
    023
    024import org.apache.commons.lang3.ArrayUtils;
    025import org.apache.commons.lang3.StringUtils;
    026
    027/**
    028 * Usage: archive-name [list]
    029 */
    030public class CLI {
    031
    032    /**
    033     * Enumerates modes.
    034     */
    035    private enum Mode {
    036        LIST("Analysing") {
    037            private String getContentMethods(final SevenZArchiveEntry entry) {
    038                final StringBuilder sb = new StringBuilder();
    039                boolean first = true;
    040                for (final SevenZMethodConfiguration m : entry.getContentMethods()) {
    041                    if (!first) {
    042                        sb.append(", ");
    043                    }
    044                    first = false;
    045                    sb.append(m.getMethod());
    046                    if (m.getOptions() != null) {
    047                        sb.append("(").append(m.getOptions()).append(")");
    048                    }
    049                }
    050                return sb.toString();
    051            }
    052
    053            @Override
    054            public void takeAction(final SevenZFile archive, final SevenZArchiveEntry entry) {
    055                System.out.print(entry.getName());
    056                if (entry.isDirectory()) {
    057                    System.out.print(" dir");
    058                } else {
    059                    System.out.print(" " + entry.getCompressedSize() + "/" + entry.getSize());
    060                }
    061                if (entry.getHasLastModifiedDate()) {
    062                    System.out.print(" " + entry.getLastModifiedDate());
    063                } else {
    064                    System.out.print(" no last modified date");
    065                }
    066                if (!entry.isDirectory()) {
    067                    System.out.println(" " + getContentMethods(entry));
    068                } else {
    069                    System.out.println();
    070                }
    071            }
    072        };
    073
    074        private final String message;
    075
    076        Mode(final String message) {
    077            this.message = message;
    078        }
    079
    080        public String getMessage() {
    081            return message;
    082        }
    083
    084        public abstract void takeAction(SevenZFile archive, SevenZArchiveEntry entry) throws IOException;
    085    }
    086
    087    private static Mode grabMode(final String[] args) {
    088        if (args.length < 2) {
    089            return Mode.LIST;
    090        }
    091        return Enum.valueOf(Mode.class, StringUtils.toRootUpperCase(args[1]));
    092    }
    093
    094    /**
    095     * Command line entry point.
    096     *
    097     * @param args Command line arguments.
    098     * @throws IOException if an I/O problem occurs.
    099     */
    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}