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