View Javadoc

1   /**
2    * Copyright 2007 Google Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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                          // TODO(chrisn): log
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           // delete the empty directories
74          IoUtil.copyZipWithoutEmptyDirectories(tmpTo, to);
75          tmpTo.delete();
76  
77      }
78  }