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