Decompressing JAR file

JAR file is file archiver which collects/groups different files and folder into a format which represents one file.

The format of JAR file is an uncompressed format.

Pack200 is a tool that can be used to compress and decompress a jar file.

Below is a simple example of how we can use Pack200 tool to decompress the file

Main Code


1  package Pack200.UnPacker;
2  
3  import java.io.File;
4  import java.io.FileOutputStream;
5  import java.io.IOException;
6  import java.util.jar.JarOutputStream;
7  import java.util.jar.Pack200;
8  import java.util.jar.Pack200.Unpacker;
9  
10 public class Demo1 {
11  public static void main(String[] args) {
12      Unpacker unpacker = Pack200.newUnpacker();
13      File packFile = new File("test.pack");
14      
15      try (FileOutputStream jarFile = new FileOutputStream("jsonb1-ri-1.0.jar");
16          JarOutputStream jos = new JarOutputStream(jarFile)) {
17          unpacker.unpack(packFile, jos);
18      } catch(IOException excep) {
19          excep.printStackTrace();
20      }
21  }
22 }

Explanation

In the above code we decompress the test.pack file to jsonb-ri-1.0.jar file.

At line 12 we create an instance of Unpacker, which unzips the file, as shown below
Unpacker unpacker = Pack200.newUnpacker();

At line 13 we create the source file test.pack.

At line 15 adn 16 we create an output stream through which uncompressed data is sent to jsonb1-ri-1.0.jar.

At line 17, we decompress the jar file using unpack method, which takes two arguments which are
1) The source file which has to be unzipped.
2) The output stream through which uncompressed data has to be sent.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s