1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.tonicsystems.jarjar.util;
18
19 import java.util.jar.JarEntry;
20 import java.util.jar.JarFile;
21 import java.util.jar.JarOutputStream;
22 import java.util.Enumeration;
23 import java.io.*;
24 import java.util.*;
25
26 public class StandaloneJarProcessor
27 {
28 public static void run(File from, File to, JarProcessor proc) throws IOException {
29 run(from, to, proc, false);
30 }
31 public static void run(File from, File to, JarProcessor proc, boolean ignoreDuplicates) throws IOException {
32 byte[] buf = new byte[0x2000];
33
34 JarFile in = new JarFile(from);
35 final File tmpTo = File.createTempFile("jarjar", ".jar");
36 JarOutputStream out = new JarOutputStream(new FileOutputStream(tmpTo));
37 Set<String> entries = new HashSet<String>();
38 try {
39 EntryStruct struct = new EntryStruct();
40 Enumeration<JarEntry> e = in.entries();
41 while (e.hasMoreElements()) {
42 JarEntry entry = e.nextElement();
43 struct.name = entry.getName();
44 struct.time = entry.getTime();
45 ByteArrayOutputStream baos = new ByteArrayOutputStream();
46 IoUtil.pipe(in.getInputStream(entry), baos, buf);
47 struct.data = baos.toByteArray();
48 if (proc.process(struct)) {
49 if (entries.add(struct.name)) {
50 entry = new JarEntry(struct.name);
51 entry.setTime(struct.time);
52 entry.setCompressedSize(-1);
53 out.putNextEntry(entry);
54 out.write(struct.data);
55 } else if (struct.name.endsWith("/")) {
56
57 } else if (!ignoreDuplicates) {
58 throw new IllegalArgumentException("Duplicate jar entries: " + struct.name);
59 }
60 }
61 }
62
63 }
64 finally {
65 try {
66 in.close();
67 } catch (IOException e) {}
68 try {
69 out.close();
70 } catch (IOException e) {}
71 }
72
73
74 IoUtil.copyZipWithoutEmptyDirectories(tmpTo, to);
75 tmpTo.delete();
76
77 }
78 }