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