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.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   */
52  public class ZipReader implements Serializable {
53  
54      public ZipReader(File file) throws Exception {
55          this(new FileInputStream(file));
56      }
57  
58      public ZipReader(InputStream stream) {
59          try {
60              _zis = new ZipInputStream(stream);
61  
62              while (true) {
63                  ZipEntry entry = _zis.getNextEntry();
64  
65                  if (entry == null) {
66                      break;
67                  }
68  
69                  String currentName = entry.getName();
70  
71                  ByteArrayMaker bam = new ByteArrayMaker();
72  
73                  while (true) {
74                      int count = _zis.read(_data, 0, _BUFFER);
75  
76                      if (count == -1) {
77                          break;
78                      }
79  
80                      bam.write(_data, 0, count);
81                  }
82  
83                  byte[] bytes = bam.toByteArray();
84  
85                  _entries.put(currentName, bytes);
86  
87                  int pos = currentName.lastIndexOf(StringPool.SLASH);
88  
89                  String folderPath = StringPool.BLANK;
90                  String fileName = currentName;
91  
92                  if (pos > 0) {
93                      folderPath = currentName.substring(0, pos + 1);
94                      fileName = currentName.substring(pos + 1);
95                  }
96  
97                  List<ObjectValuePair<String, byte[]>> files =
98                      _folderEntries.get(folderPath);
99  
100                 if (files == null) {
101                     files = new ArrayList<ObjectValuePair<String, byte[]>>();
102 
103                     _folderEntries.put(folderPath, files);
104                 }
105 
106                 ObjectValuePair<String, byte[]> ovp =
107                     new ObjectValuePair<String, byte[]>(fileName, bytes);
108 
109                 files.add(ovp);
110             }
111         }
112         catch (IOException ioe) {
113             _log.error(ioe, ioe);
114         }
115         finally {
116             try {
117                 _zis.close();
118             }
119             catch (Exception e) {
120                 if (_log.isWarnEnabled()) {
121                     _log.warn(e);
122                 }
123             }
124         }
125     }
126 
127     public Map<String, byte[]> getEntries() {
128         return _entries;
129     }
130 
131     public byte[] getEntryAsByteArray(String name) {
132         if (Validator.isNull(name)) {
133             return null;
134         }
135 
136         byte[] bytes = _entries.get(name);
137 
138         if ((bytes == null) && name.startsWith(StringPool.SLASH)) {
139             bytes = _entries.get(name.substring(1));
140         }
141 
142         return bytes;
143     }
144 
145     public String getEntryAsString(String name) {
146         byte[] bytes = getEntryAsByteArray(name);
147 
148         if (bytes != null) {
149             return new String(bytes);
150         }
151 
152         return null;
153     }
154 
155     public Map<String, List<ObjectValuePair<String, byte[]>>>
156         getFolderEntries() {
157 
158         return _folderEntries;
159     }
160 
161     public List<ObjectValuePair<String, byte[]>> getFolderEntries(String path) {
162         if (Validator.isNull(path)) {
163             return null;
164         }
165 
166         return _folderEntries.get(path);
167     }
168 
169     private static final int _BUFFER = 2048;
170 
171     private static Log _log = LogFactoryUtil.getLog(ZipReader.class);
172 
173     private ZipInputStream _zis;
174     private Map<String, byte[]> _entries = new LinkedHashMap<String, byte[]>();
175     private Map<String, List<ObjectValuePair<String, byte[]>>> _folderEntries =
176         new LinkedHashMap<String, List<ObjectValuePair<String, byte[]>>>();
177     private byte[] _data = new byte[_BUFFER];
178 
179 }