1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.zip;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.FileUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.kernel.zip.ZipReader;
23  
24  import de.schlichtherle.io.ArchiveDetector;
25  import de.schlichtherle.io.DefaultArchiveDetector;
26  import de.schlichtherle.io.File;
27  import de.schlichtherle.io.FileInputStream;
28  import de.schlichtherle.io.FileOutputStream;
29  import de.schlichtherle.io.archive.zip.ZipDriver;
30  
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.io.OutputStream;
34  
35  import java.util.ArrayList;
36  import java.util.List;
37  
38  /**
39   * <a href="ZipReaderImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Raymond Augé
42   */
43  public class ZipReaderImpl implements ZipReader {
44  
45      static {
46          File.setDefaultArchiveDetector(
47              new DefaultArchiveDetector(
48                  ArchiveDetector.ALL, "lar|" + ArchiveDetector.ALL.getSuffixes(),
49                  new ZipDriver()));
50      }
51  
52      public ZipReaderImpl(InputStream inputStream) throws IOException {
53          _zipFile = new File(FileUtil.createTempFile("zip"));
54  
55          OutputStream outputStream = new FileOutputStream(_zipFile);
56  
57          try {
58              File.cat(inputStream, outputStream);
59          }
60          finally {
61              outputStream.close();
62              inputStream.close();
63          }
64      }
65  
66      public ZipReaderImpl(java.io.File file) {
67          _zipFile = new File(file);
68      }
69  
70      public void close() {
71          _zipFile.delete();
72      }
73  
74      public List<String> getEntries() {
75          List<String> folderEntries = new ArrayList<String>();
76  
77          File[] files = (File[])_zipFile.listFiles();
78  
79          for (File file : files) {
80              if (!file.isDirectory()) {
81                  folderEntries.add(file.getEnclEntryName());
82              }
83              else {
84                  processDirectory(file, folderEntries);
85              }
86          }
87  
88          return folderEntries;
89      }
90  
91      public byte[] getEntryAsByteArray(String name) {
92          if (Validator.isNull(name)) {
93              return null;
94          }
95  
96          byte[] bytes = null;
97  
98          try {
99              InputStream is = getEntryAsInputStream(name);
100 
101             if (is != null) {
102                 bytes = FileUtil.getBytes(is);
103             }
104         }
105         catch (IOException e) {
106             _log.error(e, e);
107         }
108 
109         return bytes;
110     }
111 
112     public InputStream getEntryAsInputStream(String name) {
113         if (Validator.isNull(name)) {
114             return null;
115         }
116 
117         if (name.startsWith(StringPool.SLASH)) {
118             name = name.substring(1);
119         }
120 
121         File file = new File(_zipFile, name, DefaultArchiveDetector.NULL);
122 
123         if (file.exists() && !file.isDirectory()) {
124             try {
125                 if (_log.isDebugEnabled()) {
126                     _log.debug("Extracting " + name);
127                 }
128 
129                 return new FileInputStream(file);
130             }
131             catch (IOException ioe) {
132                 _log.error(ioe, ioe);
133             }
134         }
135 
136         return null;
137     }
138 
139     public String getEntryAsString(String name) {
140         if (Validator.isNull(name)) {
141             return null;
142         }
143 
144         byte[] bytes = getEntryAsByteArray(name);
145 
146         if (bytes != null) {
147             return new String(bytes);
148         }
149 
150         return null;
151     }
152 
153     public List<String> getFolderEntries(String path) {
154         if (Validator.isNull(path)) {
155             return null;
156         }
157 
158         List<String> folderEntries = new ArrayList<String>();
159 
160         File directory = new File(_zipFile.getPath() + StringPool.SLASH + path);
161 
162         File[] files = (File[])directory.listFiles();
163 
164         for (File file : files) {
165             if (!file.isDirectory()) {
166                 folderEntries.add(file.getEnclEntryName());
167             }
168         }
169 
170         return folderEntries;
171     }
172 
173     protected void processDirectory(
174         File directory, List<String> folderEntries) {
175 
176         File[] files = (File[])directory.listFiles();
177 
178         for (File file : files) {
179             if (!file.isDirectory()) {
180                 folderEntries.add(file.getEnclEntryName());
181             }
182             else {
183                 processDirectory(file, folderEntries);
184             }
185         }
186     }
187 
188     private static Log _log = LogFactoryUtil.getLog(ZipReaderImpl.class);
189 
190     private File _zipFile;
191 
192 }