1
15
16 package gate.util;
17
18 import java.io.*;
19 import java.util.*;
20 import java.util.jar.*;
21
22
25 public class JarFiles {
26
27
28 private static final boolean DEBUG = false;
29 private StringBuffer dbgString = new StringBuffer();
30 private boolean warning = false;
31 String buggyJar = null;
32
33 private final static int BUFF_SIZE = 65000;
34
35 private Set directorySet = null;
36
37 private byte buffer[] = null;
38
39 public JarFiles(){
40 directorySet = new HashSet();
41 buffer = new byte[BUFF_SIZE];
42 }
43
44
51 public void merge(Set jarFileNames, String destinationJarName)
52 throws GateException {
53 String sourceJarName = null;
54 JarOutputStream jarFileDestination = null;
55 JarFile jarFileSource = null;
56
57 try {
58 jarFileDestination =
60 new JarOutputStream(new FileOutputStream(destinationJarName));
61
62 dbgString.append("Creating " + destinationJarName + " from these JARs:\n");
63 Iterator jarFileNamesIterator = jarFileNames.iterator();
65
66 while (jarFileNamesIterator.hasNext()) {
67 sourceJarName = (String) jarFileNamesIterator.next();
68
69 jarFileSource = new JarFile(sourceJarName);
71
72 addJar(jarFileDestination, jarFileSource);
75 if (jarFileSource.getName().equals(buggyJar))
76 dbgString.append(sourceJarName + "...problems occured ! \n");
77 else
78 dbgString.append(sourceJarName + "...added OK ! \n");
79 jarFileSource.close();
80 }
82 jarFileDestination.close();
83
84 } catch(IOException ioe) {
85 ioe.printStackTrace(Err.getPrintWriter());
86 }
88 if (warning == true)
89 Out.prln(dbgString);
90 }
92
93
101 private void addJar(JarOutputStream destinationJar, JarFile sourceJar)
102 throws GateException {
103 try {
104
105 Enumeration jarFileEntriesEnum = sourceJar.entries();
107
108 JarEntry currentJarEntry = null;
109 while (jarFileEntriesEnum.hasMoreElements()) {
110
111 currentJarEntry = (JarEntry) jarFileEntriesEnum.nextElement();
113
114 if(currentJarEntry.getName().equalsIgnoreCase("META-INF/") ||
116 currentJarEntry.getName().equalsIgnoreCase("META-INF/MANIFEST.MF"))
117 continue;
118
119 if( currentJarEntry.isDirectory() &&
122 directorySet.contains(currentJarEntry.getName())
123 ) continue;
124
125 try {
127 if (currentJarEntry.isDirectory())
130 directorySet.add(currentJarEntry.getName());
131
132 destinationJar.putNextEntry(new JarEntry(currentJarEntry.getName()));
134
135 InputStream currentEntryStream =
139 sourceJar.getInputStream(currentJarEntry);
140
141 int bytesRead = 0;
143 while((bytesRead = currentEntryStream.read(buffer,0,BUFF_SIZE)) != -1)
144 destinationJar.write(buffer,0,bytesRead);
145
146 currentEntryStream.close();
148
149 destinationJar.flush();
152
153 destinationJar.closeEntry();
158
159 } catch (java.util.zip.ZipException ze) {
160 if(!currentJarEntry.isDirectory()){
161 warning = true;
162 buggyJar = sourceJar.getName();
163 Out.prln("WARNING: Duplicate file entry " +
164 currentJarEntry.getName() + " (this file will be discarded)..." +
165 "It happened while adding " +
166 sourceJar.getName() + " !\n");
167 dbgString.append(currentJarEntry.getName() +" file from " +
168 sourceJar.getName() + " was discarded :( !\n");
169 } }
171 } } catch (java.io.IOException e) {
173 e.printStackTrace(Err.getPrintWriter());
174 }
176 }
178
184
185 public static void main(String[] args) {
186 if(args.length < 2) {
187 Err.println("USAGE : JarFiles arg0 arg1 ... argN" +
188 "(must be at least 2 args)");
189 } else {
191 JarFiles jarFiles = new JarFiles();
192 Set filesToMerge = new HashSet();
193 for (int i=1; i<args.length; i++) {
194 filesToMerge.add(args[i]);
195 }
196 try {
197 jarFiles.merge(filesToMerge, args[0]);
198 } catch (GateException ge) {
199 ge.printStackTrace(Err.getPrintWriter());
200 }
201 } }
204 }