1 10 11 package org.mule.util; 12 13 import java.io.BufferedOutputStream ; 14 import java.io.BufferedWriter ; 15 import java.io.File ; 16 import java.io.FileOutputStream ; 17 import java.io.FileWriter ; 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.OutputStream ; 21 import java.net.URL ; 22 import java.net.URLDecoder ; 23 import java.net.URI ; 24 import java.util.Enumeration ; 25 import java.util.zip.ZipEntry ; 26 import java.util.zip.ZipFile ; 27 28 import org.mule.MuleManager; 29 import org.mule.MuleRuntimeException; 30 import org.mule.config.i18n.Message; 31 32 36 public class FileUtils extends org.apache.commons.io.FileUtils 38 { 39 public static synchronized void copyStreamToFile(InputStream input, File destination) throws IOException { 40 if (destination.exists() && !destination.canWrite()) { 41 throw new IOException ("Destination file does not exist or is not writeable"); 42 } 43 44 try { 45 FileOutputStream output = new FileOutputStream (destination); 46 try { 47 IOUtils.copy(input, output); 48 } finally { 49 IOUtils.closeQuietly(output); 50 } 51 } finally { 52 IOUtils.closeQuietly(input); 53 } 54 } 55 56 public static File createFile(String filename) throws IOException 58 { 59 File file = FileUtils.newFile(filename); 60 if (!file.canWrite()) 61 { 62 String dirName = file.getPath(); 63 int i = dirName.lastIndexOf(File.separator); 64 if (i > -1) 65 { 66 dirName = dirName.substring(0, i); 67 File dir = FileUtils.newFile(dirName); 68 dir.mkdirs(); 69 } 70 file.createNewFile(); 71 } 72 return file; 73 } 74 75 public static String prepareWinFilename(String filename) 77 { 78 filename = filename.replaceAll("<", "("); 79 filename = filename.replaceAll(">", ")"); 80 filename = filename.replaceAll("[/\\*?|:;]", "-"); 81 return filename; 82 } 83 84 public static File openDirectory(String directory) throws IOException 86 { 87 File dir = FileUtils.newFile(directory); 88 if (!dir.exists()) 89 { 90 dir.mkdirs(); 91 } 92 if (!dir.isDirectory() || !dir.canRead()) 93 { 94 throw new IOException ("Path: " + directory + " exists but isn't a directory"); 95 } 96 return dir; 97 } 98 99 107 public static File stringToFile(String filename, String data) throws IOException 108 { 109 return stringToFile(filename, data, false); 110 } 111 112 public static synchronized File stringToFile(String filename, String data, boolean append) 114 throws IOException 115 { 116 return stringToFile(filename, data, append, false); 117 } 118 119 public static synchronized File stringToFile(String filename, String data, boolean append, boolean newLine) 121 throws IOException 122 { 123 File f = createFile(filename); 124 BufferedWriter writer = null; 125 try 126 { 127 writer = new BufferedWriter (new FileWriter (f, append)); 128 writer.write(data); 129 if (newLine) 130 { 131 writer.newLine(); 132 } 133 } 134 finally 135 { 136 if (writer != null) 137 { 138 writer.close(); 139 } 140 } 141 return f; 142 } 143 144 public static String getResourcePath(String resourceName, Class callingClass) throws IOException 146 { 147 return getResourcePath(resourceName, callingClass, MuleManager.getConfiguration().getEncoding()); 148 } 149 150 public static String getResourcePath(String resourceName, Class callingClass, String encoding) 152 throws IOException 153 { 154 if (resourceName == null) 155 { 156 return null; 158 } 159 160 URL url = IOUtils.getResourceAsUrl(resourceName, callingClass); 161 if (url == null) 162 { 163 return null; 165 } 166 167 String resource = URLDecoder.decode(url.toExternalForm(), encoding); 168 if (resource != null) 169 { 170 if (resource.startsWith("file:/")) 171 { 172 resource = resource.substring(6); 173 } 174 if (!resource.startsWith(File.separator)) 175 { 176 resource = File.separator + resource; 177 } 178 } 179 180 return resource; 181 } 182 183 public static boolean deleteTree(File dir) 185 { 186 if (dir == null || !dir.exists()) 187 { 188 return true; 189 } 190 File [] files = dir.listFiles(); 191 if (files != null) 192 { 193 for (int i = 0; i < files.length; i++) 194 { 195 if (files[i].isDirectory()) 196 { 197 if (!deleteTree(files[i])) 198 { 199 return false; 200 } 201 } 202 else 203 { 204 if (!files[i].delete()) 205 { 206 return false; 207 } 208 } 209 } 210 } 211 return dir.delete(); 212 } 213 214 217 public static void unzip(File archive, File directory) throws IOException 218 { 219 ZipFile zip = null; 220 221 if (directory.exists()) 222 { 223 if (!directory.isDirectory()) 224 { 225 throw new IOException ("Directory is not a directory: " + directory); 226 } 227 } 228 else 229 { 230 if (!directory.mkdirs()) 231 { 232 throw new IOException ("Could not create directory: " + directory); 233 } 234 } 235 try 236 { 237 zip = new ZipFile (archive); 238 for (Enumeration entries = zip.entries(); entries.hasMoreElements();) 239 { 240 ZipEntry entry = (ZipEntry )entries.nextElement(); 241 File f = new File (directory, entry.getName()); 242 if (entry.isDirectory()) 243 { 244 if (!f.mkdirs()) 245 { 246 throw new IOException ("Could not create directory: " + f); 247 } 248 } 249 else 250 { 251 InputStream is = zip.getInputStream(entry); 252 OutputStream os = new BufferedOutputStream (new FileOutputStream (f)); 253 IOUtils.copy(is, os); 254 IOUtils.closeQuietly(is); 255 IOUtils.closeQuietly(os); 256 } 257 } 258 } 259 finally 260 { 261 if (zip != null) 262 { 263 zip.close(); 264 } 265 } 266 } 267 268 279 public static File newFile(String pathName) 280 { 281 try 282 { 283 return new File (pathName).getCanonicalFile(); 284 } 285 catch (IOException e) 286 { 287 throw new MuleRuntimeException( 288 Message.createStaticMessage("Unable to create a canonical file for " + pathName), e); 289 } 290 } 291 292 303 public static File newFile(URI uri) 304 { 305 try 306 { 307 return new File (uri).getCanonicalFile(); 308 } 309 catch (IOException e) 310 { 311 throw new MuleRuntimeException( 312 Message.createStaticMessage("Unable to create a canonical file for " + uri), e); 313 } 314 } 315 316 327 public static File newFile(File parent, String child) 328 { 329 try 330 { 331 return new File (parent, child).getCanonicalFile(); 332 } 333 catch (IOException e) 334 { 335 throw new MuleRuntimeException( 336 Message.createStaticMessage("Unable to create a canonical file for parent: " + 337 parent + " and child: " + child), e); 338 } 339 } 340 341 352 public static File newFile(String parent, String child) 353 { 354 try 355 { 356 return new File (parent, child).getCanonicalFile(); 357 } 358 catch (IOException e) 359 { 360 throw new MuleRuntimeException( 361 Message.createStaticMessage("Unable to create a canonical file for parent: " + 362 parent + " and child: " + child), e); 363 } 364 } 365 } 366 | Popular Tags |