KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > util > JarUtils


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.ioc.util;
8
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.FileOutputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.OutputStream JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.net.URLClassLoader JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.StringTokenizer JavaDoc;
19 import java.util.jar.Attributes JavaDoc;
20 import java.util.jar.JarFile JavaDoc;
21 import java.util.jar.JarInputStream JavaDoc;
22 import java.util.jar.Manifest JavaDoc;
23 import java.util.zip.ZipEntry JavaDoc;
24
25 /**
26  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
27  */

28
29 public class JarUtils {
30
31     /**
32      * Dump the contents of a JarArchive to the dpecified destination.
33      */

34     public static void extract(File JavaDoc jarFile, File JavaDoc dest) throws IOException JavaDoc {
35         if(!dest.exists()) {
36             dest.mkdirs();
37         }
38         if(!dest.isDirectory()) {
39             throw new IOException JavaDoc("Destination must be a directory.");
40         }
41         JarInputStream JavaDoc jin = new JarInputStream JavaDoc(new FileInputStream JavaDoc(jarFile));
42         byte[] buffer = new byte[1024];
43
44         ZipEntry JavaDoc entry = jin.getNextEntry();
45         while(entry != null) {
46             String JavaDoc fileName = entry.getName();
47             if(fileName.charAt(fileName.length() - 1) == '/') {
48                 fileName = fileName.substring(0, fileName.length() - 1);
49             }
50             if(fileName.charAt(0) == '/') {
51                 fileName = fileName.substring(1);
52             }
53             if(File.separatorChar != '/') {
54                 fileName = fileName.replace('/', File.separatorChar);
55             }
56             File JavaDoc file = new File JavaDoc(dest, fileName);
57             if(entry.isDirectory()) {
58                 // make sure the directory exists
59
file.mkdirs();
60                 jin.closeEntry();
61             }
62             else {
63                 // make sure the directory exists
64
File JavaDoc parent = file.getParentFile();
65                 if(parent != null && !parent.exists()) {
66                     parent.mkdirs();
67                 }
68
69                 // dump the file
70
OutputStream JavaDoc out = new FileOutputStream JavaDoc(file);
71                 int len = 0;
72                 while((len = jin.read(buffer, 0, buffer.length)) != -1) {
73                     out.write(buffer, 0, len);
74                 }
75                 out.flush();
76                 out.close();
77                 jin.closeEntry();
78                 file.setLastModified(entry.getTime());
79             }
80             entry = jin.getNextEntry();
81         }
82         /* Explicity write out the META-INF/MANIFEST.MF so that any headers such
83         as the Class-Path are see for the unpackaged jar
84         */

85         Manifest JavaDoc mf = jin.getManifest();
86         if(mf != null) {
87             File JavaDoc file = new File JavaDoc(dest, "META-INF/MANIFEST.MF");
88             File JavaDoc parent = file.getParentFile();
89             if(parent.exists() == false) {
90                 parent.mkdirs();
91             }
92             OutputStream JavaDoc out = new FileOutputStream JavaDoc(file);
93             mf.write(out);
94             out.flush();
95             out.close();
96         }
97         jin.close();
98     }
99
100     /**
101      * use URLClassLoader.findResource, this method will not find resource use parent
102      *
103      * @param loader
104      * @param descriptorName
105      * @return null if no such descriptor
106      */

107     public static URL JavaDoc getDescriptor(URLClassLoader JavaDoc loader, String JavaDoc descriptorName) {
108         return loader.findResource(descriptorName);
109     }
110
111     public static Manifest JavaDoc getManifest(File JavaDoc jarFile) throws IOException JavaDoc {
112         return new JarFile JavaDoc(jarFile).getManifest();
113     }
114
115     public static String JavaDoc[] getClassPathFromManifest(File JavaDoc jarFile) throws IOException JavaDoc {
116         Manifest JavaDoc mani = getManifest(jarFile);
117         Attributes JavaDoc attrs = mani.getMainAttributes();
118         String JavaDoc cpString = attrs.getValue("Class-Path");
119         if(cpString == null || cpString.trim().equals("")) {
120             return new String JavaDoc[0];
121         }
122         else {
123             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(cpString);
124             List JavaDoc cpList = new ArrayList JavaDoc();
125             while(st.hasMoreTokens()) {
126                 cpList.add(st.nextToken());
127             }
128             return (String JavaDoc[]) cpList.toArray(new String JavaDoc[cpList.size()]);
129         }
130     }
131
132     public static void main(String JavaDoc[] args) {
133
134     }
135 }
136
137
138
139
Popular Tags