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                    try (OutputStream outputStream = new FileOutputStream(
089                                    new File(getPath() + StringPool.SLASH + name))) {
090    
091                            File.cat(inputStream, outputStream);
092                    }
093            }
094    
095            @Override
096            public void addEntry(String name, String s) throws IOException {
097                    if (s == null) {
098                            return;
099                    }
100    
101                    addEntry(name, s.getBytes(StringPool.UTF8));
102            }
103    
104            @Override
105            public void addEntry(String name, StringBuilder sb) throws IOException {
106                    if (sb == null) {
107                            return;
108                    }
109    
110                    addEntry(name, sb.toString());
111            }
112    
113            @Override
114            public byte[] finish() throws IOException {
115                    java.io.File file = getFile();
116    
117                    return FileUtil.getBytes(file);
118            }
119    
120            @Override
121            public java.io.File getFile() {
122                    try {
123                            File.umount(_file);
124                    }
125                    catch (ArchiveException ae) {
126                            _log.error(ae, ae);
127                    }
128    
129                    return _file.getDelegate();
130            }
131    
132            @Override
133            public String getPath() {
134                    return _file.getPath();
135            }
136    
137            private static final Log _log = LogFactoryUtil.getLog(ZipWriterImpl.class);
138    
139            static {
140                    File.setDefaultArchiveDetector(
141                            new DefaultArchiveDetector(
142                                    ArchiveDetector.ALL, "lar|" + ArchiveDetector.ALL.getSuffixes(),
143                                    new ZipDriver()));
144    
145                    TrueZIPHelperUtil.initialize();
146            }
147    
148            private final File _file;
149    
150    }