1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.geronimo.ews.ws4j2ee.utils.packager;
18
19 import java.io.BufferedOutputStream;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Enumeration;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.jar.JarFile;
29 import java.util.jar.JarOutputStream;
30 import java.util.zip.ZipEntry;
31
32 /***
33 * @author hemapani@opensource.lk
34 */
35 public ModulePackager {/package-summary.html">class ModulePackager {
36 private JarOutputStream jarFile;
37 private byte[] buf = new byte[1024];
38 private HashMap entries = new HashMap();
39
40 public ModulePackager(File outjarName) throws IOException {/package-summary.html">ong> ModulePackager(File outjarName) throws IOException {
41 BufferedOutputStream bo =
42 new BufferedOutputStream(new FileOutputStream(outjarName));
43 jarFile = new JarOutputStream(bo);
44 }
45
46 public void addJarFile(File file) throws IOException {
47 JarFile infile = new JarFile(file);
48 Enumeration enu = infile.entries();
49 while (enu.hasMoreElements()) {
50 ZipEntry zipentry = (ZipEntry) enu.nextElement();
51 InputStream in = infile.getInputStream(zipentry);
52 addFileToJar(zipentry.getName(), in);
53 }
54 }
55
56 public void addClassFiles(File dir) throws IOException {
57 addClassFiles(dir, null);
58 }
59
60 private void addClassFiles(File file, String path) throws IOException {
61 if (file.isDirectory()) {
62 if (path == null) {
63 path = "";
64 } else if ("".equals(path)) {
65 path = file.getName();
66 } else {
67 path = path + "/" + file.getName();
68 }
69 File[] files = file.listFiles();
70 if (files != null) {
71 for (int i = 0; i < files.length; i++) {
72 addClassFiles(files[i], path);
73 }
74 }
75 } else {
76 path = path + "/" + file.getName();
77 addFileToJar(path, new FileInputStream(file));
78 }
79 }
80
81 public void addFileToJar(String path, File input) throws IOException {
82 addFileToJar(path, new FileInputStream(input));
83 }
84
85 private void addFileToJar(String entry, InputStream instream) throws IOException {
86 if (entries.containsKey(entry)) {
87 entries.remove(entry);
88 }
89 entries.put(entry, instream);
90 }
91
92 public void finalizeJar() throws IOException {
93 Iterator jarentries = entries.keySet().iterator();
94 while (jarentries.hasNext()) {
95 String entryName = (String) jarentries.next();
96 ZipEntry entry = new ZipEntry(entryName);
97 InputStream instream = (InputStream) entries.get(entryName);
98 jarFile.putNextEntry(entry);
99 int anz;
100 while ((anz = instream.read(buf)) != -1) {
101 jarFile.write(buf, 0, anz);
102 }
103 jarFile.flush();
104 jarFile.closeEntry();
105 instream.close();
106 }
107 jarFile.close();
108 }
109
110 }