commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject commons-compress git commit: add 7z support to Lister CLI class
Date Mon, 07 May 2018 04:39:05 GMT
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 <bodewig@apache.org>
Authored: Mon May 7 06:38:41 2018 +0200
Committer: Stefan Bodewig <bodewig@apache.org>
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]");
     }


Mime
View raw message