KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spjar > Spjar


1 /* ====================================================================
2  * Copyright (c) 2002, Hidetoshi Ohuchi <hchacha@users.sourceforge.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of the hchacha nor the names of its contributors
17  * may be used to endorse or promote products derived from this
18  * software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  * ====================================================================
33  */

34 package spjar;
35
36 import java.io.*;
37 import java.util.Enumeration JavaDoc;
38 import java.util.zip.*;
39 import java.util.jar.*;
40
41 /**
42  * The Main class of 'split a jar file into class and resource' (spjar) .
43  *
44  * Copyright (c) 2002,
45  * <A HREF="mailto:Hidetoshi Ohuchi <hchacha@users.sourceforge.net>">
46  * Hidetoshi Ohuchi &lt;hchacha@users.sourceforge.net&gt;</A><BR>
47  *
48  * @version 0.9.1 - $Id: Spjar.java,v 1.1 2002/03/31 12:10:27 hchacha Exp $
49  * @author Hidetoshi Ohuchi &lt;hchacha@users.sourceforge.net&gt;
50  */

51 public class Spjar {
52    boolean isVerbose = false;
53
54    private static void printVersion() {
55       System.out.println("spjar - split a jar file into class and resource - ver 0.9.1");
56    }
57
58    private static void printUsage() {
59       printVersion();
60       System.out.println("[usage] java -jar spjar.jar xxxx.jar");
61       System.out.println(" options");
62       System.out.println(" -verbose : verbose mode");
63 // System.out.println(" -");
64
}
65
66    private Spjar() {
67    }
68
69    public static void main(String JavaDoc[] argv) {
70       Spjar spj = new Spjar();
71       String JavaDoc jarFile = null;
72
73       // Parse command line arguments.
74
for (int i=0; i < argv.length; i++) {
75          if (argv[i].charAt(0) == '-') { // command line switch
76
if(argv[i].equals("-verbose")) {
77                spj.isVerbose = false;
78             } else {
79                System.err.println("Unknown switch " + argv[i] + " ignored.");
80             }
81          } else if(argv[i].endsWith(".jar")) {
82             jarFile = argv[i];
83          }
84       }
85
86       try {
87          if (jarFile == null) {
88             printUsage();
89          } else {
90             spj.procJarFile(jarFile);
91          }
92       } catch(IOException e) {
93          System.err.println(e.getMessage());
94       } catch(Exception JavaDoc e) {
95          System.err.println(e);
96       }
97    }
98
99    private void procJarFile(String JavaDoc jarfn) throws IOException {
100       // in file
101
File jarf = new File(jarfn);
102       if (!jarf.exists()) {
103          System.err.println("File not found : " + jarfn);
104          return;
105       }
106       JarFile jarin = new JarFile(jarf);
107
108       // check resource
109
boolean hasResource = false;
110       {
111          Enumeration JavaDoc en = jarin.entries();
112          while (en.hasMoreElements()) {
113             ZipEntry et = (ZipEntry)en.nextElement();
114             String JavaDoc etname = et.getName();
115             if (isResource(etname)) {
116                hasResource = true;
117                break;
118             }
119 // long sz = et.getSize();
120
// long csz = et.getCompressedSize();
121
// printEntryLine(sz, csz, etname);
122
}
123       }
124       if (!hasResource) {
125          System.out.println("Resource not found : " + jarfn);
126          return;
127       }
128
129       // out file for class
130
String JavaDoc jarfn2 = jarfn.substring(0, jarfn.length()-4) + "_c.jar";
131       File jarfoutc = new File(jarfn2);
132       if (jarfoutc.exists()) {
133          jarfoutc.delete();
134       }
135       FileOutputStream foutc = new FileOutputStream(jarfoutc);
136       JarOutputStream jaroutc = new JarOutputStream(foutc);
137
138       // out file for resource
139
String JavaDoc jarfn3 = jarfn.substring(0, jarfn.length()-4) + "_r.jar";
140       File jarfoutr = new File(jarfn3);
141       if (jarfoutr.exists()) {
142          jarfoutr.delete();
143       }
144       FileOutputStream foutr = new FileOutputStream(jarfoutr);
145       JarOutputStream jaroutr = new JarOutputStream(foutr);
146
147       //
148
procSplit(jarin, jaroutc, jaroutr, jarfn3);
149
150       //
151
jarin.close();
152       jaroutc.close();
153       jaroutr.close();
154
155       return;
156    }
157
158    private void procSplit(JarFile jarin, JarOutputStream jaroutc, JarOutputStream jaroutr, String JavaDoc jaroutrfn) throws IOException {
159       Enumeration JavaDoc en = jarin.entries();
160       while (en.hasMoreElements()) {
161          ZipEntry ze = (ZipEntry)en.nextElement();
162          String JavaDoc zename = ze.getName();
163          if (zename.equals("META-INF/MANIFEST.MF")) {
164             copyManifest(jarin, ze, jaroutc, jaroutrfn);
165          } else if (isResource(zename)) {
166             copyEntry(jarin, ze, jaroutr);
167          } else {
168             copyEntry(jarin, ze, jaroutc);
169          }
170          if (this.isVerbose) {
171             long sz = ze.getSize();
172             long csz = ze.getCompressedSize();
173             printEntryLine(sz, csz, zename);
174          }
175       }
176    }
177
178    private void copyManifest(JarFile jarin, ZipEntry zein, JarOutputStream jarout, String JavaDoc jaroutfn) throws IOException {
179       String JavaDoc filename = zein.getName();
180       byte[] buf = inJarEntry2(jarin, zein);
181       String JavaDoc str = new String JavaDoc(buf, "UTF-8");
182
183       boolean hasClassPath = false;
184       java.util.StringTokenizer JavaDoc st = new java.util.StringTokenizer JavaDoc(str, "\n");
185       java.util.ArrayList JavaDoc ls = new java.util.ArrayList JavaDoc();
186       while (st.hasMoreTokens()) {
187          String JavaDoc tok = st.nextToken();
188          if (tok.startsWith("Class-Path: ")) {
189             tok = tok + " " + jaroutfn;
190             hasClassPath = true;
191          }
192          ls.add(tok);
193       }
194       st = null;
195
196       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
197       if (!hasClassPath) {
198          sb.append("Class-Path: ");
199          sb.append(jaroutfn);
200          sb.append("\n");
201       }
202       for (int i=0; i<ls.size(); i++) {
203          sb.append(ls.get(i));
204          sb.append("\n");
205       }
206       String JavaDoc stro = sb.toString();
207
208       outEntry(jarout, filename, stro.getBytes("UTF-8"), zein.getMethod());
209    }
210
211    private void copyEntry(JarFile jarin, ZipEntry zein, JarOutputStream jarout) throws IOException {
212       String JavaDoc filename = zein.getName();
213       byte[] buf = inJarEntry2(jarin, zein);
214       outEntry(jarout, filename, buf, zein.getMethod());
215    }
216
217    private void outEntry(JarOutputStream jarout, String JavaDoc filename, byte[] buf, int mtn) throws IOException {
218       // write a entry
219
ZipEntry ze = new ZipEntry(filename);
220       CRC32 crc32 = new CRC32();
221
222       ze.setMethod(mtn);
223       ze.setCompressedSize(-1);
224       if (buf == null) {
225          ze.setSize(0);
226          ze.setCrc(0);
227       } else {
228          ze.setSize(buf.length);
229          crc32.reset();
230          crc32.update(buf);
231          ze.setCrc(crc32.getValue());
232       }
233       jarout.putNextEntry(ze);
234
235       // write a entry data
236
if (buf != null) {
237          jarout.setMethod(mtn);
238          jarout.write(buf);
239       }
240       jarout.closeEntry();
241    }
242
243    private byte[] inJarEntry2(JarFile jarin, ZipEntry ze) throws IOException {
244       long sz = ze.getSize();
245
246       // read a entry data
247
byte[] buf = new byte[(int)sz];
248       InputStream zein = jarin.getInputStream(ze);
249       DataInputStream din = new DataInputStream(zein);
250       din.readFully(buf);
251       din.close();
252       zein.close();
253
254       return buf;
255    }
256
257    private static boolean isResource(String JavaDoc path) {
258       if (path.startsWith("META-INF/")) {
259       } else if (path.endsWith("/")) {
260       } else if (path.endsWith(".class")) {
261       } else {
262          return true;
263       }
264       return false;
265    }
266
267    private static void printEntryLine(long sz, long csz, String JavaDoc name) {
268       int cmp = 0;
269
270       if (sz != 0) {
271          cmp = (int)((100L * csz) / sz);
272       }
273
274       String JavaDoc s = Long.toString(sz);
275       for (int i=s.length(); i<8; i++) {
276          System.out.print(' ');
277       }
278       System.out.print(sz);
279       System.out.print(' ');
280
281       String JavaDoc cs = Integer.toString(cmp);
282       for (int i=cs.length(); i<3; i++) {
283          System.out.print(' ');
284       }
285       System.out.print(cs);
286       System.out.print('%');
287       System.out.print(' ');
288
289       System.out.println(name);
290    }
291 }
292
Popular Tags