KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > ant > Explode


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.common.ant;
10
11 import java.io.File JavaDoc;
12 import java.io.FileInputStream JavaDoc;
13 import java.io.FileNotFoundException JavaDoc;
14 import java.io.FileOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.util.Arrays JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Set JavaDoc;
20 import java.util.zip.ZipEntry JavaDoc;
21 import java.util.zip.ZipException JavaDoc;
22 import java.util.zip.ZipInputStream JavaDoc;
23
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.Task;
26
27 /**
28  * Ant task that explode an archive.
29  *
30  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
31  * @version $Revision: 1.2 $
32  */

33 public class Explode extends Task
34 {
35
36    /** Unzipped extensions. */
37    public static Set JavaDoc extensions = new HashSet JavaDoc(Arrays.asList(new String JavaDoc[]{"ear","war","sar","har"}));
38
39    /** The exploded file. */
40    private File JavaDoc file;
41
42    /** The target directory. */
43    private File JavaDoc todir;
44
45    /** The target optional name. */
46    private String JavaDoc name;
47
48    public void setFile(File JavaDoc file)
49    {
50       this.file = file;
51    }
52
53    public void setTodir(File JavaDoc todir)
54    {
55       this.todir = todir;
56    }
57
58    public void setName(String JavaDoc name)
59    {
60       this.name = name;
61    }
62
63    public void execute() throws BuildException
64    {
65       try
66       {
67          explode(file, todir);
68       }
69       catch (DirException e)
70       {
71          throw new BuildException(e.getMessage());
72       }
73    }
74
75    public void explode(File JavaDoc file, File JavaDoc todir) throws BuildException, DirException
76    {
77       if (!file.exists())
78       {
79          throw new BuildException("source file does not exists");
80       }
81       if (!file.isFile())
82       {
83          throw new BuildException("source file is not file");
84       }
85       if (name == null)
86       {
87          name = file.getName();
88       }
89       InputStream JavaDoc in = null;
90       try
91       {
92          in = new FileInputStream JavaDoc(file);
93          ZipInputStream JavaDoc zip = new ZipInputStream JavaDoc(in);
94          log("Process archive " + name);
95          explode(this, name, zip, todir);
96       }
97       catch (FileNotFoundException JavaDoc e)
98       {
99          throw new BuildException("Unexpected error " + e.getMessage());
100       }
101       finally
102       {
103          if (in != null)
104          {
105             try
106             {
107                in.close();
108             }
109             catch (IOException JavaDoc ignored)
110             {
111             }
112          }
113       }
114    }
115
116    /**
117     * Explode a zip stream into a directory.
118     *
119     * @param explode used to log
120     * @param name the name of the created directory
121     * @param zip the zip stream will not be closed
122     * @param todir the parent directory
123     * @throws BuildException
124     * @throws DirException
125     */

126    public static void explode(Explode explode, String JavaDoc name, ZipInputStream JavaDoc zip, File JavaDoc todir) throws BuildException, DirException
127    {
128       // First ensure the target directory exists
129
if (!todir.exists())
130       {
131          throw new BuildException("target dir does not exists");
132       }
133       if (!todir.isDirectory())
134       {
135          throw new BuildException("target dir is not a directory");
136       }
137       try
138       {
139          // Buffer
140
byte[] buffer = new byte[512];
141
142          // The real target dir
143
todir = new File JavaDoc(todir, name);
144
145          // Get the directory
146
ensureDirExist(explode, todir);
147
148          // Process each file
149
for (ZipEntry JavaDoc entry = zip.getNextEntry();entry != null;entry = zip.getNextEntry())
150          {
151             // Next entry
152
File JavaDoc fic = new File JavaDoc(todir, entry.getName());
153             int lastDot = fic.getName().lastIndexOf(".");
154
155             if (entry.isDirectory())
156             {
157                // This is a directory that we must create
158
try
159                {
160                   ensureDirExist(explode, fic);
161                }
162                catch (DirException e)
163                {
164                   explode.log(e.getMessage());
165                }
166             }
167             else if (lastDot != -1 && extensions.contains(fic.getName().substring(lastDot + 1)))
168             {
169                // This is a nested archive, we explode it
170
try
171                {
172                   explode.log("Process nested archive " + fic.getName());
173                   explode(explode, fic.getName(), new ZipInputStream JavaDoc(zip), todir);
174                }
175                catch (DirException e)
176                {
177                   explode.log(e.getMessage());
178                }
179             }
180             else
181             {
182                // This is a file we write it
183
FileOutputStream JavaDoc out = null;
184                try
185                {
186                   out = new FileOutputStream JavaDoc(fic);
187                   for (int size = zip.read(buffer);size != -1;size = zip.read(buffer))
188                   {
189                      out.write(buffer, 0, size);
190                   }
191                }
192                catch (IOException JavaDoc e)
193                {
194                   explode.log("Problem when writing file " + e.getMessage());
195                }
196                finally
197                {
198                   if (out != null)
199                   {
200                      try
201                      {
202                         out.close();
203                      }
204                      catch (IOException JavaDoc ignored)
205                      {
206                      }
207                   }
208                }
209             }
210          }
211       }
212       catch (ZipException JavaDoc e)
213       {
214          throw new BuildException(e);
215       }
216       catch (IOException JavaDoc e)
217       {
218          throw new BuildException(e);
219       }
220    }
221
222    /**
223     * When it returns the dir exists otherwise it throws a BuildException
224     */

225    private static void ensureDirExist(Explode explode, File JavaDoc dir) throws FileIsNotDirException, CannotCreateDirException
226    {
227       if (dir.exists())
228       {
229          if (dir.isDirectory())
230          {
231             // explode.log(dir.getName() + " exists and is used");
232
}
233          else
234          {
235             throw new FileIsNotDirException(dir);
236          }
237       }
238       else
239       {
240          if (dir.mkdirs())
241          {
242             // explode.log("Created directory " + dir.getName());
243
}
244          else
245          {
246             throw new CannotCreateDirException(dir);
247          }
248       }
249    }
250 }
251
Popular Tags