1
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
31 import java.io.File;
32 import java.io.FileInputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.Serializable;
36
37 import java.util.ArrayList;
38 import java.util.LinkedHashMap;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.zip.ZipEntry;
42 import java.util.zip.ZipInputStream;
43
44
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 byte[] bytes = _entries.get(name);
132
133 if ((bytes == null) && name.startsWith(StringPool.SLASH)) {
134 bytes = _entries.get(name.substring(1));
135 }
136
137 return bytes;
138 }
139
140 public String getEntryAsString(String name) {
141 byte[] bytes = getEntryAsByteArray(name);
142
143 if (bytes != null) {
144 return new String(bytes);
145 }
146
147 return null;
148 }
149
150 public Map<String, List<ObjectValuePair<String, byte[]>>>
151 getFolderEntries() {
152
153 return _folderEntries;
154 }
155
156 public List<ObjectValuePair<String, byte[]>> getFolderEntries(String path) {
157 return _folderEntries.get(path);
158 }
159
160 private static final int _BUFFER = 2048;
161
162 private static Log _log = LogFactoryUtil.getLog(ZipReader.class);
163
164 private ZipInputStream _zis;
165 private Map<String, byte[]> _entries = new LinkedHashMap<String, byte[]>();
166 private Map<String, List<ObjectValuePair<String, byte[]>>> _folderEntries =
167 new LinkedHashMap<String, List<ObjectValuePair<String, byte[]>>>();
168 private byte[] _data = new byte[_BUFFER];
169
170 }