| ZipReader.java |
1 /**
2 * Copyright (c) 2000-2007 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
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.LinkedHashMap;
36 import java.util.Map;
37 import java.util.zip.ZipEntry;
38 import java.util.zip.ZipInputStream;
39
40 /**
41 * <a href="ZipReader.java.html"><b><i>View Source</i></b></a>
42 *
43 * @author Alexander Chow
44 * @author Brian Wing Shun Chan
45 *
46 */
47 public class ZipReader implements Serializable {
48
49 public ZipReader(File file) throws Exception {
50 _zis = new ZipInputStream(new FileInputStream(file));
51 }
52
53 public ZipReader(InputStream stream) {
54 try {
55 _zis = new ZipInputStream(stream);
56
57 while (true) {
58 ZipEntry entry = _zis.getNextEntry();
59
60 if (entry == null) {
61 break;
62 }
63
64 String currentName = entry.getName();
65
66 ByteArrayMaker bam = new ByteArrayMaker();
67
68 while (true) {
69 int count = _zis.read(_data, 0, _BUFFER);
70
71 if (count == -1) {
72 break;
73 }
74
75 bam.write(_data, 0, count);
76 }
77
78 byte[] byteArray = bam.toByteArray();
79
80 _entries.put(currentName, byteArray);
81 }
82 }
83 catch (IOException ioe) {
84 _log.error(ioe, ioe);
85 }
86 finally {
87 try {
88 _zis.close();
89 }
90 catch (Exception e) {
91 if (_log.isWarnEnabled()) {
92 _log.warn(e);
93 }
94 }
95 }
96 }
97
98 public Map getEntries() {
99 return _entries;
100 }
101
102 public String getEntryAsString(String name) {
103 byte[] byteArray = getEntryAsByteArray(name);
104
105 if (byteArray != null) {
106 return new String(byteArray);
107 }
108
109 return null;
110 }
111
112 public byte[] getEntryAsByteArray(String name) {
113 return (byte[])_entries.get(name);
114 }
115
116 private static final int _BUFFER = 2048;
117
118 private static Log _log = LogFactoryUtil.getLog(ZipReader.class);
119
120 private ZipInputStream _zis;
121 private Map _entries = new LinkedHashMap();
122 private byte[] _data = new byte[_BUFFER];
123
124 }