KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > common > JJarFile


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Eric HARDESTY
22  * --------------------------------------------------------------------------
23  * $Id: JJarFile.java,v 1.0
24  * --------------------------------------------------------------------------
25  */

26
27
28 package org.objectweb.jonas.common;
29
30 import java.io.File JavaDoc;
31 import java.io.FileOutputStream JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.util.jar.JarFile JavaDoc;
35 import java.util.jar.JarEntry JavaDoc;
36
37 /**
38  * This class implements a JarFile that adds a extract/write method
39  * @author Eric Hardesty
40  */

41 public class JJarFile extends JarFile JavaDoc {
42
43     /**
44      * Size of the buffer.
45      */

46     private static final int BUFFER_SIZE = 2048;
47
48     /**
49      * Constructs a new JarFile with the specified File.
50      * @param file the jar file to be opened
51      */

52     public JJarFile(File JavaDoc file) throws IOException JavaDoc {
53         super(file);
54     }
55
56     /**
57      * Constructs a new JarFile with the specified File.
58      * @param file the jar file to be opened
59      * @param verity boolean to verify if the JarFile is signed
60      */

61     public JJarFile(File JavaDoc file, boolean verify) throws IOException JavaDoc {
62         super(file, verify);
63     }
64
65     /**
66      * Constructs a new JarFile with the specified File.
67      * @param file the jar file to be opened
68      * @param verity boolean to verify if the JarFile is signed
69      * @param mode int of the mode to open the jar
70      */

71     public JJarFile(File JavaDoc file, boolean verify, int mode) throws IOException JavaDoc {
72         super(file, verify, mode);
73     }
74
75     /**
76      * Constructs a new JarFile with the specified File.
77      * @param name the name of the jar file to be opened
78      */

79     public JJarFile(String JavaDoc name) throws IOException JavaDoc {
80         super(name);
81     }
82
83     /**
84      * Constructs a new JarFile with the specified File.
85      * @param name the name of the jar file to be opened
86      * @param verity boolean to verify if the JarFile is signed
87      */

88     public JJarFile(String JavaDoc name, boolean verify) throws IOException JavaDoc {
89         super(name, verify);
90     }
91
92     /**
93      * Extract the specified Jar Entry to the path given
94      * @param jEnt the JarEntry to extract
95      * @param filename the filename to write the extracted file
96      */

97     public void extract(JarEntry JavaDoc jEnt, String JavaDoc filename) throws IOException JavaDoc {
98         
99         try {
100             //File output
101
FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(filename);
102             InputStream JavaDoc jis = this.getInputStream(jEnt);
103             int n = 0;
104             try {
105                 //buffer
106
byte buffer[] = new byte[BUFFER_SIZE];
107   
108                 while ((n = jis.read(buffer)) > 0) {
109                     out.write(buffer, 0, n);
110                 }
111             } finally {
112                 out.close();
113                 jis.close();
114             }
115         } catch (IOException JavaDoc e) {
116             throw new IOException JavaDoc("Error while uncompressing the file " + filename + ": " + e.getMessage());
117         }
118
119     }
120
121 }
122
Popular Tags