Extract/See Content of a Specific File Inside a .War File

Seeing contents of war file without extracting

The t option to the command line jar program will list the contents of a jar (or war) file, e.g.:

$ jar tf the-file.war

Extract/See content of a specific file inside a .war file

jar has x option for that:

$ jar -help 2>&1 | grep extract
-x extract named (or all) files from archive

Let's try it:

$ jar tvf target/my.war | grep test-data
1811 Tue Jun 21 19:34:50 CEST 2016 WEB-INF/classes/test/spring/test-data.properties
$ jar xvf target/my.war WEB-INF/classes/test/spring/test-data.properties
inflated: WEB-INF/classes/test/spring/test-data.properties
$ ls -l WEB-INF/classes/test/spring/test-data.properties
-rw-rw-r--. 1 coder coder 1811 Jun 21 19:34 WEB-INF/classes/test/spring/test-data.properties

How to extract only one file from war file?

The -j flag is exactly what you need:

unzip -j test.war "*test.properties*" 

From the docs:

-j

junk paths. The archive's directory structure is not recreated; all
files are deposited in the extraction directory (by default, the
current one).

One liner for listing contents of file within war file

Is this what you are looking for?

unzip -qc SOME_WAR_FILE.war META-INF/MANIFEST.MF

See the previous answer here.

How to extract .war files in java? ZIP vs JAR

If you look at the JarFile API you'll see that it's a subclass of the ZipFile class.

The jar-specific classes mostly just add jar-specific functionality, like direct support for manifest file attributes and so on.

It's OOP "in action"; since jar files are zip files, the jar classes can use zip functionality and provide additional utility.

How do I access a text file from within my war

Check out ServletContext.getResourceAsStream(). Here is an example.



Related Topics



Leave a reply



Submit