KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > common > JarUtil


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * JarUtil.java
20  *
21  * This class supplies methods to interact with a jar file.
22  */

23
24 // package path
25
package com.rift.coad.lib.common;
26
27 // java imports
28
import com.rift.coad.lib.common.*;
29 import java.io.InputStream JavaDoc;
30 import java.io.FileOutputStream JavaDoc;
31 import java.io.File JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.jar.JarFile JavaDoc;
34 import java.util.jar.JarEntry JavaDoc;
35
36 /**
37  * This class supplies methods to interact with a jar file.
38  *
39  * @author Brett Chaldecott
40  */

41 public class JarUtil {
42     
43     /**
44      * Private constructor to prevent instanciation of this object.
45      */

46     private JarUtil() {
47     }
48     
49     
50     /**
51      * This method extracts the contents of a source file to a destination file.
52      *
53      * @param source The source file to extract.
54      * @param destination The destination file to extract.
55      */

56     public static void extract (File JavaDoc source, File JavaDoc destination)
57             throws CommonException {
58         try {
59             JarFile JavaDoc jarFile = new JarFile JavaDoc(source);
60             Enumeration JavaDoc entries = jarFile.entries();
61             byte[] readBuffer = new byte[1024];
62             while (entries.hasMoreElements()) {
63                 JarEntry JavaDoc entry = (JarEntry JavaDoc)entries.nextElement();
64                 File JavaDoc path = new File JavaDoc(destination + File.separator
65                         + entry.getName());
66                 if (entry.isDirectory()) {
67                     if (!path.mkdirs()) {
68                         throw new CommonException("Failed to create the dir [" +
69                                 path.getAbsolutePath() + "]");
70                     }
71                     continue;
72                 }
73                 InputStream JavaDoc inputStream =
74                         jarFile.getInputStream(entry);
75                 FileOutputStream JavaDoc outputStream = new FileOutputStream JavaDoc(path);
76                 int readBytes = 0;
77                 while((readBytes =
78                         inputStream.read(readBuffer,0,readBuffer.length)) != -1) {
79                     outputStream.write(readBuffer,0,readBytes);
80                 }
81                 outputStream.flush();
82                 inputStream.close();
83                 outputStream.close();
84             }
85         } catch (Exception JavaDoc ex) {
86             throw new CommonException("Failed to extract the file [" +
87                     source.getAbsolutePath() + "] to [" +
88                     destination.getAbsolutePath() + "] because :" +
89                     ex.getMessage(),ex);
90         }
91     }
92 }
93
Popular Tags