1 25 26 package org.objectweb.easybeans.util.files; 27 28 import java.io.File ; 29 import java.io.FileInputStream ; 30 import java.io.FileOutputStream ; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.nio.channels.FileChannel ; 34 import java.util.Enumeration ; 35 import java.util.jar.JarEntry ; 36 import java.util.jar.JarFile ; 37 38 import org.objectweb.easybeans.log.JLog; 39 import org.objectweb.easybeans.log.JLogFactory; 40 41 45 public final class FileUtils { 46 47 50 private static final int BUFFER_SIZE = 2048; 51 52 55 private static JLog logger = JLogFactory.getLog(FileUtils.class); 56 57 60 private FileUtils() { 61 62 } 63 64 71 public static void unpack(final JarFile packedJar, final File dest) throws FileUtilsException { 72 73 JarEntry entry = null; 74 75 Enumeration entries = packedJar.entries(); 77 while (entries.hasMoreElements()) { 78 entry = (JarEntry ) entries.nextElement(); 79 80 File entryFile = new File (dest, entry.getName()); 82 83 if (entry.isDirectory()) { 85 if (!entryFile.exists()) { 86 if (!entryFile.mkdirs()) { 88 String err = "Can not create directory " + entryFile + ", Check the write access."; 89 throw new FileUtilsException(err); 90 } 91 } 92 continue; 93 } 94 95 entryFile.getParentFile().mkdirs(); 98 99 InputStream is = null; 100 try { 102 is = packedJar.getInputStream(entry); 103 dump(is, entryFile); 105 } catch (IOException ioe) { 106 throw new FileUtilsException("Cannot get inputstream of entry '" + entry + "' of file '" + packedJar + "'."); 107 } finally { 108 try { 109 is.close(); 110 } catch (IOException ioe) { 111 logger.debug("Cannot close input stream", ioe); 112 } 113 } 114 115 } 116 } 117 118 124 private static void dump(final InputStream in, final File entryFile) throws FileUtilsException { 125 126 try { 127 FileOutputStream out = new FileOutputStream (entryFile); 129 int n = 0; 130 try { 131 byte[] buffer = new byte[BUFFER_SIZE]; 133 n = in.read(buffer); 134 while (n > 0) { 135 out.write(buffer, 0, n); 136 n = in.read(buffer); 137 } 138 } finally { 139 out.close(); 140 } 141 } catch (IOException e) { 142 String err = "Error while unpacking entry " + entryFile + " : "; 143 throw new FileUtilsException(err, e); 144 } 145 } 146 147 153 public static void copyFile(final String src, final String dest) throws FileUtilsException { 154 copyFile(new File (src), new File (dest)); 155 } 156 157 163 public static void copyFile(final File src, final File dest) throws FileUtilsException { 164 File newDest = null; 165 if (dest.isDirectory()) { 166 logger.debug("Copy a file to a directory, append source filename to directory."); 167 newDest = new File (dest, src.getName()); 168 } else { 169 newDest = dest; 170 } 171 172 FileInputStream fIn = null; 173 FileOutputStream fOut = null; 174 FileChannel fcIn = null; 175 FileChannel fcOut = null; 176 try { 177 178 fIn = new FileInputStream (src); 180 fOut = new FileOutputStream (newDest); 181 182 FileChannel sourceFC = fIn.getChannel(); 184 FileChannel targetFC = fOut.getChannel(); 185 186 targetFC.transferFrom(sourceFC, 0, sourceFC.size()); 187 } catch (Exception e) { 188 throw new FileUtilsException("Error during copy file : " + src + " -> " + dest, e); 189 } finally { 190 try { 191 fOut.close(); 192 fIn.close(); 193 fcOut.close(); 194 fcIn.close(); 195 } catch (Exception e) { 196 logger.debug("Cannot close some i/o which are open.", e); 197 } 198 199 } 200 } 201 202 206 public static boolean delete(final String path) { 207 return delete(new File (path)); 208 } 209 210 214 public static boolean delete(final File f) { 215 if (f.isFile()) { 216 return f.delete(); 217 } 218 File [] children = f.listFiles(); 220 if (children == null) { 221 return f.delete(); 223 } 224 225 boolean result = true; 227 for (int i = 0; i < children.length; i++) { 228 result &= delete(children[i]); 229 } 230 return result && f.delete(); 231 } 232 233 239 public static void copyDirectory(final String src, final String dest) throws FileUtilsException { 240 copyDirectory(new File (src), new File (dest)); 241 } 242 243 249 public static void copyDirectory(final File src, final File dest) throws FileUtilsException { 250 251 if (!src.isDirectory()) { 252 throw new IllegalArgumentException ("Source '" + src + "' must be a directory"); 254 } 255 256 if (!dest.exists()) { 258 dest.mkdirs(); 259 } 260 261 File [] childs = src.listFiles(); 263 if (childs != null) { 264 for (int i = 0; i < childs.length; i++) { 266 File child = childs[i]; 267 if (child.isFile()) { 268 copyFile(child, dest); 270 } else { 271 copyDirectory(child, new File (dest, child.getName())); 273 } 274 } 275 } 276 } 277 278 } 279 | Popular Tags |