1 17 package org.apache.servicemix.jbi.util; 18 19 import java.io.BufferedInputStream ; 20 import java.io.BufferedOutputStream ; 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.net.URL ; 28 import java.util.Enumeration ; 29 import java.util.zip.ZipEntry ; 30 import java.util.zip.ZipFile ; 31 import java.util.zip.ZipOutputStream ; 32 33 38 public class FileUtil { 39 40 44 private static final int DEFAULT_BUFFER_SIZE = 4096; 45 46 53 public static void moveFile(File src, File targetDirectory) throws IOException { 54 if (!src.renameTo(new File (targetDirectory, src.getName()))){ 55 throw new IOException ("Failed to move " + src + " to " + targetDirectory); 56 } 57 } 58 59 66 public static File getDirectoryPath(File parent, String subDirectory) { 67 File result = null; 68 if (parent != null){ 69 result = new File (parent, subDirectory); 70 } 71 return result; 72 } 73 74 80 public static boolean buildDirectory(File file) { 81 return file.exists() || file.mkdirs(); 82 } 83 84 91 public static void copyInputStream(InputStream in, OutputStream out) throws IOException { 92 byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; 93 int len; 94 while ((len = in.read(buffer)) >= 0) { 95 out.write(buffer, 0, len); 96 } 97 in.close(); 98 out.close(); 99 } 100 101 109 public static File unpackArchive(File theFile, File targetDir) throws IOException { 110 if (!theFile.exists()) { 111 throw new IOException (theFile.getAbsolutePath() + " does not exist"); 112 } 113 if (!buildDirectory(targetDir)) { 114 throw new IOException ("Could not create directory: " + targetDir); 115 } 116 ZipFile zipFile; 117 zipFile = new ZipFile (theFile); 118 for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) { 119 ZipEntry entry = (ZipEntry ) entries.nextElement(); 120 File file = new File (targetDir, File.separator + entry.getName()); 121 if (!buildDirectory(file.getParentFile())) { 124 throw new IOException ("Could not create directory: " + file.getParentFile()); 125 } 126 if (!entry.isDirectory()) { 127 copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream (new FileOutputStream (file))); 128 } else { 129 if (!buildDirectory(file)) { 130 throw new IOException ("Could not create directory: " + file); 131 } 132 } 133 } 134 zipFile.close(); 135 return theFile; 136 } 137 138 146 public static File unpackArchive(URL url, File targetDir) throws IOException { 147 if (!targetDir.exists()) { 148 targetDir.mkdirs(); 149 } 150 InputStream in = new BufferedInputStream (url.openStream(), DEFAULT_BUFFER_SIZE); 151 File zip = File.createTempFile("arc", ".zip", targetDir); 153 OutputStream out = new BufferedOutputStream (new FileOutputStream (zip)); 154 copyInputStream(in, out); 155 out.close(); 156 return unpackArchive(zip, targetDir); 157 } 158 159 167 public static boolean archiveContainsEntry(File theFile, String name) throws IOException { 168 boolean result = false; 169 ZipFile zipFile; 170 zipFile = new ZipFile (theFile); 171 for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) { 172 ZipEntry entry = (ZipEntry ) entries.nextElement(); 173 if (entry.getName().equals(name)) { 174 result = true; 175 break; 176 } 177 } 178 zipFile.close(); 179 return result; 180 } 181 182 190 public synchronized static File createUniqueDirectory(File rootDir, String seed) throws IOException { 191 int index = seed.lastIndexOf('.'); 192 if (index > 0) { 193 seed = seed.substring(0, index); 194 } 195 File result = null; 196 int count = 0; 197 while (result == null) { 198 String name = seed + "." + count + ".tmp"; 199 File file = new File (rootDir, name); 200 if (!file.exists()) { 201 file.mkdirs(); 202 result = file; 203 } 204 count++; 205 } 206 return result; 207 } 208 209 215 public static boolean deleteFile(File fileToDelete) { 216 boolean result = true; 217 if (fileToDelete != null && fileToDelete.exists()) { 218 if (fileToDelete.isDirectory()) { 219 File [] files = fileToDelete.listFiles(); 220 if (files == null) { 221 result = false; 222 } else { 223 for (int i = 0; i < files.length; i++) { 224 File file = files[i]; 225 if (!file.getName().equals(".") && !file.getName().equals("..")) { 226 if (file.isDirectory()) { 227 result &= deleteFile(file); 228 } else { 229 result &= file.delete(); 230 } 231 } 232 } 233 } 234 } 235 result &= fileToDelete.delete(); 236 } 237 return result; 238 } 239 240 247 public static void zipDir(String directory, String zipName) throws IOException { 248 ZipOutputStream zos = new ZipOutputStream (new FileOutputStream (zipName)); 250 String path = ""; 251 zipDir(directory, zos, path); 252 zos.close(); 254 } 255 256 263 public static void zipDir(String directory, ZipOutputStream zos, String path) throws IOException { 264 File zipDir = new File (directory); 265 String [] dirList = zipDir.list(); 267 byte[] readBuffer = new byte[2156]; 268 int bytesIn = 0; 269 for (int i = 0; i < dirList.length; i++) { 271 File f = new File (zipDir, dirList[i]); 272 if (f.isDirectory()) { 273 String filePath = f.getPath(); 274 zipDir(filePath, zos, path + f.getName() + "/"); 275 continue; 276 } 277 FileInputStream fis = new FileInputStream (f); 278 ZipEntry anEntry = new ZipEntry (path + f.getName()); 279 zos.putNextEntry(anEntry); 280 while ((bytesIn = fis.read(readBuffer)) != -1) { 281 zos.write(readBuffer, 0, bytesIn); 282 } 283 fis.close(); 284 } 285 } 286 } | Popular Tags |