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