Repository: commons-compress Updated Branches: refs/heads/master d3dac8c0f -> 4da56433b
add 7z support to Lister CLI class Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/4da56433 Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/4da56433 Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/4da56433 Branch: refs/heads/master Commit: 4da56433ba20213f51e887d9ad6dfffe9669ba13 Parents: d3dac8c Author: Stefan Bodewig <[email protected]> Authored: Mon May 7 06:38:41 2018 +0200 Committer: Stefan Bodewig <[email protected]> Committed: Mon May 7 06:38:41 2018 +0200 ---------------------------------------------------------------------- .../commons/compress/archivers/Lister.java | 27 ++++++++++++++++++++ 1 file changed, 27 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-compress/blob/4da56433/src/main/java/org/apache/commons/compress/archivers/Lister.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/archivers/Lister.java b/src/main/java/org/apache/commons/compress/archivers/Lister.java index 7df9591..07a8e9c 100644 --- a/src/main/java/org/apache/commons/compress/archivers/Lister.java +++ b/src/main/java/org/apache/commons/compress/archivers/Lister.java @@ -20,8 +20,10 @@ package org.apache.commons.compress.archivers; import java.io.BufferedInputStream; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; +import org.apache.commons.compress.archivers.sevenz.SevenZFile; /** * Simple command line application that lists the contents of an archive. @@ -44,6 +46,15 @@ public final class Lister { if (!f.isFile()) { System.err.println(f + " doesn't exist or is a directory"); } + String format = args.length > 1 ? args[1] : detectFormat(f); + if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format)) { + list7z(f); + } else { + listStream(f, args); + } + } + + private static void listStream(File f, String[] args) throws ArchiveException, IOException { try (final InputStream fis = new BufferedInputStream(Files.newInputStream(f.toPath())); final ArchiveInputStream ais = createArchiveInputStream(args, fis)) { System.out.println("Created " + ais.toString()); @@ -62,6 +73,22 @@ public final class Lister { return factory.createArchiveInputStream(fis); } + private static String detectFormat(File f) throws ArchiveException, IOException { + try (final InputStream fis = new BufferedInputStream(Files.newInputStream(f.toPath()))) { + return factory.detect(fis); + } + } + + private static void list7z(File f) throws ArchiveException, IOException { + try (SevenZFile z = new SevenZFile(f)) { + System.out.println("Created " + z.toString()); + ArchiveEntry ae; + while ((ae = z.getNextEntry()) != null) { + System.out.println(ae.getName()); + } + } + } + private static void usage() { System.out.println("Parameters: archive-name [archive-type]"); }
