Commit in servicemix/base/src/main/java/org/servicemix/jbi/util on MAIN
FileUtil.java+195-1871.6 -> 1.7
Fixed the handling for ZIP's generated by Microsoft that do not include the directory as a separate entry.

servicemix/base/src/main/java/org/servicemix/jbi/util
FileUtil.java 1.6 -> 1.7
diff -u -r1.6 -r1.7
--- FileUtil.java	1 Aug 2005 00:03:40 -0000	1.6
+++ FileUtil.java	3 Aug 2005 16:47:56 -0000	1.7
@@ -18,6 +18,7 @@
  **/
 
 package org.servicemix.jbi.util;
+
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.File;
@@ -33,194 +34,201 @@
 /**
  * File utilities
  * 
- * @version $Revision: 1.6 $
+ * @version $Revision: 1.7 $
  */
 public class FileUtil {
-    private static final int DEFAULT_BUFFER_SIZE = 4096;
+	private static final int DEFAULT_BUFFER_SIZE = 4096;
 
-    /**
-     * Move a File
-     * 
-     * @param src
-     * @param targetDirectory
-     */
-    public static void moveFile(File src, File targetDirectory) {
-        src.renameTo(new File(targetDirectory, src.getName()));
-    }
-
-    /**
-     * Build a path- but do not create it
-     * 
-     * @param parent
-     * @param subDirectory
-     * @return a File representing the path
-     */
-    public static File getDirectoryPath(File parent, String subDirectory) {
-        File result = new File(parent.getAbsolutePath() + File.separator + subDirectory);
-        return result;
-    }
-
-    /**
-     * Build a directory path - creating directories if neccesary
-     * 
-     * @param file
-     * @return true if the directory exists, or making it was successful
-     */
-    public static boolean buildDirectory(File file) {
-        return file.exists() || file.mkdirs();
-    }
-
-    /**
-     * Copy in stream to an out stream
-     * 
-     * @param in
-     * @param out
-     * @throws IOException
-     */
-    public static void copyInputStream(InputStream in, OutputStream out) throws IOException {
-        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
-        int len;
-        while ((len = in.read(buffer)) >= 0)
-            out.write(buffer, 0, len);
-        in.close();
-        out.close();
-    }
-
-    /**
-     * Unpack a zip file
-     * 
-     * @param theFile
-     * @param targetDir
-     * @return the file
-     * @throws IOException
-     */
-    public static File unpackArchive(File theFile, File targetDir) throws IOException {
-        if (!theFile.exists()) {
-            throw new IOException(theFile.getAbsolutePath() + " Does Not Exist!");
-        }
-        if (!targetDir.exists()) {
-            targetDir.mkdirs();
-        }
-        ZipFile zipFile;
-        zipFile = new ZipFile(theFile);
-        for (Enumeration entries = zipFile.entries();entries.hasMoreElements();) {
-            ZipEntry entry = (ZipEntry) entries.nextElement();
-            File file = new File(targetDir, File.separator + entry.getName());
-            if (entry.isDirectory()) {
-                if (!file.exists()) {
-                    file.mkdirs();
-                }
-            }
-            else {
-                copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(file)));
-            }
-        }
-        zipFile.close();
-        return theFile;
-    }
-
-    /**
-     * Unpack an archive from a URL
-     * 
-     * @param url
-     * @param targetDir
-     * @return the file to the url
-     * @throws IOException
-     */
-    public static File unpackArchive(URL url, File targetDir) throws IOException {
-        if (!targetDir.exists()) {
-            targetDir.mkdirs();
-        }
-        InputStream in = new BufferedInputStream(url.openStream(), DEFAULT_BUFFER_SIZE);
-        String fileName = url.getFile();
-        // make sure we get the actual file
-        int index = fileName.lastIndexOf("/");
-        if (index > 0) {
-            fileName = fileName.substring(index + 1);
-        }
-        File zip = new File(targetDir, fileName);
-        OutputStream out = new BufferedOutputStream(new FileOutputStream(zip));
-        copyInputStream(in, out);
-        out.close();
-        return unpackArchive(zip, targetDir);
-    }
-
-    /**
-     * Validate an arcive contains a named entry
-     * 
-     * @param theFile
-     * @param name
-     * @return true if the entry exists
-     * @throws IOException
-     */
-    public static boolean archiveContainsEntry(File theFile, String name) throws IOException {
-        boolean result = false;
-        ZipFile zipFile;
-        zipFile = new ZipFile(theFile);
-        for (Enumeration entries = zipFile.entries();entries.hasMoreElements();) {
-            ZipEntry entry = (ZipEntry) entries.nextElement();
-            if (entry.getName().equals(name)) {
-                result = true;
-                break;
-            }
-        }
-        zipFile.close();
-        return result;
-    }
-
-    /**
-     * Create a unique directory within a directory 'root'
-     * 
-     * @param rootDir
-     * @param seed
-     * @return unique directory
-     * @throws IOException
-     */
-    public synchronized static File createUniqueDirectory(File rootDir, String seed) throws IOException {
-        int index = seed.lastIndexOf('.');
-        if (index > 0) {
-            seed = seed.substring(0, index);
-        }
-        File result = null;
-        int count = 0;
-        while (result == null) {
-            String name = seed + "." + count + ".tmp";
-            File file = new File(rootDir, name);
-            if (!file.exists()) {
-                file.mkdirs();
-                result = file;
-            }
-            count++;
-        }
-        return result;
-    }
-
-    /**
-     * Delete a file
-     * 
-     * @param fileToDelete
-     * @return true if the File is deleted
-     */
-    public static boolean deleteFile(File fileToDelete) {
-        boolean result = true;
-        if (fileToDelete != null && fileToDelete.exists()) {
-            if (fileToDelete.isDirectory()) {
-                File[] files = fileToDelete.listFiles();
-                for (int i = 0;i < files.length;i++) {
-                    File file = files[i];
-                    if (!file.isHidden()) {
-                        if (file.isDirectory()) {
-                            result &= deleteFile(file);
-                        }
-                        result &= file.delete();
-                    }
-                }
-            }
-            result &= fileToDelete.delete();
-            if (fileToDelete.exists()) {
-                fileToDelete.delete();
-            }
-        }
-        return result;
-    }
+	/**
+	 * Move a File
+	 * 
+	 * @param src
+	 * @param targetDirectory
+	 */
+	public static void moveFile(File src, File targetDirectory) {
+		src.renameTo(new File(targetDirectory, src.getName()));
+	}
+
+	/**
+	 * Build a path- but do not create it
+	 * 
+	 * @param parent
+	 * @param subDirectory
+	 * @return a File representing the path
+	 */
+	public static File getDirectoryPath(File parent, String subDirectory) {
+		File result = new File(parent.getAbsolutePath() + File.separator
+				+ subDirectory);
+		return result;
+	}
+
+	/**
+	 * Build a directory path - creating directories if neccesary
+	 * 
+	 * @param file
+	 * @return true if the directory exists, or making it was successful
+	 */
+	public static boolean buildDirectory(File file) {
+		return file.exists() || file.mkdirs();
+	}
+
+	/**
+	 * Copy in stream to an out stream
+	 * 
+	 * @param in
+	 * @param out
+	 * @throws IOException
+	 */
+	public static void copyInputStream(InputStream in, OutputStream out)
+			throws IOException {
+		byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
+		int len;
+		while ((len = in.read(buffer)) >= 0)
+			out.write(buffer, 0, len);
+		in.close();
+		out.close();
+	}
+
+	/**
+	 * Unpack a zip file
+	 * 
+	 * @param theFile
+	 * @param targetDir
+	 * @return the file
+	 * @throws IOException
+	 */
+	public static File unpackArchive(File theFile, File targetDir)
+			throws IOException {
+		if (!theFile.exists()) {
+			throw new IOException(theFile.getAbsolutePath()
+					+ " Does Not Exist!");
+		}
+		if (!targetDir.exists()) {
+			targetDir.mkdirs();
+		}
+		ZipFile zipFile;
+		zipFile = new ZipFile(theFile);
+		for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) {
+			ZipEntry entry = (ZipEntry) entries.nextElement();			
+			File file = new File(targetDir, File.separator + entry.getName());
+			// Take the sledgehammer approach to creating directories
+			// to work around ZIP's that incorrectly miss directories
+			file.mkdirs();
+			file.delete();
+			if (!entry.isDirectory())
+				copyInputStream(zipFile.getInputStream(entry),
+						new BufferedOutputStream(new FileOutputStream(file)));
+		}
+		zipFile.close();
+		return theFile;
+	}
+
+	/**
+	 * Unpack an archive from a URL
+	 * 
+	 * @param url
+	 * @param targetDir
+	 * @return the file to the url
+	 * @throws IOException
+	 */
+	public static File unpackArchive(URL url, File targetDir)
+			throws IOException {
+		if (!targetDir.exists()) {
+			targetDir.mkdirs();
+		}
+		InputStream in = new BufferedInputStream(url.openStream(),
+				DEFAULT_BUFFER_SIZE);
+		String fileName = url.getFile();
+		// make sure we get the actual file
+		int index = fileName.lastIndexOf("/");
+		if (index > 0) {
+			fileName = fileName.substring(index + 1);
+		}
+		File zip = new File(targetDir, fileName);
+		OutputStream out = new BufferedOutputStream(new FileOutputStream(zip));
+		copyInputStream(in, out);
+		out.close();
+		return unpackArchive(zip, targetDir);
+	}
+
+	/**
+	 * Validate an arcive contains a named entry
+	 * 
+	 * @param theFile
+	 * @param name
+	 * @return true if the entry exists
+	 * @throws IOException
+	 */
+	public static boolean archiveContainsEntry(File theFile, String name)
+			throws IOException {
+		boolean result = false;
+		ZipFile zipFile;
+		zipFile = new ZipFile(theFile);
+		for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) {
+			ZipEntry entry = (ZipEntry) entries.nextElement();
+			if (entry.getName().equals(name)) {
+				result = true;
+				break;
+			}
+		}
+		zipFile.close();
+		return result;
+	}
+
+	/**
+	 * Create a unique directory within a directory 'root'
+	 * 
+	 * @param rootDir
+	 * @param seed
+	 * @return unique directory
+	 * @throws IOException
+	 */
+	public synchronized static File createUniqueDirectory(File rootDir,
+			String seed) throws IOException {
+		int index = seed.lastIndexOf('.');
+		if (index > 0) {
+			seed = seed.substring(0, index);
+		}
+		File result = null;
+		int count = 0;
+		while (result == null) {
+			String name = seed + "." + count + ".tmp";
+			File file = new File(rootDir, name);
+			if (!file.exists()) {
+				file.mkdirs();
+				result = file;
+			}
+			count++;
+		}
+		return result;
+	}
+
+	/**
+	 * Delete a file
+	 * 
+	 * @param fileToDelete
+	 * @return true if the File is deleted
+	 */
+	public static boolean deleteFile(File fileToDelete) {
+		boolean result = true;
+		if (fileToDelete != null && fileToDelete.exists()) {
+			if (fileToDelete.isDirectory()) {
+				File[] files = fileToDelete.listFiles();
+				for (int i = 0; i < files.length; i++) {
+					File file = files[i];
+					if (!file.isHidden()) {
+						if (file.isDirectory()) {
+							result &= deleteFile(file);
+						}
+						result &= file.delete();
+					}
+				}
+			}
+			result &= fileToDelete.delete();
+			if (fileToDelete.exists()) {
+				fileToDelete.delete();
+			}
+		}
+		return result;
+	}
 }
CVSspam 0.2.8



Reply via email to