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