01: /*
02: * The contents of this file are subject to the terms
03: * of the Common Development and Distribution License
04: * (the "License"). You may not use this file except
05: * in compliance with the License.
06: *
07: * You can obtain a copy of the license at
08: * https://jwsdp.dev.java.net/CDDLv1.0.html
09: * See the License for the specific language governing
10: * permissions and limitations under the License.
11: *
12: * When distributing Covered Code, include this CDDL
13: * HEADER in each file and include the License file at
14: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
15: * add the following below this CDDL HEADER, with the
16: * fields enclosed by brackets "[]" replaced with your
17: * own identifying information: Portions Copyright [yyyy]
18: * [name of copyright owner]
19: */
20: package com.sun.codemodel.writer;
21:
22: import java.io.FilterOutputStream;
23: import java.io.IOException;
24: import java.io.OutputStream;
25: import java.util.zip.ZipEntry;
26: import java.util.zip.ZipOutputStream;
27:
28: import com.sun.codemodel.CodeWriter;
29: import com.sun.codemodel.JPackage;
30:
31: /**
32: * Writes all the files into a zip file.
33: *
34: * @author
35: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
36: */
37: public class ZipCodeWriter extends CodeWriter {
38: /**
39: * @param target
40: * Zip file will be written to this stream.
41: */
42: public ZipCodeWriter(OutputStream target) {
43: zip = new ZipOutputStream(target);
44: // nullify the close method.
45: filter = new FilterOutputStream(zip) {
46: public void close() {
47: }
48: };
49: }
50:
51: private final ZipOutputStream zip;
52:
53: private final OutputStream filter;
54:
55: public OutputStream openBinary(JPackage pkg, String fileName)
56: throws IOException {
57: String name = fileName;
58: if (!pkg.isUnnamed())
59: name = toDirName(pkg) + name;
60:
61: zip.putNextEntry(new ZipEntry(name));
62: return filter;
63: }
64:
65: /** Converts a package name to the directory name. */
66: private static String toDirName(JPackage pkg) {
67: return pkg.name().replace('.', '/') + '/';
68: }
69:
70: public void close() throws IOException {
71: zip.close();
72: }
73:
74: }
|