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<>();
079    
080                    File[] files = (File[])_zipFile.listFiles();
081    
082                    if (files == null) {
083                            return null;
084                    }
085    
086                    for (File file : files) {
087                            if (!file.isDirectory()) {
088                                    folderEntries.add(file.getEnclEntryName());
089                            }
090                            else {
091                                    processDirectory(file, folderEntries);
092                            }
093                    }
094    
095                    return folderEntries;
096            }
097    
098            @Override
099            public byte[] getEntryAsByteArray(String name) {
100                    if (Validator.isNull(name)) {
101                            return null;
102                    }
103    
104                    byte[] bytes = null;
105    
106                    try {
107                            InputStream is = getEntryAsInputStream(name);
108    
109                            if (is != null) {
110                                    bytes = FileUtil.getBytes(is);
111                            }
112                    }
113                    catch (IOException ioe) {
114                            _log.error(ioe, ioe);
115                    }
116    
117                    return bytes;
118            }
119    
120            @Override
121            public InputStream getEntryAsInputStream(String name) {
122                    if (Validator.isNull(name)) {
123                            return null;
124                    }
125    
126                    if (name.startsWith(StringPool.SLASH)) {
127                            name = name.substring(1);
128                    }
129    
130                    File file = new File(_zipFile, name, DefaultArchiveDetector.NULL);
131    
132                    if (file.exists() && !file.isDirectory()) {
133                            try {
134                                    if (_log.isDebugEnabled()) {
135                                            _log.debug("Extracting " + name);
136                                    }
137    
138                                    return new FileInputStream(file);
139                            }
140                            catch (IOException ioe) {
141                                    _log.error(ioe, ioe);
142                            }
143                    }
144    
145                    return null;
146            }
147    
148            @Override
149            public String getEntryAsString(String name) {
150                    if (Validator.isNull(name)) {
151                            return null;
152                    }
153    
154                    byte[] bytes = getEntryAsByteArray(name);
155    
156                    if (bytes != null) {
157                            return new String(bytes);
158                    }
159    
160                    return null;
161            }
162    
163            @Override
164            public List<String> getFolderEntries(String path) {
165                    if (Validator.isNull(path)) {
166                            return Collections.emptyList();
167                    }
168    
169                    List<String> folderEntries = new ArrayList<>();
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 final Log _log = LogFactoryUtil.getLog(ZipReaderImpl.class);
204    
205            static {
206                    File.setDefaultArchiveDetector(
207                            new DefaultArchiveDetector(
208                                    ArchiveDetector.ALL, "lar|" + ArchiveDetector.ALL.getSuffixes(),
209                                    new ZipDriver()));
210    
211                    TrueZIPHelperUtil.initialize();
212            }
213    
214            private final File _zipFile;
215    
216    }