01: package biz.hammurapi.web.mda;
02:
03: import java.io.IOException;
04: import java.io.OutputStream;
05: import java.io.OutputStreamWriter;
06: import java.io.StringWriter;
07: import java.io.Writer;
08: import java.util.Iterator;
09: import java.util.Map;
10: import java.util.zip.ZipEntry;
11: import java.util.zip.ZipOutputStream;
12:
13: /**
14: * Outputs channels to files.
15: * @author Pavel
16: */
17: public class ZipRootChannel extends MemoryChannelBase {
18:
19: /**
20: * Converts channel name to file name. By default returns channel name.
21: * @param channelName
22: * @return
23: */
24: protected String entryName(String channelName) {
25: return channelName;
26: }
27:
28: public void flush(OutputStream out) throws IOException {
29: ZipOutputStream zos = new ZipOutputStream(out);
30: Writer writer = new OutputStreamWriter(zos) {
31: public void close() throws IOException {
32: // Nothing - don't close master stream.
33: }
34: };
35: try {
36: Iterator it = getParts().entrySet().iterator();
37: while (it.hasNext()) {
38: Map.Entry entry = (Map.Entry) it.next();
39: ZipEntry zEntry = new ZipEntry(entryName(String
40: .valueOf(entry.getKey())));
41: zos.putNextEntry(zEntry);
42: if (entry.getValue() instanceof SubChannel) {
43: ((SubChannel) entry.getValue()).flush(writer);
44: } else if (entry.getValue() instanceof StringWriter) {
45: ((StringWriter) entry.getValue()).close();
46: writer.write(entry.getValue().toString());
47: }
48: }
49: } finally {
50: zos.close();
51: }
52: }
53: }
|