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;
019
020import java.io.BufferedInputStream;
021import java.io.File;
022import java.io.IOException;
023import java.io.InputStream;
024import java.nio.file.Files;
025import java.util.Enumeration;
026
027import org.apache.commons.compress.archivers.sevenz.SevenZFile;
028import org.apache.commons.compress.archivers.tar.TarFile;
029import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
030import org.apache.commons.compress.archivers.zip.ZipFile;
031
032/**
033 * Simple command line application that lists the contents of an archive.
034 *
035 * <p>The name of the archive must be given as a command line argument.</p>
036 * <p>The optional second argument defines the archive type, in case the format is not recognized.</p>
037 *
038 * @since 1.1
039 */
040public final class Lister {
041
042    private static final ArchiveStreamFactory FACTORY = ArchiveStreamFactory.DEFAULT;
043
044    private static ArchiveInputStream createArchiveInputStream(final String[] args, final InputStream fis)
045            throws ArchiveException {
046        if (args.length > 1) {
047            return FACTORY.createArchiveInputStream(args[1], fis);
048        }
049        return FACTORY.createArchiveInputStream(fis);
050    }
051
052    private static String detectFormat(final File f) throws ArchiveException, IOException {
053        try (final InputStream fis = new BufferedInputStream(Files.newInputStream(f.toPath()))) {
054            return ArchiveStreamFactory.detect(fis);
055        }
056    }
057
058    private static void list7z(final File f) throws IOException {
059        try (SevenZFile z = new SevenZFile(f)) {
060            System.out.println("Created " + z);
061            ArchiveEntry ae;
062            while ((ae = z.getNextEntry()) != null) {
063                final String name = ae.getName() == null ? z.getDefaultName() + " (entry name was null)"
064                    : ae.getName();
065                System.out.println(name);
066            }
067        }
068    }
069
070    private static void listStream(final File f, final String[] args) throws ArchiveException, IOException {
071        try (final InputStream fis = new BufferedInputStream(Files.newInputStream(f.toPath()));
072                final ArchiveInputStream ais = createArchiveInputStream(args, fis)) {
073            System.out.println("Created " + ais.toString());
074            ArchiveEntry ae;
075            while ((ae = ais.getNextEntry()) != null) {
076                System.out.println(ae.getName());
077            }
078        }
079    }
080
081    private static void listZipUsingTarFile(final File f) throws IOException {
082        try (TarFile t = new TarFile(f)) {
083            System.out.println("Created " + t);
084            t.getEntries().forEach(en -> System.out.println(en.getName()));
085        }
086    }
087
088    private static void listZipUsingZipFile(final File f) throws IOException {
089        try (ZipFile z = new ZipFile(f)) {
090            System.out.println("Created " + z);
091            for (final Enumeration<ZipArchiveEntry> en = z.getEntries(); en.hasMoreElements(); ) {
092                System.out.println(en.nextElement().getName());
093            }
094        }
095    }
096
097    /**
098     * Runs this class from the command line.
099     * <p>
100     * The name of the archive must be given as a command line argument.
101     * </p>
102     * <p>
103     * The optional second argument defines the archive type, in case the format is not recognized.
104     * </p>
105     *
106     * @param args name of the archive and optional argument archive type.
107     * @throws ArchiveException Archiver related Exception.
108     * @throws IOException an I/O exception.
109     */
110    public static void main(final String[] args) throws ArchiveException, IOException {
111        if (args.length == 0) {
112            usage();
113            return;
114        }
115        System.out.println("Analysing " + args[0]);
116        final File f = new File(args[0]);
117        if (!f.isFile()) {
118            System.err.println(f + " doesn't exist or is a directory");
119        }
120        final String format = args.length > 1 ? args[1] : detectFormat(f);
121        if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format)) {
122            list7z(f);
123        } else if ("zipfile".equals(format)) {
124            listZipUsingZipFile(f);
125        } else if ("tarfile".equals(format)) {
126            listZipUsingTarFile(f);
127        } else {
128            listStream(f, args);
129        }
130    }
131
132    private static void usage() {
133        System.out.println("Parameters: archive-name [archive-type]\n");
134        System.out.println("The magic archive-type 'zipfile' prefers ZipFile over ZipArchiveInputStream");
135        System.out.println("The magic archive-type 'tarfile' prefers TarFile over TarArchiveInputStream");
136    }
137
138}