• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  */
18 package org.apache.commons.compress.archivers.sevenz;
19 
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 
24 public class CLI {
25 
26 
27     private enum Mode {
28         LIST("Analysing") {
29             @Override
takeAction(final SevenZFile archive, final SevenZArchiveEntry entry)30             public void takeAction(final SevenZFile archive, final SevenZArchiveEntry entry) {
31                 System.out.print(entry.getName());
32                 if (entry.isDirectory()) {
33                     System.out.print(" dir");
34                 } else {
35                     System.out.print(" " + entry.getCompressedSize()
36                                      + "/" + entry.getSize());
37                 }
38                 if (entry.getHasLastModifiedDate()) {
39                     System.out.print(" " + entry.getLastModifiedDate());
40                 } else {
41                     System.out.print(" no last modified date");
42                 }
43                 if (!entry.isDirectory()) {
44                     System.out.println(" " + getContentMethods(entry));
45                 } else {
46                     System.out.println("");
47                 }
48             }
49 
getContentMethods(final SevenZArchiveEntry entry)50             private String getContentMethods(final SevenZArchiveEntry entry) {
51                 final StringBuilder sb = new StringBuilder();
52                 boolean first = true;
53                 for (final SevenZMethodConfiguration m : entry.getContentMethods()) {
54                     if (!first) {
55                         sb.append(", ");
56                     }
57                     first = false;
58                     sb.append(m.getMethod());
59                     if (m.getOptions() != null) {
60                         sb.append("(").append(m.getOptions()).append(")");
61                     }
62                 }
63                 return sb.toString();
64             }
65         },
66         EXTRACT("Extracting") {
67             private final byte[] buf = new byte[8192];
68             @Override
takeAction(final SevenZFile archive, final SevenZArchiveEntry entry)69             public void takeAction(final SevenZFile archive, final SevenZArchiveEntry entry)
70                 throws IOException {
71                 final File outFile = new File(entry.getName());
72                 if (entry.isDirectory()) {
73                     if (!outFile.isDirectory() && !outFile.mkdirs()) {
74                         throw new IOException("Cannot create directory " + outFile);
75                     }
76                     System.out.println("created directory " + outFile);
77                     return;
78                 }
79 
80                 System.out.println("extracting to " + outFile);
81                 final File parent = outFile.getParentFile();
82                 if (parent != null && !parent.exists() && !parent.mkdirs()) {
83                     throw new IOException("Cannot create " + parent);
84                 }
85                 try (final FileOutputStream fos = new FileOutputStream(outFile)) {
86                     final long total = entry.getSize();
87                     long off = 0;
88                     while (off < total) {
89                         final int toRead = (int) Math.min(total - off, buf.length);
90                         final int bytesRead = archive.read(buf, 0, toRead);
91                         if (bytesRead < 1) {
92                             throw new IOException("reached end of entry "
93                                                   + entry.getName()
94                                                   + " after " + off
95                                                   + " bytes, expected "
96                                                   + total);
97                         }
98                         off += bytesRead;
99                         fos.write(buf, 0, bytesRead);
100                     }
101                 }
102             }
103         };
104 
105         private final String message;
Mode(final String message)106         Mode(final String message) {
107             this.message = message;
108         }
getMessage()109         public String getMessage() {
110             return message;
111         }
takeAction(SevenZFile archive, SevenZArchiveEntry entry)112         public abstract void takeAction(SevenZFile archive, SevenZArchiveEntry entry)
113             throws IOException;
114     }
115 
main(final String[] args)116     public static void main(final String[] args) throws Exception {
117         if (args.length == 0) {
118             usage();
119             return;
120         }
121         final Mode mode = grabMode(args);
122         System.out.println(mode.getMessage() + " " + args[0]);
123         final File f = new File(args[0]);
124         if (!f.isFile()) {
125             System.err.println(f + " doesn't exist or is a directory");
126         }
127         try (final SevenZFile archive = new SevenZFile(f)) {
128             SevenZArchiveEntry ae;
129             while((ae=archive.getNextEntry()) != null) {
130                 mode.takeAction(archive, ae);
131             }
132         }
133     }
134 
usage()135     private static void usage() {
136         System.out.println("Parameters: archive-name [list|extract]");
137     }
138 
grabMode(final String[] args)139     private static Mode grabMode(final String[] args) {
140         if (args.length < 2) {
141             return Mode.LIST;
142         }
143         return Enum.valueOf(Mode.class, args[1].toUpperCase());
144     }
145 
146 }
147