1
22
23 package com.liferay.portal.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.FileUtil;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.kernel.util.Validator;
30 import com.liferay.portal.kernel.zip.ZipReader;
31
32 import de.schlichtherle.io.DefaultArchiveDetector;
33 import de.schlichtherle.io.File;
34 import de.schlichtherle.io.FileInputStream;
35 import de.schlichtherle.io.FileOutputStream;
36 import de.schlichtherle.io.archive.zip.ZipDriver;
37
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.io.OutputStream;
41
42 import java.util.ArrayList;
43 import java.util.List;
44
45
50 public class ZipReaderImpl implements ZipReader {
51
52 static {
53 File.setDefaultArchiveDetector(
54 new DefaultArchiveDetector(
55 DefaultArchiveDetector.DEFAULT, "lar|war|zip",
56 new ZipDriver()));
57 }
58
59 public ZipReaderImpl(InputStream inputStream) throws IOException {
60 _zipFile = new File(FileUtil.createTempFile("zip"));
61
62 OutputStream outputStream = new FileOutputStream(_zipFile);
63
64 try {
65 File.cat(inputStream, outputStream);
66 }
67 finally {
68 outputStream.close();
69 inputStream.close();
70 }
71 }
72
73 public ZipReaderImpl(java.io.File file) {
74 _zipFile = new File(file);
75 }
76
77 public void close() {
78 _zipFile.delete();
79 }
80
81 public List<String> getEntries() {
82 List<String> folderEntries = new ArrayList<String>();
83
84 File[] files = (File[])_zipFile.listFiles();
85
86 for (File file : files) {
87 if (!file.isDirectory()) {
88 folderEntries.add(file.getEnclEntryName());
89 }
90 else {
91 processDirectory(file, folderEntries);
92 }
93 }
94
95 return folderEntries;
96 }
97
98 public byte[] getEntryAsByteArray(String name) {
99 if (Validator.isNull(name)) {
100 return null;
101 }
102
103 byte[] bytes = null;
104
105 try {
106 InputStream is = getEntryAsInputStream(name);
107
108 if (is != null) {
109 bytes = FileUtil.getBytes(is);
110 }
111 }
112 catch (IOException e) {
113 _log.error(e, e);
114 }
115
116 return bytes;
117 }
118
119 public InputStream getEntryAsInputStream(String name) {
120 if (Validator.isNull(name)) {
121 return null;
122 }
123
124 if (name.startsWith(StringPool.SLASH)) {
125 name = name.substring(1);
126 }
127
128 File file = new File(_zipFile, name, DefaultArchiveDetector.NULL);
129
130 if (file.exists() && !file.isDirectory()) {
131 try {
132 if (_log.isDebugEnabled()) {
133 _log.debug("Extracting " + name);
134 }
135
136 return new FileInputStream(file);
137 }
138 catch (IOException ioe) {
139 _log.error(ioe, ioe);
140 }
141 }
142
143 return null;
144 }
145
146 public String getEntryAsString(String name) {
147 if (Validator.isNull(name)) {
148 return null;
149 }
150
151 byte[] bytes = getEntryAsByteArray(name);
152
153 if (bytes != null) {
154 return new String(bytes);
155 }
156
157 return null;
158 }
159
160 public List<String> getFolderEntries(String path) {
161 if (Validator.isNull(path)) {
162 return null;
163 }
164
165 List<String> folderEntries = new ArrayList<String>();
166
167 File directory = new File(_zipFile.getPath() + StringPool.SLASH + path);
168
169 File[] files = (File[])directory.listFiles();
170
171 for (File file : files) {
172 if (!file.isDirectory()) {
173 folderEntries.add(file.getEnclEntryName());
174 }
175 }
176
177 return folderEntries;
178 }
179
180 protected void processDirectory(
181 File directory, List<String> folderEntries) {
182
183 File[] files = (File[])directory.listFiles();
184
185 for (File file : files) {
186 if (!file.isDirectory()) {
187 folderEntries.add(file.getEnclEntryName());
188 }
189 else {
190 processDirectory(file, folderEntries);
191 }
192 }
193 }
194
195 private static Log _log = LogFactoryUtil.getLog(ZipReaderImpl.class);
196
197 private File _zipFile;
198
199 }