From b66e2d6e52df3dc5a2609b63e4132ff2f114800b Mon Sep 17 00:00:00 2001 From: mdb Date: Tue, 4 Apr 2006 18:41:45 +0000 Subject: [PATCH] Moved robust jar unpacking code out of Narya and into Samskivert. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1807 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/util/FileUtil.java | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/src/java/com/samskivert/util/FileUtil.java b/src/java/com/samskivert/util/FileUtil.java index 802afd70..90c955d7 100644 --- a/src/java/com/samskivert/util/FileUtil.java +++ b/src/java/com/samskivert/util/FileUtil.java @@ -3,9 +3,19 @@ package com.samskivert.util; +import java.io.BufferedOutputStream; import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; + +import java.util.Enumeration; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +import org.apache.commons.io.CopyUtils; import com.samskivert.Log; +import com.samskivert.io.StreamUtil; /** * Utility methods for files. @@ -44,6 +54,65 @@ public class FileUtil return path + newext; } + /** + * Unpacks the specified jar file intto the specified target directory. + * + * @return true if the jar file was successfully unpacked, false if an + * error occurred that prevented the unpacking. The error will be logged. + */ + public static boolean unpackJar (JarFile jar, File target) + { + boolean failure = false; + Enumeration entries = jar.entries(); + while (!failure && entries.hasMoreElements()) { + JarEntry entry = (JarEntry)entries.nextElement(); + File efile = new File(target, entry.getName()); + + // if we're unpacking a normal jar file, it will have special path + // entries that allow us to create our directories first + if (entry.isDirectory()) { + if (!efile.exists() && !efile.mkdir()) { + Log.warning("Failed to create jar entry path [jar=" + jar + + ", entry=" + entry + "]."); + } + continue; + } + + // but some do not, so we want to ensure that our directories exist + // prior to getting down and funky + File parent = new File(efile.getParent()); + if (!parent.exists() && !parent.mkdirs()) { + Log.warning("Failed to create jar entry parent [jar=" + jar + + ", parent=" + parent + "]."); + continue; + } + + BufferedOutputStream fout = null; + InputStream jin = null; + try { + fout = new BufferedOutputStream(new FileOutputStream(efile)); + jin = jar.getInputStream(entry); + CopyUtils.copy(jin, fout); + } catch (Exception e) { + Log.warning("Failure unpacking [jar=" + jar + + ", entry=" + efile + ", error=" + e + "]."); + failure = true; + } finally { + StreamUtil.close(jin); + StreamUtil.close(fout); + } + } + + try { + jar.close(); + } catch (Exception e) { + Log.warning("Failed to close jar file [jar=" + jar + + ", error=" + e + "]."); + } + + return !failure; + } + /** Helper function. */ protected static void recursiveWipe (File file, boolean wipeMe) {