Added JarUtils: programatically extract JAR files.

This commit is contained in:
Jonathan Bernard 2012-01-02 15:08:26 -06:00
parent 2308280480
commit e12a605b5c
2 changed files with 39 additions and 3 deletions

View File

@ -1,6 +1,6 @@
#Mon, 05 Dec 2011 22:17:22 -0600 #Mon, 02 Jan 2012 15:07:25 -0600
name=jdb-util name=jdb-util
version=1.4 version=1.5
lib.local=true lib.local=true
build.number=9 build.number=2

View File

@ -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() }
}