KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > util > JarUtil


1 /*
2  * Created by IntelliJ IDEA.
3  * User: leo
4  * Date: Jul 11, 2002
5  * Time: 4:32:15 PM
6  * To change template for new class use
7  * Code Style | Class Templates options (Tools | IDE Options).
8  */

9 package org.snipsnap.util;
10
11 import org.radeox.util.logging.Logger;
12
13 import java.io.*;
14 import java.util.Enumeration JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.jar.JarEntry JavaDoc;
17 import java.util.jar.JarFile JavaDoc;
18 import java.util.zip.Adler32 JavaDoc;
19 import java.util.zip.CheckedInputStream JavaDoc;
20
21 public class JarUtil {
22
23   public static Checksum extract(JarFile JavaDoc file, File JavaDoc target) throws IOException {
24     return extract(file, target, null, null);
25   }
26
27   /**
28    * Extract jar file and return a map of the checksummed files extracted.
29    * @param file the jar file to extract
30    * @param target the target directory (will be created if it does not exist)
31    * @return a map of the checksums
32    * @throws IOException
33    */

34   public static Checksum extract(JarFile JavaDoc file, File JavaDoc target, List JavaDoc install, List JavaDoc unpack) throws IOException {
35     if (!target.exists()) {
36       target.mkdirs();
37     }
38
39     Checksum checksum = new Checksum(target.getAbsolutePath());
40     if (target.isDirectory()) {
41       byte buf[] = new byte[8192];
42       Enumeration JavaDoc entries = file.entries();
43       while (entries.hasMoreElements()) {
44         JarEntry JavaDoc entry = (JarEntry JavaDoc) entries.nextElement();
45         File JavaDoc f = new File JavaDoc(target, entry.getName());
46         if (entry.isDirectory()) {
47           f.mkdir();
48         } else {
49           boolean extract = true, rename = false;
50           String JavaDoc name = entry.toString();
51           if (install != null && !install.contains(name)) {
52             extract = false;
53           }
54           if (unpack != null && unpack.contains(name)) {
55             extract = true;
56             rename = true;
57           }
58
59           if (extract) {
60             System.out.println("Extracting " + f.getAbsolutePath());
61             CheckedInputStream JavaDoc fin = new CheckedInputStream JavaDoc(new BufferedInputStream(file.getInputStream(entry)),
62                 new Adler32 JavaDoc());
63             if (rename) {
64               f = new File JavaDoc(target, entry.getName() + ".new");
65               System.out.println("Renaming to " + f.getAbsolutePath());
66             }
67             BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(f));
68             int n = 0;
69             while ((n = fin.read(buf)) >= 0) {
70               fout.write(buf, 0, n);
71             }
72             checksum.add(entry.toString(), new Long JavaDoc(fin.getChecksum().getValue()));
73             fin.close();
74             fout.close();
75           }
76         }
77       }
78     }
79
80     return checksum;
81   }
82
83   public static Checksum checksumJar(JarFile JavaDoc file) throws IOException {
84     Checksum checksum = new Checksum(file.toString());
85     Enumeration JavaDoc entries = file.entries();
86     while (entries.hasMoreElements()) {
87       JarEntry JavaDoc entry = (JarEntry JavaDoc) entries.nextElement();
88       if (!entry.isDirectory()) {
89         CheckedInputStream JavaDoc fin = new CheckedInputStream JavaDoc(new BufferedInputStream(file.getInputStream(entry)),
90             new Adler32 JavaDoc());
91         byte buffer[] = new byte[8192];
92         while ((fin.read(buffer)) != -1) {
93           /* ignore ... */
94         }
95         Long JavaDoc checkSum = new Long JavaDoc(fin.getChecksum().getValue());
96         checksum.add(entry.toString(), checkSum);
97         fin.close();
98       }
99     }
100     return checksum;
101   }
102
103   public static void main(String JavaDoc args[]) {
104     try {
105       JarFile JavaDoc file = new JarFile JavaDoc(args[0]);
106       Checksum checksum = checksumJar(file);
107       checksum.store(new File JavaDoc("./CHECKSUMS"));
108     } catch (IOException e) {
109       Logger.warn("JarUtil: usage: JarUtil jarfile", e);
110     }
111   }
112 }
113
Popular Tags