View Javadoc
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  
19  package org.apache.commons.compress.archivers.zip;
20  
21  import java.io.BufferedInputStream;
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.util.Enumeration;
28  
29  import org.apache.commons.compress.archivers.ArchiveEntry;
30  import org.apache.commons.compress.utils.IOUtils;
31  
32  /**
33   * Simple command line application that lists the contents of a ZIP archive.
34   *
35   * <p>The name of the archive must be given as a command line argument.</p>
36   *
37   * <p>Optional command line arguments specify the encoding to assume
38   * and whether to use ZipFile or ZipArchiveInputStream.</p>
39   */
40  public final class Lister {
41      private static class CommandLine {
42          String archive;
43          boolean useStream = false;
44          String encoding;
45          boolean allowStoredEntriesWithDataDescriptor = false;
46          String dir;
47      }
48  
49      public static void main(final String[] args) throws IOException {
50          final CommandLine cl = parse(args);
51          final File f = new File(cl.archive);
52          if (!f.isFile()) {
53              System.err.println(f + " doesn't exists or is a directory");
54              usage();
55          }
56          if (cl.useStream) {
57              try (BufferedInputStream fs = new BufferedInputStream(new FileInputStream(f))) {
58                  final ZipArchiveInputStream zs =
59                          new ZipArchiveInputStream(fs, cl.encoding, true,
60                                  cl.allowStoredEntriesWithDataDescriptor);
61                  for (ArchiveEntry entry = zs.getNextEntry();
62                       entry != null;
63                       entry = zs.getNextEntry()) {
64                      final ZipArchiveEntry ze = (ZipArchiveEntry) entry;
65                      list(ze);
66                      if (cl.dir != null) {
67                          extract(cl.dir, ze, zs);
68                      }
69                  }
70              }
71          } else {
72              try (ZipFile zf = new ZipFile(f, cl.encoding)) {
73                  for (final Enumeration<ZipArchiveEntry> entries = zf.getEntries();
74                       entries.hasMoreElements(); ) {
75                      final ZipArchiveEntry ze = entries.nextElement();
76                      list(ze);
77                      if (cl.dir != null) {
78                          try (InputStream is = zf.getInputStream(ze)) {
79                              extract(cl.dir, ze, is);
80                          }
81                      }
82                  }
83              }
84          }
85      }
86  
87      private static void list(final ZipArchiveEntry entry) {
88          System.out.println(entry.getName());
89      }
90  
91      private static void extract(final String dir, final ZipArchiveEntry entry,
92                                  final InputStream is) throws IOException {
93          final File f = new File(dir, entry.getName());
94          if (!f.getParentFile().exists()) {
95              f.getParentFile().mkdirs();
96          }
97          FileOutputStream fos = null;
98          try {
99              fos = new FileOutputStream(f);
100             IOUtils.copy(is, fos);
101         } finally {
102             if (fos != null) {
103                 fos.close();
104             }
105         }
106     }
107 
108     private static CommandLine parse(final String[] args) {
109         final CommandLine cl = new CommandLine();
110         boolean error = false;
111         for (int i = 0; i < args.length; i++) {
112             if (args[i].equals("-enc")) {
113                 if (args.length > i + 1) {
114                     cl.encoding = args[++i];
115                 } else {
116                     System.err.println("missing argument to -enc");
117                     error = true;
118                 }
119             } else if (args[i].equals("-extract")) {
120                 if (args.length > i + 1) {
121                     cl.dir = args[++i];
122                 } else {
123                     System.err.println("missing argument to -extract");
124                     error = true;
125                 }
126             } else if (args[i].equals("-stream")) {
127                 cl.useStream = true;
128             } else if (args[i].equals("+storeddd")) {
129                 cl.allowStoredEntriesWithDataDescriptor = true;
130             } else if (args[i].equals("-file")) {
131                 cl.useStream = false;
132             } else if (cl.archive != null) {
133                 System.err.println("Only one archive");
134                 error = true;
135             } else {
136                 cl.archive = args[i];
137             }
138         }
139         if (error || cl.archive == null) {
140             usage();
141         }
142         return cl;
143     }
144 
145     private static void usage() {
146         System.err.println("lister [-enc encoding] [-stream] [-file]"
147                            + " [+storeddd] [-extract dir] archive");
148         System.exit(1);
149     }
150 }