Core Java

Explode a WAR File Recursively

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:

  1. A JAR file
  2. A WAR file which every JAR file it finds also exploded.
  3. 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

01
02
03
04
05
06
07
08
09
10
11
12
13
14
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

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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.

Published on Java Code Geeks with permission by Michael Remijan, partner at our JCG program. See the original article here: Explode a WAR File Recursively

Opinions expressed by Java Code Geeks contributors are their own.

Michael Remijan

Michael Remijan is a System Architect at the Federal Reserve Bank St. Louis. He is co-author of 'EJB 3 In Action Second', an active blogger in the Java EE community, a Java EE Guardian, and JavaOne presenter. He has developed enterprise systems for B2C and B2B commerce, manufacturing, astronomy, agriculture, telecommunications, national defense, healthcare, and financial areas.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Elena gillbert
4 years ago

Hi…
I’m Elena gillbert.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.

Back to top button