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