Created
August 3, 2012 07:46
-
-
Save ffbit/3245533 to your computer and use it in GitHub Desktop.
Example of extracting a jar file from java
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
log.debug("Started extracting resources."); | |
final String ZIP_FILE_PATH_SEPARATOR = "/"; | |
URL location = AClassFromTheNeededJar.class.getProtectionDomain().getCodeSource() | |
.getLocation(); | |
String jarPath = location.getPath(); | |
try { | |
JarInputStream jar = new JarInputStream(new FileInputStream(jarPath)); | |
JarEntry jarEntry = null; | |
while ((jarEntry = jar.getNextJarEntry()) != null) { | |
String jarEntryName = jarEntry.getName(); | |
File entry = new File(dir, jarEntryName); | |
if (jarEntryName.endsWith(ZIP_FILE_PATH_SEPARATOR)) | |
{ | |
entry.mkdirs(); | |
} | |
else { | |
entry.createNewFile(); | |
FileOutputStream out = new FileOutputStream(entry); | |
byte[] buffer = new byte[1024]; | |
int readCount = 0; | |
while ((readCount = jar.read(buffer)) >= 0) { | |
out.write(buffer, 0, readCount); | |
} | |
jar.closeEntry(); | |
out.flush(); | |
out.close(); | |
} | |
log.debug("File or directory has been extracted to {}", | |
entry.getAbsolutePath()); | |
} | |
jar.close(); | |
} | |
catch (FileNotFoundException e) { | |
throw new AnacondaException("SDG Initialization Failed:", e); | |
} | |
catch (IOException e) { | |
throw new AnacondaException("SDG Initialization Failed:", e); | |
} | |
log.debug("Finished extracting resources."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment