View Javadoc

1   package org.devaki.nextobjects.util;
2   /*
3    *  nextobjects Copyright (C) 2001-2005 Emmanuel Florent
4    *  This program is free software; you can redistribute it and/or modify
5    *  it under the terms of the GNU General Public License as published by the
6    *  Free Software Foundation; either version 2 of the License, or (at your
7    *  option) any later version.
8    *  This program is distributed in the hope that it will
9    *  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
10   *  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   *  PURPOSE. See the GNU General Public License for more details.
12   *  You should have received a copy of the GNU General Public License along
13   *  with this program; if not, write to the Free Software Foundation, Inc., 59
14   *  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15   */
16  import java.io.BufferedInputStream;
17  import java.io.BufferedOutputStream;
18  import java.io.File;
19  import java.io.FileNotFoundException;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.util.Enumeration;
23  import java.util.zip.ZipEntry;
24  import java.util.zip.ZipException;
25  import java.util.zip.ZipFile;
26  import javax.swing.JProgressBar;
27  /***
28   *  This class contains general usefull function for handling files
29   *
30   * @author     <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
31   * @created    December 22, 2003
32   */
33  public final class NOFileUtil
34  {
35      /***
36       * Constructor avoid instanciation
37       *
38       */
39      private NOFileUtil()
40      {
41      }
42      /***
43       *  Deletes a file or a directory along with all files and directories it
44       *  contains.
45       *
46       * @param  file  the file or directory to delete
47       * @return       whether delete was successful
48       */
49      public static boolean deleteFile(final File file)
50      {
51          if (file == null)
52          {
53              return false;
54          }
55          if (file.isDirectory())
56          {
57              String[] dirListing = file.list();
58              // For each file/directory in listing, make recursive call.
59              int len = dirListing.length;
60              for (int i = 0; i < len; i++)
61              {
62                  if (!deleteFile(new File(file.getAbsolutePath()
63                      + File.separator
64                      + dirListing[i])))
65                  {
66                      // Break and return an error.
67                      return false;
68                  }
69              }
70          }
71          // Delete file or directory.
72          if (!file.delete())
73          {
74              // Display message and return an error.
75              System.out.println(
76                  "Could not delete: " + file.getAbsolutePath());
77              return false;
78          }
79          return true;
80      }
81      /***
82       *  Given a path string create all the directories in the path. For example,
83       *  if the path string is "java/applet", the method will create directory
84       *  "java" and then "java/applet" if they don't exist. The file separator
85       *  string "/" is platform dependent system property.
86       *
87       * @param  path  Directory path string.
88       */
89      public static void createDirectory(final String path)
90      {
91          File dir = new File(path);
92          if (dir.exists())
93          {
94              return;
95          }
96          else
97          {
98              if (dir.mkdirs())
99              {
100                 return;
101             }
102             else
103             {
104                 throw (new SecurityException());
105             }
106         }
107     }
108     /***
109      *  Unzip a fie
110      *
111      * @param  file           the file
112      * @param  dest           the destination directory
113      * @param  jProgressBar   the load advance
114      * @throws  IOException   an IO exception
115      */
116     public static void unzip(
117         final String file,
118         final String dest,
119         final JProgressBar jProgressBar)
120         throws IOException
121     {
122         int s = 0;
123         ZipFile zipFile = new ZipFile(file);
124         Enumeration enum = zipFile.entries();
125         try
126         {
127             while (enum.hasMoreElements())
128             {
129                 ZipEntry target =
130                     new ZipEntry((ZipEntry) enum.nextElement());
131                 jProgressBar.setValue((int) (++s * 100) / 459);
132                 getEntry(zipFile, target, dest);
133             }
134         }
135         catch (FileNotFoundException e)
136         {
137             System.err.println("zipfile not found");
138         }
139         catch (ZipException e)
140         {
141             throw e;
142         }
143         catch (IOException e)
144         {
145             throw e;
146         }
147     }
148     /***
149      *  Get an entry of a zip file
150      *
151      * @param  zipFile        the zip file
152      * @param  target         the target
153      * @param  dest           the destination
154      * @throws  IOException   an IO exception
155      */
156     private static void getEntry(
157         final ZipFile zipFile,
158         final ZipEntry target,
159         final String dest)
160         throws IOException
161     {
162         try
163         {
164             File file = new File(dest + File.separator + target.getName());
165             if (target.isDirectory())
166             {
167                 file.mkdirs();
168                 //
169             }
170             else
171             {
172                 BufferedInputStream bis =
173                     new BufferedInputStream(zipFile.getInputStream(target));
174                 String parentName;
175                 if ((parentName = file.getParent()) != null)
176                 {
177                     File dir = new File(parentName);
178                     dir.mkdirs();
179                     //
180                 }
181                 BufferedOutputStream bos =
182                     new BufferedOutputStream(
183                         new FileOutputStream(file),
184                         2048);
185                 int c;
186                 // while EOF
187                 while ((c = bis.read()) != -1)
188                 {
189                     bos.write((byte) c);
190                 }
191                 bos.close();
192             }
193         }
194         catch (ZipException e)
195         {
196             throw e;
197         }
198         catch (IOException e)
199         {
200             throw e;
201         }
202     }
203 }