Skip to content

Instantly share code, notes, and snippets.

@bitsnaps
Created June 2, 2018 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitsnaps/83fca9f6dec11f1121d69f5cb246d310 to your computer and use it in GitHub Desktop.
Save bitsnaps/83fca9f6dec11f1121d69f5cb246d310 to your computer and use it in GitHub Desktop.
Extract Jar file pragmatically with Groovy
/*
Original Java Code from here:
https://stackoverflow.com/questions/1529611/how-to-write-a-java-program-which-can-extract-a-jar-file-and-store-its-data-in-s
*/
import java.util.jar.*
def jar = new JarFile("path/to/MyLib.jar")
def jarFile = new File(jar.name)
def distDir = "${jarFile.parent+File.separator}"
def libName = jarFile.name.with {it.take(it.lastIndexOf('.'))}
new File("${distDir+libName}").with {
if (!it.exists()) it.mkdir()
}
distDir += libName+File.separator
for (JarEntry file in jar.entries()){
def f = new File("${distDir+file.name}")
if (file.isDirectory()){
f.mkdir()
continue
}
def is = jar.getInputStream(file)
f.withOutputStream { def stream ->
while (is.available()>0)
stream.write(is.read())
}
is.close()
}
jar.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment