|
|
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: x�s��-(J-.V�J,K4��/�LN�K��st 2
Decomplressed Data: Compress java2novice.com
|
|
|
|
|
List of all java.util.zip class sample examples:- How to compress byte array in java?
- How to decompress byte array in java?
- How to zip a single file?
- How to zip multiple files?
- How to read zip files entries or file name list?
- How to unzip files in java?
- How to generate checksum value for for a file in java?
- How to compress and store objects using zip utility?
- How to decompress the compressed objects using zip utility?
- How to zip a file using ZipFile class?
- How to compress a file in GZip format?
- How to uncompress a file from GZip format?
|
|
|
Class, Constructor and Primitive data types
Class is a template for multiple objects with similar features and it is a blue print for objects. It defines a type of object
according to the data the object can hold and the operations the object can perform. Constructor is a special kind of method that
determines how an object is initialized when created. Primitive data types are 8 types and they are: byte, short, int, long, float,
double, boolean, char.
There is a great difference between worry and concern. A worried person sees a problem, and a concerned person solves a problem.
-- Harold Stephens
|