1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.zip;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.FileUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.kernel.zip.ZipReader;
31  
32  import de.schlichtherle.io.DefaultArchiveDetector;
33  import de.schlichtherle.io.File;
34  import de.schlichtherle.io.FileInputStream;
35  import de.schlichtherle.io.FileOutputStream;
36  import de.schlichtherle.io.archive.zip.ZipDriver;
37  
38  import java.io.IOException;
39  import java.io.InputStream;
40  import java.io.OutputStream;
41  
42  import java.util.ArrayList;
43  import java.util.List;
44  
45  /**
46   * <a href="ZipReaderImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Raymond Augé
49   */
50  public class ZipReaderImpl implements ZipReader {
51  
52      static {
53          File.setDefaultArchiveDetector(
54              new DefaultArchiveDetector(
55                  DefaultArchiveDetector.DEFAULT, "lar|war|zip",
56                  new ZipDriver()));
57      }
58  
59      public ZipReaderImpl(InputStream inputStream) throws IOException {
60          _zipFile = new File(FileUtil.createTempFile("zip"));
61  
62          OutputStream outputStream = new FileOutputStream(_zipFile);
63  
64          try {
65              File.cat(inputStream, outputStream);
66          }
67          finally {
68              outputStream.close();
69              inputStream.close();
70          }
71      }
72  
73      public ZipReaderImpl(java.io.File file) {
74          _zipFile = new File(file);
75      }
76  
77      public void close() {
78          _zipFile.delete();
79      }
80  
81      public List<String> getEntries() {
82          List<String> folderEntries = new ArrayList<String>();
83  
84          File[] files = (File[])_zipFile.listFiles();
85  
86          for (File file : files) {
87              if (!file.isDirectory()) {
88                  folderEntries.add(file.getEnclEntryName());
89              }
90              else {
91                  processDirectory(file, folderEntries);
92              }
93          }
94  
95          return folderEntries;
96      }
97  
98      public byte[] getEntryAsByteArray(String name) {
99          if (Validator.isNull(name)) {
100             return null;
101         }
102 
103         byte[] bytes = null;
104 
105         try {
106             InputStream is = getEntryAsInputStream(name);
107 
108             if (is != null) {
109                 bytes = FileUtil.getBytes(is);
110             }
111         }
112         catch (IOException e) {
113             _log.error(e, e);
114         }
115 
116         return bytes;
117     }
118 
119     public InputStream getEntryAsInputStream(String name) {
120         if (Validator.isNull(name)) {
121             return null;
122         }
123 
124         if (name.startsWith(StringPool.SLASH)) {
125             name = name.substring(1);
126         }
127 
128         File file = new File(_zipFile, name, DefaultArchiveDetector.NULL);
129 
130         if (file.exists() && !file.isDirectory()) {
131             try {
132                 if (_log.isDebugEnabled()) {
133                     _log.debug("Extracting " + name);
134                 }
135 
136                 return new FileInputStream(file);
137             }
138             catch (IOException ioe) {
139                 _log.error(ioe, ioe);
140             }
141         }
142 
143         return null;
144     }
145 
146     public String getEntryAsString(String name) {
147         if (Validator.isNull(name)) {
148             return null;
149         }
150 
151         byte[] bytes = getEntryAsByteArray(name);
152 
153         if (bytes != null) {
154             return new String(bytes);
155         }
156 
157         return null;
158     }
159 
160     public List<String> getFolderEntries(String path) {
161         if (Validator.isNull(path)) {
162             return null;
163         }
164 
165         List<String> folderEntries = new ArrayList<String>();
166 
167         File directory = new File(_zipFile.getPath() + StringPool.SLASH + path);
168 
169         File[] files = (File[])directory.listFiles();
170 
171         for (File file : files) {
172             if (!file.isDirectory()) {
173                 folderEntries.add(file.getEnclEntryName());
174             }
175         }
176 
177         return folderEntries;
178     }
179 
180     protected void processDirectory(
181         File directory, List<String> folderEntries) {
182 
183         File[] files = (File[])directory.listFiles();
184 
185         for (File file : files) {
186             if (!file.isDirectory()) {
187                 folderEntries.add(file.getEnclEntryName());
188             }
189             else {
190                 processDirectory(file, folderEntries);
191             }
192         }
193     }
194 
195     private static Log _log = LogFactoryUtil.getLog(ZipReaderImpl.class);
196 
197     private File _zipFile;
198 
199 }