diff --git a/project.properties b/project.properties index 8869e54..b67f443 100644 --- a/project.properties +++ b/project.properties @@ -1,6 +1,6 @@ -#Mon, 05 Dec 2011 22:17:22 -0600 +#Mon, 02 Jan 2012 15:07:25 -0600 name=jdb-util -version=1.4 +version=1.5 lib.local=true -build.number=9 +build.number=2 diff --git a/src/main/com/jdbernard/util/JarUtils.groovy b/src/main/com/jdbernard/util/JarUtils.groovy new file mode 100644 index 0000000..6252b48 --- /dev/null +++ b/src/main/com/jdbernard/util/JarUtils.groovy @@ -0,0 +1,36 @@ +package com.jdbernard.util + +import java.util.jar.JarEntry +import java.util.jar.JarFile +import java.util.jar.JarInputStream +import java.util.jar.JarOutputStream + +public class JarUtils { + + public static void extract(File f, File outputDir) { + extract(new JarFile(f), outputDir) } + + public static void extract(JarFile jarFile, File outputDir) { + jarFile.entries().each { jarEntry -> + def outputFile = new File(outputDir, jarEntry.name) + if (jarEntry.isDirectory()) { outputFile.mkdirs() } + else { + jarFile.getInputStream(jarEntry).withStream { is -> + outputFile.withOutputStream { os -> + while (is.available() > 0) { os.write(is.read()) }}}}}} + + public static void extract(JarInputStream jarIS, File outputDir) { + JarEntry curEntry = jarIS.nextJarEntry + while(curEntry != null) { + File outFile = new File(outputDir, curEntry.name) + if (curEntry.isDirectory()) { outFile.mkdirs() } + else { + outFile.withOutputStream { os -> + byte[] buffer = new byte[curEntry.size] + int bytesRead = jarIS.read(buffer, 0, curEntry.size as + Integer) + os.write(buffer, 0, bytesRead) }} + + curEntry = jarIS.nextJarEntry } + jarIS.close() } +}