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.IOException;
024import java.io.InputStream;
025import java.nio.file.Files;
026import org.apache.commons.compress.archivers.sevenz.SevenZFile;
027
028/**
029 * Simple command line application that lists the contents of an archive.
030 *
031 * <p>The name of the archive must be given as a command line argument.</p>
032 * <p>The optional second argument defines the archive type, in case the format is not recognized.</p>
033 *
034 * @since 1.1
035 */
036public final class Lister {
037    private static final ArchiveStreamFactory factory = new ArchiveStreamFactory();
038
039    public static void main(final String[] args) throws Exception {
040        if (args.length == 0) {
041            usage();
042            return;
043        }
044        System.out.println("Analysing " + args[0]);
045        final File f = new File(args[0]);
046        if (!f.isFile()) {
047            System.err.println(f + " doesn't exist or is a directory");
048        }
049        String format = args.length > 1 ? args[1] : detectFormat(f);
050        if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format)) {
051            list7z(f);
052        } else {
053            listStream(f, args);
054        }
055    }
056
057    private static void listStream(File f, String[] args) throws ArchiveException, IOException {
058        try (final InputStream fis = new BufferedInputStream(Files.newInputStream(f.toPath()));
059                final ArchiveInputStream ais = createArchiveInputStream(args, fis)) {
060            System.out.println("Created " + ais.toString());
061            ArchiveEntry ae;
062            while ((ae = ais.getNextEntry()) != null) {
063                System.out.println(ae.getName());
064            }
065        }
066    }
067
068    private static ArchiveInputStream createArchiveInputStream(final String[] args, final InputStream fis)
069            throws ArchiveException {
070        if (args.length > 1) {
071            return factory.createArchiveInputStream(args[1], fis);
072        }
073        return factory.createArchiveInputStream(fis);
074    }
075
076    private static String detectFormat(File f) throws ArchiveException, IOException {
077        try (final InputStream fis = new BufferedInputStream(Files.newInputStream(f.toPath()))) {
078            return factory.detect(fis);
079        }
080    }
081
082    private static void list7z(File f) throws ArchiveException, IOException {
083        try (SevenZFile z = new SevenZFile(f)) {
084            System.out.println("Created " + z.toString());
085            ArchiveEntry ae;
086            while ((ae = z.getNextEntry()) != null) {
087                System.out.println(ae.getName());
088            }
089        }
090    }
091
092    private static void usage() {
093        System.out.println("Parameters: archive-name [archive-type]");
094    }
095
096}