001    /**
002     * Copyright (c) 2000-2012 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            public void addEntry(String name, byte[] bytes) throws IOException {
071                    UnsyncByteArrayInputStream unsyncByteArrayInputStream =
072                            new UnsyncByteArrayInputStream(bytes);
073    
074                    try {
075                            addEntry(name, unsyncByteArrayInputStream);
076                    }
077                    finally {
078                            unsyncByteArrayInputStream.close();
079                    }
080            }
081    
082            public void addEntry(String name, InputStream inpuStream)
083                    throws IOException {
084    
085                    if (name.startsWith(StringPool.SLASH)) {
086                            name = name.substring(1);
087                    }
088    
089                    if (inpuStream == null) {
090                            return;
091                    }
092    
093                    if (_log.isDebugEnabled()) {
094                            _log.debug("Adding " + name);
095                    }
096    
097                    FileUtil.mkdirs(getPath());
098    
099                    OutputStream outputStream = new FileOutputStream(
100                            new File(getPath() + StringPool.SLASH + name));
101    
102                    try {
103                            File.cat(inpuStream, outputStream);
104                    }
105                    finally {
106                            outputStream.close();
107                    }
108            }
109    
110            public void addEntry(String name, String s) throws IOException {
111                    addEntry(name, s.getBytes(StringPool.UTF8));
112            }
113    
114            public void addEntry(String name, StringBuilder sb) throws IOException {
115                    addEntry(name, sb.toString());
116            }
117    
118            public byte[] finish() throws IOException {
119                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
120                            new UnsyncByteArrayOutputStream();
121    
122                    InputStream inputStream = new FileInputStream(_file);
123    
124                    try {
125                            File.cat(inputStream, unsyncByteArrayOutputStream);
126                    }
127                    finally {
128                            unsyncByteArrayOutputStream.close();
129                            inputStream.close();
130                    }
131    
132                    return unsyncByteArrayOutputStream.toByteArray();
133            }
134    
135            public java.io.File getFile() {
136                    try {
137                            File.umount(_file);
138                    }
139                    catch (ArchiveException ae) {
140                            _log.error(ae, ae);
141                    }
142    
143                    return _file.getDelegate();
144            }
145    
146            public String getPath() {
147                    return _file.getPath();
148            }
149    
150            private static Log _log = LogFactoryUtil.getLog(ZipWriter.class);
151    
152            private File _file;
153    
154    }