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, new DeleteFileFinalizeAction(_file.getAbsolutePath()),
053                            FinalizeManager.PHANTOM_REFERENCE_FACTORY);
054            }
055    
056            public ZipWriterImpl(java.io.File file) {
057                    _file = new File(file.getAbsolutePath());
058    
059                    _file.mkdir();
060            }
061    
062            @Override
063            public void addEntry(String name, byte[] bytes) throws IOException {
064                    try (UnsyncByteArrayInputStream unsyncByteArrayInputStream =
065                                    new UnsyncByteArrayInputStream(bytes)) {
066    
067                            addEntry(name, unsyncByteArrayInputStream);
068                    }
069            }
070    
071            @Override
072            public void addEntry(String name, InputStream inputStream)
073                    throws IOException {
074    
075                    if (name.startsWith(StringPool.SLASH)) {
076                            name = name.substring(1);
077                    }
078    
079                    if (inputStream == null) {
080                            return;
081                    }
082    
083                    if (_log.isDebugEnabled()) {
084                            _log.debug("Adding " + name);
085                    }
086    
087                    FileUtil.mkdirs(getPath());
088    
089                    try (OutputStream outputStream = new FileOutputStream(
090                                    new File(getPath() + StringPool.SLASH + name))) {
091    
092                            File.cat(inputStream, outputStream);
093                    }
094            }
095    
096            @Override
097            public void addEntry(String name, String s) throws IOException {
098                    if (s == null) {
099                            return;
100                    }
101    
102                    addEntry(name, s.getBytes(StringPool.UTF8));
103            }
104    
105            @Override
106            public void addEntry(String name, StringBuilder sb) throws IOException {
107                    if (sb == null) {
108                            return;
109                    }
110    
111                    addEntry(name, sb.toString());
112            }
113    
114            @Override
115            public byte[] finish() throws IOException {
116                    java.io.File file = getFile();
117    
118                    return FileUtil.getBytes(file);
119            }
120    
121            @Override
122            public java.io.File getFile() {
123                    try {
124                            File.umount(_file);
125                    }
126                    catch (ArchiveException ae) {
127                            _log.error(ae, ae);
128                    }
129    
130                    return _file.getDelegate();
131            }
132    
133            @Override
134            public String getPath() {
135                    return _file.getPath();
136            }
137    
138            private static final Log _log = LogFactoryUtil.getLog(ZipWriterImpl.class);
139    
140            static {
141                    File.setDefaultArchiveDetector(
142                            new DefaultArchiveDetector(
143                                    ArchiveDetector.ALL, "lar|" + ArchiveDetector.ALL.getSuffixes(),
144                                    new ZipDriver()));
145            }
146    
147            private final File _file;
148    
149    }