001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.ArchiveDetector;
025    import de.schlichtherle.io.DefaultArchiveDetector;
026    import de.schlichtherle.io.File;
027    import de.schlichtherle.io.FileInputStream;
028    import de.schlichtherle.io.FileOutputStream;
029    import de.schlichtherle.io.archive.zip.ZipDriver;
030    
031    import java.io.IOException;
032    import java.io.InputStream;
033    import java.io.OutputStream;
034    
035    import java.util.ArrayList;
036    import java.util.List;
037    
038    /**
039     * @author Raymond Augé
040     */
041    public class ZipReaderImpl implements ZipReader {
042    
043            static {
044                    File.setDefaultArchiveDetector(
045                            new DefaultArchiveDetector(
046                                    ArchiveDetector.ALL, "lar|" + ArchiveDetector.ALL.getSuffixes(),
047                                    new ZipDriver()));
048            }
049    
050            public ZipReaderImpl(InputStream inputStream) throws IOException {
051                    _zipFile = new File(FileUtil.createTempFile("zip"));
052    
053                    OutputStream outputStream = new FileOutputStream(_zipFile);
054    
055                    try {
056                            File.cat(inputStream, outputStream);
057                    }
058                    finally {
059                            outputStream.close();
060                            inputStream.close();
061                    }
062            }
063    
064            public ZipReaderImpl(java.io.File file) {
065                    _zipFile = new File(file);
066            }
067    
068            public void close() {
069                    _zipFile.delete();
070            }
071    
072            public List<String> getEntries() {
073                    List<String> folderEntries = new ArrayList<String>();
074    
075                    File[] files = (File[])_zipFile.listFiles();
076    
077                    for (File file : files) {
078                            if (!file.isDirectory()) {
079                                    folderEntries.add(file.getEnclEntryName());
080                            }
081                            else {
082                                    processDirectory(file, folderEntries);
083                            }
084                    }
085    
086                    return folderEntries;
087            }
088    
089            public byte[] getEntryAsByteArray(String name) {
090                    if (Validator.isNull(name)) {
091                            return null;
092                    }
093    
094                    byte[] bytes = null;
095    
096                    try {
097                            InputStream is = getEntryAsInputStream(name);
098    
099                            if (is != null) {
100                                    bytes = FileUtil.getBytes(is);
101                            }
102                    }
103                    catch (IOException e) {
104                            _log.error(e, e);
105                    }
106    
107                    return bytes;
108            }
109    
110            public InputStream getEntryAsInputStream(String name) {
111                    if (Validator.isNull(name)) {
112                            return null;
113                    }
114    
115                    if (name.startsWith(StringPool.SLASH)) {
116                            name = name.substring(1);
117                    }
118    
119                    File file = new File(_zipFile, name, DefaultArchiveDetector.NULL);
120    
121                    if (file.exists() && !file.isDirectory()) {
122                            try {
123                                    if (_log.isDebugEnabled()) {
124                                            _log.debug("Extracting " + name);
125                                    }
126    
127                                    return new FileInputStream(file);
128                            }
129                            catch (IOException ioe) {
130                                    _log.error(ioe, ioe);
131                            }
132                    }
133    
134                    return null;
135            }
136    
137            public String getEntryAsString(String name) {
138                    if (Validator.isNull(name)) {
139                            return null;
140                    }
141    
142                    byte[] bytes = getEntryAsByteArray(name);
143    
144                    if (bytes != null) {
145                            return new String(bytes);
146                    }
147    
148                    return null;
149            }
150    
151            public List<String> getFolderEntries(String path) {
152                    if (Validator.isNull(path)) {
153                            return null;
154                    }
155    
156                    List<String> folderEntries = new ArrayList<String>();
157    
158                    File directory = new File(_zipFile.getPath() + StringPool.SLASH + path);
159    
160                    File[] files = (File[])directory.listFiles();
161    
162                    for (File file : files) {
163                            if (!file.isDirectory()) {
164                                    folderEntries.add(file.getEnclEntryName());
165                            }
166                    }
167    
168                    return folderEntries;
169            }
170    
171            protected void processDirectory(
172                    File directory, List<String> folderEntries) {
173    
174                    File[] files = (File[])directory.listFiles();
175    
176                    for (File file : files) {
177                            if (!file.isDirectory()) {
178                                    folderEntries.add(file.getEnclEntryName());
179                            }
180                            else {
181                                    processDirectory(file, folderEntries);
182                            }
183                    }
184            }
185    
186            private static Log _log = LogFactoryUtil.getLog(ZipReaderImpl.class);
187    
188            private File _zipFile;
189    
190    }