Home  >  Core Java

JAR Packer and Unpacker with Pack200 Format in Java

By Arvind Rai, January 16, 2014
Java provides an approach to reduce the size of Jar so that it can travel on HTTP network faster to deploy jar on a website. Pack200 is a format to compress and decompress jar files. It is a HTTP compression that runs faster on network. Pack200 has been specified in JSR 200. There are two components in Pack200. One is used to pack jar files with .pack extension and other is used to unpack .pack file into jar files. The file with .pack will be smaller in size in comparison to original jar file and can travel on network faster. And later at destination, it will be unpacked.
JAR Packer and Unpacker with Pack200 Format in Java

Pack200 in Java

java.util.jar.Pack200 is an abstract class which has been introduced in JDK 5. Pack200 has two static methods as given below.
newPacker(): It returns the Pack200.Packer instance that packs a jar file.
newUnpacker(): It returns the Pack200.Unpacker instance that unpacks the jar file.

Example of Pack200.Packer in Java

java.util.jar.Pack200.Packer is an interface that has been introduced in JDK 5. Packer is instantiated using Pack200. newPacker() method. Packer applies many transformations to the input jar file so that the stream can be highly compressed using any compression method like gzip and zip. In our example we will read a jar file and convert it into .pack file.
PackerDemo.java
package com.concretepage.util.jar;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.jar.JarFile;
import java.util.jar.Pack200;
import java.util.jar.Pack200.Packer;
public class PackerDemo {
  public static void main(String[] args) {
           Packer packer = Pack200.newPacker();
           Map<String,String> packProp = packer.properties();
           packProp.put(Packer.EFFORT, "6");
           packProp.put(Packer.SEGMENT_LIMIT, "-1");
           packProp.put(Packer.KEEP_FILE_ORDER, Packer.FALSE);
           packProp.put(Packer.MODIFICATION_TIME, Packer.LATEST);
           packProp.put(Packer.DEFLATE_HINT, Packer.FALSE);
           packProp.put(Packer.CODE_ATTRIBUTE_PFX+"LineNumberTable", Packer.STRIP);
           packProp.put(Packer.UNKNOWN_ATTRIBUTE, Packer.ERROR);
           packProp.put(Packer.PASS_FILE_PFX+0, "mutants/Rogue.class");
           try {
               JarFile jarFile = new JarFile("D:/cp/jar/testjar.jar");
               FileOutputStream fos = new FileOutputStream("D:/cp/testpack.pack");
               packer.pack(jarFile, fos);
               jarFile.close();
               fos.close();
               System.out.println("Done");
           } catch (IOException e) {
               e.printStackTrace();
           }
  }
}
 
Now compare the size of .jar and .pack file. We can observe that size of .pack has been reduced. In the next example we will unpack the .pack file and convert it into jar.

Example of Pack200.Unpacker in Java

java.util.jar.Pack200.Unpacker is an interface that has been introduced in JDK 5. Unpacker is instantiated using Pack200. newUnpacker() method. The role of Unpacker is unpacking .pack file into jar file. Now the unpacked jar will contain "PACK200" string as a zip file comment. In the example we are unpacking the .pack file which has been packed by first example in this page
UnpackerDemo.java
package com.concretepage.util.jar;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.jar.JarOutputStream;
import java.util.jar.Pack200;
import java.util.jar.Pack200.Unpacker;
public class UnpackerDemo {
        public static void main(String[] args) {
           try {
              Unpacker unpacker = Pack200.newUnpacker();
               File file = new File("D:/cp/testpack.pack");
               FileOutputStream fos = new FileOutputStream("D:/cp/jar/test.jar");
               JarOutputStream jostream = new JarOutputStream(fos);
               unpacker.unpack(file, jostream);
               jostream.close();
               System.out.println("Done");
           } catch (IOException e) {
               e.printStackTrace();
           }
         }
}
 
Now check the file locations given in example where you will get the jar after packing and unpacking original jar.
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us