1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.archivers.sevenz;
20
21 import java.io.File;
22 import java.io.IOException;
23
24 import org.apache.commons.lang3.ArrayUtils;
25 import org.apache.commons.lang3.StringUtils;
26
27
28
29
30 public class CLI {
31
32
33
34
35 private enum Mode {
36 LIST("Analysing") {
37 private String getContentMethods(final SevenZArchiveEntry entry) {
38 final StringBuilder sb = new StringBuilder();
39 boolean first = true;
40 for (final SevenZMethodConfiguration m : entry.getContentMethods()) {
41 if (!first) {
42 sb.append(", ");
43 }
44 first = false;
45 sb.append(m.getMethod());
46 if (m.getOptions() != null) {
47 sb.append("(").append(m.getOptions()).append(")");
48 }
49 }
50 return sb.toString();
51 }
52
53 @Override
54 public void takeAction(final SevenZFile archive, final SevenZArchiveEntry entry) {
55 System.out.print(entry.getName());
56 if (entry.isDirectory()) {
57 System.out.print(" dir");
58 } else {
59 System.out.print(" " + entry.getCompressedSize() + "/" + entry.getSize());
60 }
61 if (entry.getHasLastModifiedDate()) {
62 System.out.print(" " + entry.getLastModifiedDate());
63 } else {
64 System.out.print(" no last modified date");
65 }
66 if (!entry.isDirectory()) {
67 System.out.println(" " + getContentMethods(entry));
68 } else {
69 System.out.println();
70 }
71 }
72 };
73
74 private final String message;
75
76 Mode(final String message) {
77 this.message = message;
78 }
79
80 public String getMessage() {
81 return message;
82 }
83
84 public abstract void takeAction(SevenZFile archive, SevenZArchiveEntry entry) throws IOException;
85 }
86
87 private static Mode grabMode(final String[] args) {
88 if (args.length < 2) {
89 return Mode.LIST;
90 }
91 return Enum.valueOf(Mode.class, StringUtils.toRootUpperCase(args[1]));
92 }
93
94
95
96
97
98
99
100 public static void main(final String[] args) throws IOException {
101 if (ArrayUtils.isEmpty(args)) {
102 usage();
103 return;
104 }
105 final Mode mode = grabMode(args);
106 System.out.println(mode.getMessage() + " " + args[0]);
107 final File file = new File(args[0]);
108 if (!file.isFile()) {
109 System.err.println(file + " doesn't exist or is a directory");
110 }
111 try (SevenZFile archive = SevenZFile.builder().setFile(file).get()) {
112 SevenZArchiveEntry ae;
113 while ((ae = archive.getNextEntry()) != null) {
114 mode.takeAction(archive, ae);
115 }
116 }
117 }
118
119 private static void usage() {
120 System.out.println("Parameters: archive-name [list]");
121 }
122
123 }