KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > util > JarExtract


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.tools.admingui.util;
24
25 import java.util.jar.JarInputStream JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.util.zip.ZipEntry JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.OutputStream JavaDoc;
32
33 public class JarExtract {
34     
35     public static void extract(String JavaDoc fileName, String JavaDoc dir) throws IOException JavaDoc {
36         if(fileName == null || dir == null ) {
37             //localize this message
38
throw new IOException JavaDoc("extract method: dir, or fileName is null");
39         }
40         File JavaDoc f = new File JavaDoc(fileName);
41         if(!f.exists()) {
42             //localize this message.
43
throw new IOException JavaDoc(fileName + ": file not found");
44         }
45         FileInputStream JavaDoc fin = new FileInputStream JavaDoc(f);
46         JarInputStream JavaDoc jin = new JarInputStream JavaDoc(fin);
47          ZipEntry JavaDoc e;
48         
49          while((e =jin.getNextEntry()) != null ) {
50             extract(jin, e, dir);
51         }
52         
53     }
54     
55     private static void extract(JarInputStream JavaDoc jin, ZipEntry JavaDoc e, String JavaDoc dir)
56         throws IOException JavaDoc {
57         
58         File JavaDoc f = new File JavaDoc(dir + File.separatorChar + e.getName().replace('/', File.separatorChar));
59     if (e.isDirectory()) {
60         if (!f.exists() && !f.mkdirs() || !f.isDirectory()) {
61                 //localize this mesg.
62
throw new IOException JavaDoc(f + ": could not create directory");
63         }
64     } else {
65         if (f.getParent() != null) {
66         File JavaDoc d = new File JavaDoc(f.getParent());
67         if (!d.exists() && !d.mkdirs() || !d.isDirectory()) {
68                     //localize this mesg.
69
throw new IOException JavaDoc(d + ": could not create directory");
70         }
71         }
72         OutputStream JavaDoc os = new FileOutputStream JavaDoc(f);
73         byte[] b = new byte[512];
74         int len;
75         while ((len = jin.read(b, 0, b.length)) != -1) {
76         os.write(b, 0, len);
77         }
78         jin.closeEntry();
79         os.close();
80     }
81     
82     }
83     
84 }
85
Popular Tags