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