add 7z support to Lister CLI class
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 static void main(final String[] args) throws Exception {
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 @@ private static ArchiveInputStream createArchiveInputStream(final String[] args,
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]");
}
