Skip to Content

Compress Byte Array Using Deflater Example

  • strict warning: Non-static method view::load() should not be called statically in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_argument.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter_boolean_operator.inc on line 0.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_style_default.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.

  1. /*
  2.         Compress Byte Array Using Deflater Example
  3.         This Java example shows how to compress the byte array using
  4.         Java Deflater class.
  5.        
  6.         Deflater class provides support for general purpose compression
  7.         using ZLIB compression library.
  8. */
  9.  
  10. import java.io.ByteArrayOutputStream;
  11. import java.io.IOException;
  12. import java.util.zip.Deflater;
  13.  
  14. public class CompressByteArray {
  15.        
  16.         public static void main(String args[]){
  17.                 String str = "aaaaaaaaaa bbbbbbbb cccccc";
  18.                 //String str = "Compress byte array using Deflater example";
  19.                
  20.                 //get bytes
  21.                 byte[] bytes = str.getBytes();
  22.                
  23.                 /*
  24.                  * To create an object of Deflater, use
  25.                  *
  26.                  * Deflater()
  27.                  * Constructor of Deflater class.
  28.                  *
  29.                  * This will create a new compressor with
  30.                  * default compression level.
  31.                  */
  32.                  
  33.                  Deflater deflater = new Deflater();
  34.                  
  35.                  /*
  36.                   * Set the input of compressor using,
  37.                   *
  38.                   * setInput(byte[] b)
  39.                   * method of Deflater class.
  40.                   */
  41.                  
  42.                   deflater.setInput(bytes);
  43.                  
  44.                   /*
  45.                    * We are done with the input, so say finish using
  46.                    *
  47.                    * void finish()
  48.                    * method of Deflater class.
  49.                    *
  50.                    * It ends the compression with the current contents of
  51.                    * the input.
  52.                    */
  53.                    
  54.                    deflater.finish();
  55.                    
  56.                    /*
  57.                     * At this point, we are done with the input.
  58.                     * Now we will have to create another byte array which can
  59.                     * hold the compressed bytes.
  60.                    */
  61.                    
  62.                    ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
  63.                    
  64.                    byte[] buffer = new byte[1024];
  65.                    
  66.                    /*
  67.                     * Use
  68.                     *
  69.                     * boolean finished()
  70.                     * method of Deflater class to determine whether
  71.                     * end of compressed data output stream reached.
  72.                     *
  73.                     */
  74.                    while(!deflater.finished())
  75.                    {
  76.                                 /*
  77.                                  * use
  78.                                  * int deflate(byte[] buffer)
  79.                                  * method to fill the buffer with the compressed data.
  80.                                  *
  81.                                  * This method returns actual number of bytes compressed.
  82.                                  */
  83.                                  
  84.                                  int bytesCompressed = deflater.deflate(buffer);
  85.                                  bos.write(buffer,0,bytesCompressed);
  86.                    }
  87.                    
  88.                    try
  89.                    {
  90.                            //close the output stream
  91.                            bos.close();
  92.                    }
  93.                    catch(IOException ioe)
  94.                    {
  95.                                 System.out.println("Error while closing the stream : " + ioe);
  96.                    }
  97.                    
  98.                    //get the compressed byte array from output stream
  99.                    byte[] compressedArray = bos.toByteArray();
  100.                    
  101.                    System.out.println("Byte array has been compressed!");
  102.                    System.out.println("Size of original array is:" + bytes.length);
  103.                    System.out.println("Size of compressed array is:" + compressedArray.length);
  104.                    
  105.         }
  106. }
  107.  
  108. /*
  109. Output of this program would be
  110. Byte array has been compressed!
  111. Size of original array is:26
  112. Size of compressed array is:18
  113. */