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