Compress Byte Array Using Deflater Example

  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. */

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine

Related Java Examples





    Suggested Reading

    Oracle Magazine
    Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
    Cost: FREE

    View/Subscribe
    NASA Tech Briefs
    Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
    Cost: FREE

    View/Subscribe
    FierceBiotech IT
    Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
    Cost: FREE

    View/Subscribe
    Simply SQL
    Simply SQL is a practical step-by-step guide to writing SQL.
    Cost: FREE

    View/Subscribe
    Simply JavaScript
    Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
    Cost: FREE

    View/Subscribe
    PCMag.com's What's New Now
    Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
    Cost: FREE

    View/Subscribe

    Could not find what you are looking for? Search Java Examples




    Feel Tired? Read Jokes & Inspirational Stories, Play Games!