Linux: Using zip/unzip to add, update, and remove files from a Java jar/war

If you need to manipulate a Java archive such as a jar or war, and do not have access to the jar utility (it only comes packaged with the JDK, not JRE), you can always use the standard GNU zip/unzip utilities.

Install

The zip and unzip utilities will both be installed with the ‘zip’ package.

sudo apt-get install zip

File listing

Here is how you get a listing of files inside a fictional archive named “my.war”:

unzip -l my.war

Update a single file

If you need to pull out a single file, modify it, then put it back in the archive:

unzip my.war WEB-INF/classes/log4j.properties -d .
vi WEB-INF/classes/log4j.properties
zip my.war WEB-INF/classes/log4j.properties

You can validate the content was changed using the command below, which just outputs the content from the archive to stdout.

unzip -p my.war WEB-INF/classes/log4j.properties

Delete single file

zip -d my.war WEB-INF/classes/log4j.properties

Delete entire directory

You wouldn’t want to delete the META-INF directory for a Java archive, but just as an example, this would be the command:

zip -d my.war META-INF/\*

 

REFERENCES

https://linux.die.net/man/1/zip

https://linux.die.net/man/1/unzip

https://unix.stackexchange.com/questions/59276/how-to-extract-only-a-specific-folder-from-a-zipped-archive-to-a-given-directory