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(String[] args) throws IOException {
50          CommandLine cl = parse(args);
51          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              BufferedInputStream fs =
58                  new BufferedInputStream(new FileInputStream(f));
59              try {
60                  ZipArchiveInputStream zs =
61                      new ZipArchiveInputStream(fs, cl.encoding, true,
62                                                cl.allowStoredEntriesWithDataDescriptor);
63                  for (ArchiveEntry entry = zs.getNextEntry();
64                       entry != null;
65                       entry = zs.getNextEntry()) {
66                      ZipArchiveEntry ze = (ZipArchiveEntry) entry;
67                      list(ze);
68                      if (cl.dir != null) {
69                          extract(cl.dir, ze, zs);
70                      }
71                  }
72              } finally {
73                  fs.close();
74              }
75          } else {
76              ZipFile zf = new ZipFile(f, cl.encoding);
77              try {
78                  for (Enumeration<ZipArchiveEntry> entries = zf.getEntries();
79                       entries.hasMoreElements(); ) {
80                      ZipArchiveEntry ze = entries.nextElement();
81                      list(ze);
82                      if (cl.dir != null) {
83                          InputStream is = zf.getInputStream(ze);
84                          try {
85                              extract(cl.dir, ze, is);
86                          } finally {
87                              is.close();
88                          }
89                      }
90                  }
91              } finally {
92                  zf.close();
93              }
94          }
95      }
96  
97      private static void list(ZipArchiveEntry entry) {
98          System.out.println(entry.getName());
99      }
100 
101     private static void extract(String dir, ZipArchiveEntry entry,
102                                 InputStream is) throws IOException {
103         File f = new File(dir, entry.getName());
104         if (!f.getParentFile().exists()) {
105             f.getParentFile().mkdirs();
106         }
107         FileOutputStream fos = null;
108         try {
109             fos = new FileOutputStream(f);
110             IOUtils.copy(is, fos);
111         } finally {
112             if (fos != null) {
113                 fos.close();
114             }
115         }
116     }
117 
118     private static CommandLine parse(String[] args) {
119         CommandLine cl = new CommandLine();
120         boolean error = false;
121         for (int i = 0; i < args.length; i++) {
122             if (args[i].equals("-enc")) {
123                 if (args.length > i + 1) {
124                     cl.encoding = args[++i];
125                 } else {
126                     System.err.println("missing argument to -enc");
127                     error = true;
128                 }
129             } else if (args[i].equals("-extract")) {
130                 if (args.length > i + 1) {
131                     cl.dir = args[++i];
132                 } else {
133                     System.err.println("missing argument to -extract");
134                     error = true;
135                 }
136             } else if (args[i].equals("-stream")) {
137                 cl.useStream = true;
138             } else if (args[i].equals("+storeddd")) {
139                 cl.allowStoredEntriesWithDataDescriptor = true;
140             } else if (args[i].equals("-file")) {
141                 cl.useStream = false;
142             } else if (cl.archive != null) {
143                 System.err.println("Only one archive");
144                 error = true;
145             } else {
146                 cl.archive = args[i];
147             }
148         }
149         if (error || cl.archive == null) {
150             usage();
151         }
152         return cl;
153     }
154 
155     private static void usage() {
156         System.err.println("lister [-enc encoding] [-stream] [-file]"
157                            + " [+storeddd] [-extract dir] archive");
158         System.exit(1);
159     }
160 }