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 _zis = new ZipInputStream(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[] byteArray = bam.toByteArray();
83
84 _entries.put(currentName, byteArray);
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, byteArray);
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 String getEntryAsString(String name) {
131 byte[] byteArray = getEntryAsByteArray(name);
132
133 if (byteArray != null) {
134 return new String(byteArray);
135 }
136
137 return null;
138 }
139
140 public byte[] getEntryAsByteArray(String name) {
141 return _entries.get(name);
142 }
143
144 public Map<String, List<ObjectValuePair<String, byte[]>>>
145 getFolderEntries() {
146
147 return _folderEntries;
148 }
149
150 private static final int _BUFFER = 2048;
151
152 private static Log _log = LogFactoryUtil.getLog(ZipReader.class);
153
154 private ZipInputStream _zis;
155 private Map<String, byte[]> _entries = new LinkedHashMap<String, byte[]>();
156 private Map<String, List<ObjectValuePair<String, byte[]>>> _folderEntries =
157 new LinkedHashMap<String, List<ObjectValuePair<String, byte[]>>>();
158 private byte[] _data = new byte[_BUFFER];
159
160 }