    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;
    021
    022/**
    023 * Usage: archive-name [list]
    024 */
    025public class CLI {
    026
    027    private enum Mode {
    028        LIST("Analysing") {
    029            private String getContentMethods(final SevenZArchiveEntry entry) {
    030                final StringBuilder sb = new StringBuilder();
    031                boolean first = true;
    032                for (final SevenZMethodConfiguration m : entry.getContentMethods()) {
    033                    if (!first) {
    034                        sb.append(", ");
    035                    }
    036                    first = false;
    037                    sb.append(m.getMethod());
    038                    if (m.getOptions() != null) {
    039                        sb.append("(").append(m.getOptions()).append(")");
    040                    }
    041                }
    042                return sb.toString();
    043            }
    044
    045            @Override
    046            public void takeAction(final SevenZFile archive, final SevenZArchiveEntry entry) {
    047                System.out.print(entry.getName());
    048                if (entry.isDirectory()) {
    049                    System.out.print(" dir");
    050                } else {
    051                    System.out.print(" " + entry.getCompressedSize() + "/" + entry.getSize());
    052                }
    053                if (entry.getHasLastModifiedDate()) {
    054                    System.out.print(" " + entry.getLastModifiedDate());
    055                } else {
    056                    System.out.print(" no last modified date");
    057                }
    058                if (!entry.isDirectory()) {
    059                    System.out.println(" " + getContentMethods(entry));
    060                } else {
    061                    System.out.println();
    062                }
    063            }
    064        };
    065
    066        private final String message;
    067
    068        Mode(final String message) {
    069            this.message = message;
    070        }
    071
    072        public String getMessage() {
    073            return message;
    074        }
    075
    076        public abstract void takeAction(SevenZFile archive, SevenZArchiveEntry entry) throws IOException;
    077    }
    078
    079    private static Mode grabMode(final String[] args) {
    080        if (args.length < 2) {
    081            return Mode.LIST;
    082        }
    083        return Enum.valueOf(Mode.class, args[1].toUpperCase());
    084    }
    085
    086    public static void main(final String[] args) throws Exception {
    087        if (args.length == 0) {
    088            usage();
    089            return;
    090        }
    091        final Mode mode = grabMode(args);
    092        System.out.println(mode.getMessage() + " " + args[0]);
    093        final File f = new File(args[0]);
    094        if (!f.isFile()) {
    095            System.err.println(f + " doesn't exist or is a directory");
    096        }
    097        try (final SevenZFile archive = new SevenZFile(f)) {
    098            SevenZArchiveEntry ae;
    099            while ((ae = archive.getNextEntry()) != null) {
    100                mode.takeAction(archive, ae);
    101            }
    102        }
    103    }
    104
    105    private static void usage() {
    106        System.out.println("Parameters: archive-name [list]");
    107    }
    108
    109}