Abstract
Ever need to explode a WAR file as well as exploding all JAR files in the WAR file? Ya, me too!
I wrote ferris-war-exploder to explode either:
- A JAR file
- A WAR file which every JAR file it finds also exploded.
- An EAR file with every JAR file (see #1) and WAR file (see #2) also exploded.
Basically, ferris-war-exploder explodes anything which is a ZIP file format. Any entries which are in a ZIP file format will also be exploded. This happens recursively so anything that can be exploded is exploded.
Disclaimer
This post is solely informative. Critically think before using any information presented. Learn from it but ultimately make your own decisions at your own risk.
Requirements
I did all of the work for this post using the following major technologies. You may be able to do the same thing with different technologies or versions, but no guarantees.
- NetBeans 11.2
- Maven 3.3.9 (Bundled with NetBeans)
- Java 11 (zulu11.35.15-ca-jdk11.0.5-win_x64)
Download
Visit my GitHub page https://github.com/mjremijan to see all of my open source projects. The code for this post is located at: https://github.com/mjremijan/ferris-war-exploder
Let’s get to it
ferris-war-exploder explodes anything which is a ZIP file format. Any entries which are in a ZIP file format will also be exploded. This happens recursively so anything that can be exploded is exploded.
YOU need to tell it the archive (WAR, JAR, EAR, ZIP) to explode.
YOU need to tell it where to explode the archive.
NOTE See my ferris-magic-number to analyze all of the
.class
files once the WAR is exploded.
Listing 1 shows the main()
method to start the application. I have 2 examples: Exploding a JAR and exploding a WAR.
Listing 1 - The main()
method
public class Main {
public static void main(String[] args) throws Exception
{
System.out.printf("=== Welcome to Ferris WAR Exploder ===%n");
new Unzip("./src/test/jars/commons-lang3-3.7.jar", "./target/unzipped/jar")
.unzip();
new Unzip("./src/test/wars/sample.war", "./target/unzipped/war")
.unzip();
System.out.printf("%n=== DONE ===%n");
}
}
Listing 2 shows the Unzip
class. This class contains the interesting code to recursively explode an archive. Nothing in Listing 2 is difficult to understand, so I’ll leave it up to you to read through.
Listing 2 - The Unzip
method
package org.ferris.war.exploder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
/**
*
* @author Michael Remijan mjremijan@yahoo.com @mjremijan
*/
public class Unzip {
protected File zipFile;
protected File destinationDirectory;
public Unzip(String zipFilePath, String destinationDirectoryPath) {
setZipFile(zipFilePath);
setDestinationDirectory(destinationDirectoryPath);
}
public Unzip(File zipFile) {
this.zipFile = zipFile;
setDestinationDirectory(zipFile.getParent());
}
protected void setDestinationDirectory(String destinationDirectoryPath) {
destinationDirectory = new File(destinationDirectoryPath, zipFile.getName());
if (destinationDirectory.exists() && destinationDirectory.isDirectory()) {
throw new RuntimeException(
String.format(
"The destination directory \"%s\" already exists.",
destinationDirectory.getPath()
)
);
}
if (destinationDirectory.exists() && destinationDirectory.isFile()) {
destinationDirectory = new File(destinationDirectoryPath, zipFile.getName() + ".d");
}
mkdirs(destinationDirectory,
"Failed to create the destination directory \"%s\"."
);
}
protected void setZipFile(String zipFilePath) {
zipFile = new File(zipFilePath);
if (!zipFile.exists()) {
throw new RuntimeException(
String.format(
"The file \"%s\" does not exist", zipFile.getPath()
)
);
}
if (!zipFile.canRead()) {
throw new RuntimeException(
String.format(
"The file \"%s\" is not readable", zipFile.getPath()
)
);
}
}
protected void unzip() throws Exception {
System.out.printf("%n=== Unipping %s ===%n%n", zipFile.getPath());
try (ZipInputStream zip
= new ZipInputStream(new FileInputStream(zipFile));
){
for (ZipEntry z = zip.getNextEntry(); z != null; z = zip.getNextEntry()) {
if (z.isDirectory()) {
mkdirs(new File(destinationDirectory, z.getName()),
"Failed to create a zip entry directory \"%s\""
);
} else {
File zfile = new File(destinationDirectory, z.getName());
mkdirs(zfile.getParentFile(),
"Failed to create parent directory for zip entry file \"%s\"."
);
File unzippedFile = unzipEntry(z, zip);
if (isZip(unzippedFile)) {
new Unzip(unzippedFile).unzip();
}
}
}
}
}
protected boolean isZip(File file) {
boolean b = false;
try {
b = new ZipFile(file).getName().length() > 0;
} catch (IOException ignore) {}
return b;
}
protected File unzipEntry(ZipEntry z, ZipInputStream zip) throws Exception {
File zfile = new File(destinationDirectory, z.getName());
System.out.printf(" %s%n", zfile.getAbsolutePath());
try ( FileOutputStream out = new FileOutputStream(zfile)) {
zip.transferTo(out);
}
zip.closeEntry();;
return zfile;
}
protected void mkdirs(File dir, String errorMessageFormat) {
if (dir.exists() && dir.isDirectory()) {
return;
}
dir.mkdirs();
if (!dir.exists()) {
throw new RuntimeException(
String.format(errorMessageFormat, dir.getPath()
)
);
}
}
}
Summary
The ferris-war-exploder project isn’t too complicated, but it is very handy when you need to completely explode a WAR or EAR archive. Enjoy!
References
ZipOutputStream. (n.d.). Oracle. Retrieved from https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/zip/ZipOutputStream.html.
No comments:
Post a Comment