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