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.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.FileInputStream;
033    import de.schlichtherle.io.FileOutputStream;
034    import de.schlichtherle.io.archive.zip.ZipDriver;
035    
036    import java.io.IOException;
037    import java.io.InputStream;
038    import java.io.OutputStream;
039    
040    /**
041     * @author Raymond Aug??
042     */
043    public class ZipWriterImpl implements ZipWriter {
044    
045            static {
046                    File.setDefaultArchiveDetector(
047                            new DefaultArchiveDetector(
048                                    ArchiveDetector.ALL, "lar|" + ArchiveDetector.ALL.getSuffixes(),
049                                    new ZipDriver()));
050            }
051    
052            public ZipWriterImpl() {
053                    _file = new File(
054                            SystemProperties.get(SystemProperties.TMP_DIR) + StringPool.SLASH +
055                                    PortalUUIDUtil.generate() + ".zip");
056    
057                    _file.mkdir();
058    
059                    FinalizeManager.register(
060                            _file.getDelegate(),
061                            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                    OutputStream outputStream = new FileOutputStream(
100                            new File(getPath() + StringPool.SLASH + name));
101    
102                    try {
103                            File.cat(inputStream, outputStream);
104                    }
105                    finally {
106                            outputStream.close();
107                    }
108            }
109    
110            @Override
111            public void addEntry(String name, String s) throws IOException {
112                    addEntry(name, s.getBytes(StringPool.UTF8));
113            }
114    
115            @Override
116            public void addEntry(String name, StringBuilder sb) throws IOException {
117                    addEntry(name, sb.toString());
118            }
119    
120            @Override
121            public byte[] finish() throws IOException {
122                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
123                            new UnsyncByteArrayOutputStream();
124    
125                    InputStream inputStream = new FileInputStream(_file);
126    
127                    try {
128                            File.cat(inputStream, unsyncByteArrayOutputStream);
129                    }
130                    finally {
131                            unsyncByteArrayOutputStream.close();
132                            inputStream.close();
133                    }
134    
135                    return unsyncByteArrayOutputStream.toByteArray();
136            }
137    
138            @Override
139            public java.io.File getFile() {
140                    try {
141                            File.umount(_file);
142                    }
143                    catch (ArchiveException ae) {
144                            _log.error(ae, ae);
145                    }
146    
147                    return _file.getDelegate();
148            }
149    
150            @Override
151            public String getPath() {
152                    return _file.getPath();
153            }
154    
155            private static Log _log = LogFactoryUtil.getLog(ZipWriterImpl.class);
156    
157            private File _file;
158    
159    }