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        final InputStream fis = new BufferedInputStream(new FileInputStream(f));
048        ArchiveInputStream ais;
049        if (args.length > 1) {
050            ais = factory.createArchiveInputStream(args[1], fis);
051        } else {
052            ais = factory.createArchiveInputStream(fis);
053        }
054        System.out.println("Created "+ais.toString());
055        ArchiveEntry ae;
056        while((ae=ais.getNextEntry()) != null){
057            System.out.println(ae.getName());
058        }
059        ais.close();
060        fis.close();
061    }
062
063    private static void usage() {
064        System.out.println("Parameters: archive-name [archive-type]");
065    }
066
067}