    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 */
    018
    019package org.apache.commons.compress.archivers;
    020
    021import java.io.BufferedInputStream;
    022import java.io.File;
    023import java.io.FileInputStream;
    024import java.io.InputStream;
    025
    026/**
    027 * Simple command line application that lists the contents of an archive.
    028 *
    029 * <p>The name of the archive must be given as a command line argument.</p>
    030 * <p>The optional second argument defines the archive type, in case the format is not recognized.</p>
    031 *
    032 * @since 1.1
    033 */
    034public final class Lister {
    035    private static final ArchiveStreamFactory factory = new ArchiveStreamFactory();
    036
    037    public static void main(final String[] args) throws Exception {
    038        if (args.length == 0) {
    039            usage();
    040            return;
    041        }
    042        System.out.println("Analysing " + args[0]);
    043        final File f = new File(args[0]);
    044        if (!f.isFile()) {
    045            System.err.println(f + " doesn't exist or is a directory");
    046        }
    047        try (final InputStream fis = new BufferedInputStream(new FileInputStream(f));
    048                final ArchiveInputStream ais = createArchiveInputStream(args, fis)) {
    049            System.out.println("Created " + ais.toString());
    050            ArchiveEntry ae;
    051            while ((ae = ais.getNextEntry()) != null) {
    052                System.out.println(ae.getName());
    053            }
    054        }
    055    }
    056
    057    private static ArchiveInputStream createArchiveInputStream(final String[] args, final InputStream fis)
    058            throws ArchiveException {
    059        if (args.length > 1) {
    060            return factory.createArchiveInputStream(args[1], fis);
    061        }
    062        return factory.createArchiveInputStream(fis);
    063    }
    064
    065    private static void usage() {
    066        System.out.println("Parameters: archive-name [archive-type]");
    067    }
    068
    069}