Wednesday, November 9, 2016

Advance JAR compresser/decompresser using Java

Every Java installation comes with 2 exe which is pack200.exe and unpack200.exe located at C:\Program Files\Java\jdk<version>\jre\bin
These exe can be used to highly compress jar files that can be directly deployed, saving bandwidth and reducing download time.

Compress Jar:
pack200 myarchive.pack.gz myarchive.jar

In this example, myarchive.pack.gz is produced using the default pack200 settings.

Decompress Jar:
unpack200 myarchive.pack.gz myarchive.jar

In this example, the myarchive.jar is produced from myarchive.pack.gz using the default unpack200 settings.

Code Location:
https://github.com/csanuragjain/extra/tree/master/Jar%20Compresser

Program:
I created a program which can automatically compress and decompress the file input by user.

runProgram Method:
Already explained at https://cooltrickshome.blogspot.in/2016/11/runmonitor-external-program-using-java.html

Main method:
      public static void main(String[] args) throws IOException, InterruptedException {  
           // TODO Auto-generated method stub  
           Scanner s=new Scanner(System.in);  
           System.out.println("Press 1 to compress file and 2 to decompress");  
           int choice=s.nextInt();  
           if(choice==1)  
           {  
                Scanner s2=new Scanner(System.in);  
                System.out.println("Enter file to compress");  
                String file=s2.nextLine();  
                long origlength= new File(file).length();  
                runProgram(new String[]{"pack200","output.pack.gz",file});  
                long newlength= new File("output.pack.gz").length();  
                System.out.println("File compressed and named output.pack.gz, which saved "+((origlength-newlength)/1000)+" KB");  
                s2.close();  
           }  
           else  
           {  
                Scanner s2=new Scanner(System.in);  
                System.out.println("Enter file to decompress");  
                String file=s2.nextLine();  
                runProgram(new String[]{"unpack200",file,"output.jar"});  
                System.out.println("File uncompressed and named output.jar");  
                s2.close();  
           }  
           s.close();  
      }  

How it works:
1) Program asks if user wants to compress or decompress jar
2) If user opt to compress file then program ask for file to compress. It retrieves the original file length and then compress it. After compression it takes length of compressed file and subtract from original length. Lastly it print the success message with the size saving.
3) If user opts to decompress file then it ask file to decompress and run the unpack command over it to get the uncompressed version.

Sample Input:

Press 1 to compress file and 2 to decompress
2
Enter file to decompress
output.pack.gz
Process completed successfully

File uncompressed and named output.jar

Press 1 to compress file and 2 to decompress
1
Enter file to compress
my.jar
Process completed successfully
File compressed and named output.pack.gz, which saved 7000 KB

Full Program:
 package com.cooltrickshome;  
 import java.io.BufferedReader;  
 import java.io.File;  
 import java.io.IOException;  
 import java.io.InputStream;  
 import java.io.InputStreamReader;  
 import java.util.Scanner;  
 public class JARCompresser {  
      public static void runProgram(String[] program) throws InterruptedException, IOException  
      {  
           Process proc = Runtime.getRuntime().exec (program);  
           InputStream progOutput = proc.getInputStream ();  
           InputStreamReader inputReader=new InputStreamReader(progOutput);  
           BufferedReader reader = new BufferedReader(inputReader);            
           String line;  
           while ((line = reader.readLine()) != null)  
             {  
                     System.out.println(line);  
             }  
           if (0 == proc.waitFor ()) {  
             System.out.println("Process completed successfully");  
           }  
           else  
           {  
                System.out.println("Their was some issue while running the program");  
           }  
      }  
      public static void main(String[] args) throws IOException, InterruptedException {  
           // TODO Auto-generated method stub  
           Scanner s=new Scanner(System.in);  
           System.out.println("Press 1 to compress file and 2 to decompress");  
           int choice=s.nextInt();  
           if(choice==1)  
           {  
                Scanner s2=new Scanner(System.in);  
                System.out.println("Enter file to compress");  
                String file=s2.nextLine();  
                long origlength= new File(file).length();  
                runProgram(new String[]{"pack200","output.pack.gz",file});  
                long newlength= new File("output.pack.gz").length();  
                System.out.println("File compressed and named output.pack.gz, which saved "+((origlength-newlength)/1000)+" KB");  
                s2.close();  
           }  
           else  
           {  
                Scanner s2=new Scanner(System.in);  
                System.out.println("Enter file to decompress");  
                String file=s2.nextLine();  
                runProgram(new String[]{"unpack200",file,"output.jar"});  
                System.out.println("File uncompressed and named output.jar");  
                s2.close();  
           }  
           s.close();  
      }  
 }  

Hope this helps :)

No comments:

Post a Comment