KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > server > util > UnpackWar


1 package org.enhydra.server.util;
2
3 /**
4  * <p>Title: </p>
5  * <p>Description: Util class for unpack WAR file to destination directory</p>
6  * <p>Copyright: Copyright (c) 2002</p>
7  * <p>Company: </p>
8  * @author Damir Milovic
9  * @version 1.0
10  */

11
12 import java.io.File JavaDoc;
13 import java.io.FileInputStream JavaDoc;
14 import java.io.FileNotFoundException JavaDoc;
15 import java.io.FileOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.util.zip.ZipEntry JavaDoc;
19 import java.util.zip.ZipInputStream JavaDoc;
20
21 import org.enhydra.server.EnhydraServerException;
22
23 public class UnpackWar {
24
25     /**
26      * If this constructor is used must call <code>setWar()</code> and
27      * <code>setDestDir()</code> methods.
28      */

29     public UnpackWar() {
30     }
31
32     /**
33      * Default constructor
34      * @param warPath path to war file
35      * @param destDirPath path to destination directory for unpack files
36      */

37     public UnpackWar(String JavaDoc warPath, String JavaDoc destDirPath) {
38         setWar(new File JavaDoc(warPath));
39         setDestDir(new File JavaDoc(destDirPath));
40     }
41
42     private File JavaDoc dest;
43
44     private File JavaDoc source;
45
46     /**
47      * Unpack war to destination directory.
48      */

49     public void execute() throws EnhydraServerException{
50         expandFile(source, dest);
51     }
52
53     private void expandFile(File JavaDoc srcF, File JavaDoc dir) throws EnhydraServerException{
54         log("Expanding: " + srcF + " into " + dir);
55         ZipInputStream JavaDoc zis = null;
56         try {
57
58             zis = new ZipInputStream JavaDoc(new FileInputStream JavaDoc(srcF));
59             ZipEntry JavaDoc ze = null;
60
61             while ((ze = zis.getNextEntry()) != null) {
62                 extractFile(srcF, dir, zis,
63                             ze.getName(),
64                             ze.isDirectory());
65             }
66
67             log("expand complete");
68         } catch (IOException JavaDoc ioe) {
69             throw new EnhydraServerException("Error while expanding " + srcF.getPath(),
70                     ioe);
71         } finally {
72             if (zis != null) {
73                 try {
74                     zis.close();
75                     } catch (IOException JavaDoc e) {}
76             }
77         }
78     }
79
80     private void extractFile(File JavaDoc srcF, File JavaDoc dir,
81                                InputStream JavaDoc compressedInputStream,
82                                String JavaDoc entryName,
83                                boolean isDirectory)
84             throws IOException JavaDoc {
85
86         String JavaDoc filePath = PathUtil.makeAbsolutePath(dir.getAbsolutePath(), entryName);
87         File JavaDoc f = new File JavaDoc(filePath);
88         try {
89             log("expanding " + entryName + " to " + f);
90             // create intermediary directories - sometimes zip don't add them
91
File JavaDoc dirF = f.getParentFile();
92             if ( dirF != null ) {
93                 dirF.mkdirs();
94             }
95
96             if (isDirectory) {
97                 f.mkdirs();
98             } else {
99                 byte[] buffer = new byte[1024];
100                 int length = 0;
101                 FileOutputStream JavaDoc fos = null;
102                 try {
103                     fos = new FileOutputStream JavaDoc(f);
104
105                     while ((length =
106                             compressedInputStream.read(buffer)) >= 0) {
107                         fos.write(buffer, 0, length);
108                     }
109
110                     fos.close();
111                     fos = null;
112                 } finally {
113                     if (fos != null) {
114                         try {
115                             fos.close();
116                             } catch (IOException JavaDoc e) {}
117                     }
118                 }
119             }
120
121
122         } catch (FileNotFoundException JavaDoc ex) {
123             log("Unable to expand to file " + f.getPath());
124         }
125
126     }
127
128     /**
129      * Set the destination directory. File will be unzipped into the
130      * destination directory.
131      *
132      * @param d Path to the directory.
133      */

134     public void setDestDir(File JavaDoc d) {
135         this.dest = d;
136     }
137
138     /**
139      * Set the path to War-file.
140      *
141      * @param s Path to war-file.
142      */

143     public void setWar(File JavaDoc s) {
144         this.source = s;
145     }
146
147 // todo: set logger
148
private void log(String JavaDoc message){
149         System.out.println(message);
150     }
151 //For Test only
152
public static void main(String JavaDoc[] args){
153         UnpackWar test = new UnpackWar("c:/Temp/testwar/testKelpDist.war", "c:/Temp/testwar/webapps/test");
154         try{
155             test.execute();
156         }catch(Exception JavaDoc e){
157             e.printStackTrace();
158         }
159     }
160 }
Popular Tags