skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Friday, October 5, 2012

Compress/Decompress Data in Java

Posted by Raju Gupta at 12:39 PM – 0 comments
 

How to compress the data which are passed over the network in java in order to improve the performance.


import java.util.zip.*;
import java.io.*;


public class Compression {
    public Compression() {
    }
    public static byte[] compressData(String stringToCompress)
    {
      byte[] input = stringToCompress.getBytes();  
   System.out.println(" Original Lenght of Bytes " + input.length);    
      // Compressor with highest level of compression
      Deflater compressor = new Deflater();
      compressor.setLevel(Deflater.BEST_COMPRESSION);      
      // Give the compressor the data to compress
      compressor.setInput(input);
      compressor.finish();      
      // Create an expandable byte array to hold the compressed data.
      // It is not necessary that the compressed data will be smaller than
      // the uncompressed data.
      ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);      
      // Compress the data
      byte[] buf = new byte[1024];
      while (!compressor.finished()) {
          int count = compressor.deflate(buf);
          bos.write(buf, 0, count);
      }
      try {
          bos.close();
      } catch (IOException e) {
      }
      byte[] compressedData = bos.toByteArray();  
      return compressedData;
     
 }
 public static byte[] decompressData(byte[] byteArrayToDecompress)
 {
   System.out.println(" After Compression " + byteArrayToDecompress.length);    
   Inflater decompressor = new Inflater();
      decompressor.setInput(byteArrayToDecompress);      
      // Create an expandable byte array to hold the decompressed data
      ByteArrayOutputStream bos1 = new ByteArrayOutputStream(byteArrayToDecompress.length);      
      // Decompress the data
      byte[] buf1 = new byte[1024];
      while (!decompressor.finished()) {
          try {
              int count = decompressor.inflate(buf1);
              bos1.write(buf1, 0, count);
          } catch (DataFormatException e) {
          }
      }
      try {
          bos1.close();
      } catch (IOException e) {
      }      
      // Get the decompressed data
      byte[] decompressedData = bos1.toByteArray();  
      return decompressedData;     
  
 }
  
    public static void main(String args[]){
        
     try
     {
          
     byte[] compress = compressData("Test String Test String  Test String  Test String  Test String ");
     byte[] decompress = decompressData(compress);                
      
  }catch(Exception e)
  {
   e.printStackTrace();
  }
     
    }
}    


Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger