1 34 package spjar; 35 36 import java.io.*; 37 import java.util.Enumeration ; 38 import java.util.zip.*; 39 import java.util.jar.*; 40 41 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 } 65 66 private Spjar() { 67 } 68 69 public static void main(String [] argv) { 70 Spjar spj = new Spjar(); 71 String jarFile = null; 72 73 for (int i=0; i < argv.length; i++) { 75 if (argv[i].charAt(0) == '-') { 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 e) { 95 System.err.println(e); 96 } 97 } 98 99 private void procJarFile(String jarfn) throws IOException { 100 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 boolean hasResource = false; 110 { 111 Enumeration en = jarin.entries(); 112 while (en.hasMoreElements()) { 113 ZipEntry et = (ZipEntry)en.nextElement(); 114 String etname = et.getName(); 115 if (isResource(etname)) { 116 hasResource = true; 117 break; 118 } 119 } 123 } 124 if (!hasResource) { 125 System.out.println("Resource not found : " + jarfn); 126 return; 127 } 128 129 String 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 String 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 procSplit(jarin, jaroutc, jaroutr, jarfn3); 149 150 jarin.close(); 152 jaroutc.close(); 153 jaroutr.close(); 154 155 return; 156 } 157 158 private void procSplit(JarFile jarin, JarOutputStream jaroutc, JarOutputStream jaroutr, String jaroutrfn) throws IOException { 159 Enumeration en = jarin.entries(); 160 while (en.hasMoreElements()) { 161 ZipEntry ze = (ZipEntry)en.nextElement(); 162 String 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 jaroutfn) throws IOException { 179 String filename = zein.getName(); 180 byte[] buf = inJarEntry2(jarin, zein); 181 String str = new String (buf, "UTF-8"); 182 183 boolean hasClassPath = false; 184 java.util.StringTokenizer st = new java.util.StringTokenizer (str, "\n"); 185 java.util.ArrayList ls = new java.util.ArrayList (); 186 while (st.hasMoreTokens()) { 187 String 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 sb = new StringBuffer (); 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 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 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 filename, byte[] buf, int mtn) throws IOException { 218 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 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 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 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 name) { 268 int cmp = 0; 269 270 if (sz != 0) { 271 cmp = (int)((100L * csz) / sz); 272 } 273 274 String 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 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 |