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