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.io.unsync.UnsyncByteArrayInputStream;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.memory.DeleteFileFinalizeAction;
021    import com.liferay.portal.kernel.memory.FinalizeManager;
022    import com.liferay.portal.kernel.util.FileUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.SystemProperties;
025    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
026    import com.liferay.portal.kernel.zip.ZipWriter;
027    
028    import de.schlichtherle.io.ArchiveDetector;
029    import de.schlichtherle.io.ArchiveException;
030    import de.schlichtherle.io.DefaultArchiveDetector;
031    import de.schlichtherle.io.File;
032    import de.schlichtherle.io.FileOutputStream;
033    import de.schlichtherle.io.archive.zip.ZipDriver;
034    
035    import java.io.IOException;
036    import java.io.InputStream;
037    import java.io.OutputStream;
038    
039    /**
040     * @author Raymond Aug??
041     */
042    public class ZipWriterImpl implements ZipWriter {
043    
044            public ZipWriterImpl() {
045                    _file = new File(
046                            SystemProperties.get(SystemProperties.TMP_DIR) + StringPool.SLASH +
047                                    PortalUUIDUtil.generate() + ".zip");
048    
049                    _file.mkdir();
050    
051                    FinalizeManager.register(
052                            _file.getDelegate(),
053                            new DeleteFileFinalizeAction(_file.getAbsolutePath()),
054                            FinalizeManager.PHANTOM_REFERENCE_FACTORY);
055            }
056    
057            public ZipWriterImpl(java.io.File file) {
058                    _file = new File(file.getAbsolutePath());
059    
060                    _file.mkdir();
061            }
062    
063            @Override
064            public void addEntry(String name, byte[] bytes) throws IOException {
065                    try (UnsyncByteArrayInputStream unsyncByteArrayInputStream =
066                                    new UnsyncByteArrayInputStream(bytes)) {
067    
068                            addEntry(name, unsyncByteArrayInputStream);
069                    }
070            }
071    
072            @Override
073            public void addEntry(String name, InputStream inputStream)
074                    throws IOException {
075    
076                    if (name.startsWith(StringPool.SLASH)) {
077                            name = name.substring(1);
078                    }
079    
080                    if (inputStream == null) {
081                            return;
082                    }
083    
084                    if (_log.isDebugEnabled()) {
085                            _log.debug("Adding " + name);
086                    }
087    
088                    FileUtil.mkdirs(getPath());
089    
090                    try (OutputStream outputStream = new FileOutputStream(
091                                    new File(getPath() + StringPool.SLASH + name))) {
092    
093                            File.cat(inputStream, outputStream);
094                    }
095            }
096    
097            @Override
098            public void addEntry(String name, String s) throws IOException {
099                    if (s == null) {
100                            return;
101                    }
102    
103                    addEntry(name, s.getBytes(StringPool.UTF8));
104            }
105    
106            @Override
107            public void addEntry(String name, StringBuilder sb) throws IOException {
108                    if (sb == null) {
109                            return;
110                    }
111    
112                    addEntry(name, sb.toString());
113            }
114    
115            @Override
116            public byte[] finish() throws IOException {
117                    java.io.File file = getFile();
118    
119                    return FileUtil.getBytes(file);
120            }
121    
122            @Override
123            public java.io.File getFile() {
124                    try {
125                            File.umount(_file);
126                    }
127                    catch (ArchiveException ae) {
128                            _log.error(ae, ae);
129                    }
130    
131                    return _file.getDelegate();
132            }
133    
134            @Override
135            public String getPath() {
136                    return _file.getPath();
137            }
138    
139            private static final Log _log = LogFactoryUtil.getLog(ZipWriterImpl.class);
140    
141            static {
142                    File.setDefaultArchiveDetector(
143                            new DefaultArchiveDetector(
144                                    ArchiveDetector.ALL, "lar|" + ArchiveDetector.ALL.getSuffixes(),
145                                    new ZipDriver()));
146            }
147    
148            private final File _file;
149    
150    }