Pack and unpack with Pack200 in Java

Description

The following code shows how to pack and unpack with Pack200.

Example


/*from w ww  . j  av  a2s  .co  m*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.jar.JarOutputStream;
import java.util.jar.Pack200;
import java.util.zip.GZIPInputStream;

public class Main {

  public static void main(String[] args) throws Exception {

    String inName = "Main.java";
    String outName;
    if (inName.endsWith(".pack.gz")) {
      outName = inName.substring(0, inName.length() - 8);
    } else if (inName.endsWith(".pack")) {
      outName = inName.substring(0, inName.length() - 5);
    } else {
      outName = inName + ".unpacked";
    }

    JarOutputStream out = null;
    InputStream in = null;

    Pack200.Unpacker unpacker = Pack200.newUnpacker();
    out = new JarOutputStream(new FileOutputStream(outName));
    in = new FileInputStream(inName);
    if (inName.endsWith(".gz"))
      in = new GZIPInputStream(in);
    unpacker.unpack(in, out);
    out.close();
  }
}




















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip