Skip to content

Instantly share code, notes, and snippets.

@ffbit
Created August 3, 2012 07:46
Show Gist options
  • Save ffbit/3245533 to your computer and use it in GitHub Desktop.
Save ffbit/3245533 to your computer and use it in GitHub Desktop.
Example of extracting a jar file from java
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