1 package org.devaki.nextobjects.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
67 return false;
68 }
69 }
70 }
71
72 if (!file.delete())
73 {
74
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
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 }