67

I have a web program where I want the user to be able to import a .war file and I can extract certain files out of the .war file. I have found two class libraries: java.util.zip.* and java.util.jar.*. From what I understand, a WAR file is a special JAR file which is a special ZIP file. So would it be better to use java.util.jar? If ZIP and JAR files are pretty much the same why is there a need for two different libraries?

Tom11
  • 2,419
  • 8
  • 30
  • 56
user972276
  • 2,973
  • 9
  • 33
  • 46

9 Answers9

144

WAR file is just a JAR file, to extract it, just issue following jar command –

jar -xvf yourWARfileName.war

If the jar command is not found, which sometimes happens in the Windows command prompt, then specify full path i.e. in my case it is,

 c:\java\jdk-1.7.0\bin\jar -xvf my-file.war
marko
  • 5
  • 4
iltaf khalid
  • 9,928
  • 5
  • 30
  • 34
26

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.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
18

Just rename the .war into .jar and unzip it using Winrar (or any other archive manager).

Tom11
  • 2,419
  • 8
  • 30
  • 56
Mehdi
  • 357
  • 3
  • 11
6

If you using Linux or Ubuntu than you can directly extract data from .war file.

A war file is just a jar file, to extract it, just issue following command using the jar program:

jar -xvf yourWARfileName.war
Ferrybig
  • 18,194
  • 6
  • 57
  • 79
Karan
  • 557
  • 6
  • 17
1

You can use a turn-around and just deploy the application into tomcat server: just copy/paste under the webapps folder. Once tomcat is started, it will create a folder with the app name and you can access the contents directly

M. Dhaouadi
  • 617
  • 10
  • 27
1

For mac users: in terminal command :

unzip yourWARfileName.war
Olivier Royo
  • 810
  • 10
  • 13
1

This is the way to unarchive war

mkdir mywarfile
cp -r mywarfile.war mywarfile/
cd mywarfile/
jar -xvf mywarfile.war
ls
rm -rf mywarfile.war

To cut multiple lines in vi editor, you can use the following steps:

a8350801-7572-4a74-8559-5743fd2a3e86

0

Like you said, a jar is a zip file (not a special type, but just a plain old zip), so either library could be made to work. The reasoning is that the average person, seeing a *.zip extension, tends to unzip it. Since the app server wants it unzipped, a simple rename keeps people from unzipping it simply out of habit. Likewise, *.war file also should remain uncompressed.

java.util.jar basically just adds additional functionality to java.util.zip with very little extra overhead. Let the java.util.jar be a helper in posting, etc... and use it.

Affable Geek
  • 465
  • 1
  • 9
  • 19
0

Jar class/package is for specific Jar file mechanisms where there is a manifest that is used by the Jar files in some cases.

The Zip file class/package handles any compressed files that include Jar files, which is a type of compressed file.

The Jar classes thus extend the Zip package classes.

r0ast3d
  • 2,639
  • 1
  • 14
  • 18