JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to decompress byte array in java?


Description:

java.util.zip package provides Inflater class to decompress byte array. The sample code to decompress a byte array is given in the below example. Also the example contains a code to compress the byte array before decompressing it.


Code:
/**
* Decompression class
**/
package com.java2novice.zip;

import java.io.ByteArrayOutputStream;
import java.util.zip.Inflater;

public class MyByteArrayDecompress {

public byte[] decompressByteArray(byte[] bytes){
		
		ByteArrayOutputStream baos = null;
		Inflater iflr = new Inflater();
		iflr.setInput(bytes);
		baos = new ByteArrayOutputStream();
		byte[] tmp = new byte[4*1024];
		try{
			while(!iflr.finished()){
				int size = iflr.inflate(tmp);
				baos.write(tmp, 0, size);
			}
		} catch (Exception ex){
			
		} finally {
			try{
				if(baos != null) baos.close();
			} catch(Exception ex){}
		}
		
		return baos.toByteArray();
	}

	public static void main(String a[]){
		
		MyByteArrayCompress mbc = new MyByteArrayCompress();
		byte[] content = mbc.compressByteArray("Compress java2novice.com".getBytes());
		System.out.println("Compressed Data: "+new String(content));
		MyByteArrayDecompress mba = new MyByteArrayDecompress();
		byte[] decom = mba.decompressByteArray(content);
		System.out.println("Decomplressed Data: "+new String(decom));
	}
}

/**
* Compression class
**/
package com.java2novice.zip;

import java.io.ByteArrayOutputStream;
import java.util.zip.Deflater;

public class MyByteArrayCompress {

	public byte[] compressByteArray(byte[] bytes){
		
		ByteArrayOutputStream baos = null;
		Deflater dfl = new Deflater();
		dfl.setLevel(Deflater.BEST_COMPRESSION);
		dfl.setInput(bytes);
		dfl.finish();
		baos = new ByteArrayOutputStream();
		byte[] tmp = new byte[4*1024];
		try{
			while(!dfl.finished()){
				int size = dfl.deflate(tmp);
				baos.write(tmp, 0, size);
			}
		} catch (Exception ex){
			
		} finally {
			try{
				if(baos != null) baos.close();
			} catch(Exception ex){}
		}
		
		return baos.toByteArray();
	}
	
	public static void main(String a[]){
		
		MyByteArrayCompress mbc = new MyByteArrayCompress();
		byte[] content = mbc.compressByteArray("Compress java2novice.com".getBytes());
		System.out.println(new String(content));
	}
}

Output:
Compressed Data: xs-(J-.VJ,K4/LNKst	2
Decomplressed Data: Compress java2novice.com
<< Previous Program | Next Program >>

List of all java.util.zip class sample examples:

  1. How to compress byte array in java?
  2. How to decompress byte array in java?
  3. How to zip a single file?
  4. How to zip multiple files?
  5. How to read zip files entries or file name list?
  6. How to unzip files in java?
  7. How to generate checksum value for for a file in java?
  8. How to compress and store objects using zip utility?
  9. How to decompress the compressed objects using zip utility?
  10. How to zip a file using ZipFile class?
  11. How to compress a file in GZip format?
  12. How to uncompress a file from GZip format?
Knowledge Centre
Can we call servlet destory() from service()?
As you know, destory() is part of servlet life cycle methods, it is used to kill the servlet instance. Servlet Engine is used to call destory(). In case, if you call destory method from service(), it just execute the code written in the destory(), but it wont kill the servlet instance. destroy() will be called before killing the servlet instance by servlet engine.
Famous Quotations
Never argue with a fool, onlookers may not be able to tell the difference.
-- Mark Twain

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected] or you can comment on the page.

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.