001
014
015 package com.liferay.portal.zip;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.util.FileUtil;
020 import com.liferay.portal.kernel.util.StringPool;
021 import com.liferay.portal.kernel.util.Validator;
022 import com.liferay.portal.kernel.zip.ZipReader;
023
024 import de.schlichtherle.io.ArchiveBusyWarningException;
025 import de.schlichtherle.io.ArchiveDetector;
026 import de.schlichtherle.io.ArchiveException;
027 import de.schlichtherle.io.DefaultArchiveDetector;
028 import de.schlichtherle.io.File;
029 import de.schlichtherle.io.FileInputStream;
030 import de.schlichtherle.io.FileOutputStream;
031 import de.schlichtherle.io.archive.zip.ZipDriver;
032
033 import java.io.IOException;
034 import java.io.InputStream;
035 import java.io.OutputStream;
036
037 import java.util.ArrayList;
038 import java.util.Collections;
039 import java.util.List;
040
041
044 public class ZipReaderImpl implements ZipReader {
045
046 static {
047 File.setDefaultArchiveDetector(
048 new DefaultArchiveDetector(
049 ArchiveDetector.ALL, "lar|" + ArchiveDetector.ALL.getSuffixes(),
050 new ZipDriver()));
051 }
052
053 public ZipReaderImpl(InputStream inputStream) throws IOException {
054 _zipFile = new File(FileUtil.createTempFile("zip"));
055
056 OutputStream outputStream = new FileOutputStream(_zipFile);
057
058 try {
059 File.cat(inputStream, outputStream);
060 }
061 finally {
062 outputStream.close();
063 inputStream.close();
064 }
065 }
066
067 public ZipReaderImpl(java.io.File file) {
068 _zipFile = new File(file);
069 }
070
071 public void close() {
072 try {
073 File.umount(_zipFile);
074 }
075 catch (ArchiveBusyWarningException abwe) {
076 if (_log.isWarnEnabled()) {
077 _log.warn(abwe, abwe);
078 }
079 }
080 catch (ArchiveException ae) {
081 _log.error(ae, ae);
082 }
083 }
084
085 public List<String> getEntries() {
086 List<String> folderEntries = new ArrayList<String>();
087
088 File[] files = (File[])_zipFile.listFiles();
089
090 for (File file : files) {
091 if (!file.isDirectory()) {
092 folderEntries.add(file.getEnclEntryName());
093 }
094 else {
095 processDirectory(file, folderEntries);
096 }
097 }
098
099 return folderEntries;
100 }
101
102 public byte[] getEntryAsByteArray(String name) {
103 if (Validator.isNull(name)) {
104 return null;
105 }
106
107 byte[] bytes = null;
108
109 try {
110 InputStream is = getEntryAsInputStream(name);
111
112 if (is != null) {
113 bytes = FileUtil.getBytes(is);
114 }
115 }
116 catch (IOException e) {
117 _log.error(e, e);
118 }
119
120 return bytes;
121 }
122
123 public InputStream getEntryAsInputStream(String name) {
124 if (Validator.isNull(name)) {
125 return null;
126 }
127
128 if (name.startsWith(StringPool.SLASH)) {
129 name = name.substring(1);
130 }
131
132 File file = new File(_zipFile, name, DefaultArchiveDetector.NULL);
133
134 if (file.exists() && !file.isDirectory()) {
135 try {
136 if (_log.isDebugEnabled()) {
137 _log.debug("Extracting " + name);
138 }
139
140 return new FileInputStream(file);
141 }
142 catch (IOException ioe) {
143 _log.error(ioe, ioe);
144 }
145 }
146
147 return null;
148 }
149
150 public String getEntryAsString(String name) {
151 if (Validator.isNull(name)) {
152 return null;
153 }
154
155 byte[] bytes = getEntryAsByteArray(name);
156
157 if (bytes != null) {
158 return new String(bytes);
159 }
160
161 return null;
162 }
163
164 public List<String> getFolderEntries(String path) {
165 if (Validator.isNull(path)) {
166 return Collections.emptyList();
167 }
168
169 List<String> folderEntries = new ArrayList<String>();
170
171 File directory = new File(_zipFile.getPath() + StringPool.SLASH + path);
172
173 if (!directory.exists()) {
174 return folderEntries;
175 }
176
177 File[] files = (File[])directory.listFiles();
178
179 for (File file : files) {
180 if (!file.isDirectory()) {
181 folderEntries.add(file.getEnclEntryName());
182 }
183 }
184
185 return folderEntries;
186 }
187
188 protected void processDirectory(
189 File directory, List<String> folderEntries) {
190
191 File[] files = (File[])directory.listFiles();
192
193 for (File file : files) {
194 if (!file.isDirectory()) {
195 folderEntries.add(file.getEnclEntryName());
196 }
197 else {
198 processDirectory(file, folderEntries);
199 }
200 }
201 }
202
203 private static Log _log = LogFactoryUtil.getLog(ZipReaderImpl.class);
204
205 private File _zipFile;
206
207 }