1 9 package org.snipsnap.util; 10 11 import org.radeox.util.logging.Logger; 12 13 import java.io.*; 14 import java.util.Enumeration ; 15 import java.util.List ; 16 import java.util.jar.JarEntry ; 17 import java.util.jar.JarFile ; 18 import java.util.zip.Adler32 ; 19 import java.util.zip.CheckedInputStream ; 20 21 public class JarUtil { 22 23 public static Checksum extract(JarFile file, File target) throws IOException { 24 return extract(file, target, null, null); 25 } 26 27 34 public static Checksum extract(JarFile file, File target, List install, List 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 entries = file.entries(); 43 while (entries.hasMoreElements()) { 44 JarEntry entry = (JarEntry ) entries.nextElement(); 45 File f = new File (target, entry.getName()); 46 if (entry.isDirectory()) { 47 f.mkdir(); 48 } else { 49 boolean extract = true, rename = false; 50 String 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 fin = new CheckedInputStream (new BufferedInputStream(file.getInputStream(entry)), 62 new Adler32 ()); 63 if (rename) { 64 f = new File (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 (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 file) throws IOException { 84 Checksum checksum = new Checksum(file.toString()); 85 Enumeration entries = file.entries(); 86 while (entries.hasMoreElements()) { 87 JarEntry entry = (JarEntry ) entries.nextElement(); 88 if (!entry.isDirectory()) { 89 CheckedInputStream fin = new CheckedInputStream (new BufferedInputStream(file.getInputStream(entry)), 90 new Adler32 ()); 91 byte buffer[] = new byte[8192]; 92 while ((fin.read(buffer)) != -1) { 93 94 } 95 Long checkSum = new Long (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 args[]) { 104 try { 105 JarFile file = new JarFile (args[0]); 106 Checksum checksum = checksumJar(file); 107 checksum.store(new File ("./CHECKSUMS")); 108 } catch (IOException e) { 109 Logger.warn("JarUtil: usage: JarUtil jarfile", e); 110 } 111 } 112 } 113 | Popular Tags |