1
22
23 package com.liferay.portal.zip;
24
25 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
26 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
27 import com.liferay.portal.kernel.log.Log;
28 import com.liferay.portal.kernel.log.LogFactoryUtil;
29 import com.liferay.portal.kernel.util.FileUtil;
30 import com.liferay.portal.kernel.util.StringPool;
31 import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
32 import com.liferay.portal.kernel.zip.ZipWriter;
33 import com.liferay.util.SystemProperties;
34
35 import de.schlichtherle.io.ArchiveException;
36 import de.schlichtherle.io.DefaultArchiveDetector;
37 import de.schlichtherle.io.File;
38 import de.schlichtherle.io.FileInputStream;
39 import de.schlichtherle.io.FileOutputStream;
40 import de.schlichtherle.io.archive.zip.ZipDriver;
41
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.io.OutputStream;
45
46
51 public class ZipWriterImpl implements ZipWriter {
52
53 static {
54 File.setDefaultArchiveDetector(
55 new DefaultArchiveDetector(
56 DefaultArchiveDetector.DEFAULT, "lar", new ZipDriver()));
57 }
58
59 public ZipWriterImpl() {
60 _file = new File(
61 SystemProperties.get(SystemProperties.TMP_DIR) + StringPool.SLASH +
62 PortalUUIDUtil.generate() + ".zip");
63
64 _file.mkdir();
65 _file.deleteOnExit();
66 }
67
68 public ZipWriterImpl(java.io.File file) {
69 _file = new File(file);
70
71 _file.mkdir();
72 }
73
74 public void addEntry(String name, byte[] bytes) throws IOException {
75 UnsyncByteArrayInputStream unsyncByteArrayInputStream =
76 new UnsyncByteArrayInputStream(bytes);
77
78 try {
79 addEntry(name, unsyncByteArrayInputStream);
80 }
81 finally {
82 unsyncByteArrayInputStream.close();
83 }
84 }
85
86 public void addEntry(String name, InputStream inpuStream)
87 throws IOException {
88
89 if (name.startsWith(StringPool.SLASH)) {
90 name = name.substring(1);
91 }
92
93 if (inpuStream == null) {
94 return;
95 }
96
97 if (_log.isDebugEnabled()) {
98 _log.debug("Adding " + name);
99 }
100
101 FileUtil.mkdirs(getPath());
102
103 OutputStream outputStream = new FileOutputStream(
104 new File(getPath() + StringPool.SLASH + name));
105
106 try {
107 File.cat(inpuStream, outputStream);
108 }
109 finally {
110 outputStream.close();
111 }
112 }
113
114 public void addEntry(String name, String s) throws IOException {
115 addEntry(name, s.getBytes(StringPool.UTF8));
116 }
117
118 public void addEntry(String name, StringBuilder sb) throws IOException {
119 addEntry(name, sb.toString());
120 }
121
122 public byte[] finish() throws IOException {
123 UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
124 new UnsyncByteArrayOutputStream();
125
126 InputStream inputStream = new FileInputStream(_file);
127
128 try {
129 File.cat(inputStream, unsyncByteArrayOutputStream);
130 }
131 finally {
132 unsyncByteArrayOutputStream.close();
133 inputStream.close();
134 }
135
136 return unsyncByteArrayOutputStream.toByteArray();
137 }
138
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;
148 }
149
150 public String getPath() {
151 return _file.getPath();
152 }
153
154 private static final Log _log = LogFactoryUtil.getLog(ZipWriter.class);
155
156 private final File _file;
157
158 }