Created
June 2, 2018 18:08
-
-
Save bitsnaps/83fca9f6dec11f1121d69f5cb246d310 to your computer and use it in GitHub Desktop.
Extract Jar file pragmatically with Groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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