1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.zip;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.ByteArrayMaker;
25  import com.liferay.portal.kernel.util.ObjectValuePair;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  
29  import java.io.File;
30  import java.io.FileInputStream;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.io.Serializable;
34  
35  import java.util.ArrayList;
36  import java.util.LinkedHashMap;
37  import java.util.List;
38  import java.util.Map;
39  import java.util.zip.ZipEntry;
40  import java.util.zip.ZipInputStream;
41  
42  /**
43   * <a href="ZipReader.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Alexander Chow
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class ZipReader implements Serializable {
50  
51      public ZipReader(File file) throws Exception {
52          this(new FileInputStream(file));
53      }
54  
55      public ZipReader(InputStream stream) {
56          try {
57              _zis = new ZipInputStream(stream);
58  
59              while (true) {
60                  ZipEntry entry = _zis.getNextEntry();
61  
62                  if (entry == null) {
63                      break;
64                  }
65  
66                  String currentName = entry.getName();
67  
68                  ByteArrayMaker bam = new ByteArrayMaker();
69  
70                  while (true) {
71                      int count = _zis.read(_data, 0, _BUFFER);
72  
73                      if (count == -1) {
74                          break;
75                      }
76  
77                      bam.write(_data, 0, count);
78                  }
79  
80                  byte[] bytes = bam.toByteArray();
81  
82                  _entries.put(currentName, bytes);
83  
84                  int pos = currentName.lastIndexOf(StringPool.SLASH);
85  
86                  String folderPath = StringPool.BLANK;
87                  String fileName = currentName;
88  
89                  if (pos > 0) {
90                      folderPath = currentName.substring(0, pos + 1);
91                      fileName = currentName.substring(pos + 1);
92                  }
93  
94                  List<ObjectValuePair<String, byte[]>> files =
95                      _folderEntries.get(folderPath);
96  
97                  if (files == null) {
98                      files = new ArrayList<ObjectValuePair<String, byte[]>>();
99  
100                     _folderEntries.put(folderPath, files);
101                 }
102 
103                 ObjectValuePair<String, byte[]> ovp =
104                     new ObjectValuePair<String, byte[]>(fileName, bytes);
105 
106                 files.add(ovp);
107             }
108         }
109         catch (IOException ioe) {
110             _log.error(ioe, ioe);
111         }
112         finally {
113             try {
114                 _zis.close();
115             }
116             catch (Exception e) {
117                 if (_log.isWarnEnabled()) {
118                     _log.warn(e);
119                 }
120             }
121         }
122     }
123 
124     public Map<String, byte[]> getEntries() {
125         return _entries;
126     }
127 
128     public byte[] getEntryAsByteArray(String name) {
129         if (Validator.isNull(name)) {
130             return null;
131         }
132 
133         byte[] bytes = _entries.get(name);
134 
135         if ((bytes == null) && name.startsWith(StringPool.SLASH)) {
136             bytes = _entries.get(name.substring(1));
137         }
138 
139         return bytes;
140     }
141 
142     public String getEntryAsString(String name) {
143         byte[] bytes = getEntryAsByteArray(name);
144 
145         if (bytes != null) {
146             return new String(bytes);
147         }
148 
149         return null;
150     }
151 
152     public Map<String, List<ObjectValuePair<String, byte[]>>>
153         getFolderEntries() {
154 
155         return _folderEntries;
156     }
157 
158     public List<ObjectValuePair<String, byte[]>> getFolderEntries(String path) {
159         if (Validator.isNull(path)) {
160             return null;
161         }
162 
163         return _folderEntries.get(path);
164     }
165 
166     private static final int _BUFFER = 2048;
167 
168     private static Log _log = LogFactoryUtil.getLog(ZipReader.class);
169 
170     private ZipInputStream _zis;
171     private Map<String, byte[]> _entries = new LinkedHashMap<String, byte[]>();
172     private Map<String, List<ObjectValuePair<String, byte[]>>> _folderEntries =
173         new LinkedHashMap<String, List<ObjectValuePair<String, byte[]>>>();
174     private byte[] _data = new byte[_BUFFER];
175 
176 }