• Post Reply Bookmark Topic Watch Topic
  • New Topic

decompressing byte[]  RSS feed

 
wo shi yao ming
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, sorry for taking your time. however i am encountering problem reguarding compress and uncompressing of byte[].

below is the codes i used for compression
----------
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
try
{
java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(baos);
oos.writeObject(object);
}
catch (java.io.IOException ioe)
{
java.util.logging.Logger.global.log(java.util.logging.Level.SEVERE, ioe.getMessage());
}

byte[] bytearr = baos.toByteArray();
Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
deflater.setInput(bytearr);
deflater.finish();

deflater.deflate(bytearr);
byte[] compressedByte = new byte[deflater.getTotalOut()];
System.arraycopy(bytearr, 0, compressedByte, 0, size);

return compressedByte;
----------
 
wo shi yao ming
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the codes i use for uncompression.
----------
Inflater inflater = new Inflater();

inflater.setInput(compressedByte);
inflater.finished();
inflater.inflate(cardByte);
int size = inflater.getTotalOut();
byte[] decompressedByte= new byte[size];

System.arraycopy(compressedByte, 0, decompressedByte, 0, size);
ByteArrayInputStream bains = new ByteArrayInputStream(cardByteUC)
Object object = new java.io.ObjectInputStream(bains).readObject();
----------
however i can't get any data out. keep geting java.io.StreamCorruptedException. another problem i see is that even after decompress the data my size of my byte[] is still the same which shouldn't be the case.

can someone kindly guide me?
 
wo shi yao ming
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the time and i solve the problem, below if the working form of the code for anyone who would encounter this problem.

---------------------------------------------------------------

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

public class dataZip {

public static void main(String[] args) {
// TODO Auto-generated method stub

Object object = "testing class";
System.out.println("object=" + object);

byte[] input = compress(object);
Object output = decompress(input);
System.out.println("object=" + output);

}

public static byte[] compress(Object input) {

java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();

try {
java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(baos);
oos.writeObject(input);
}
catch (java.io.IOException ioe) {
java.util.logging.Logger.global.log(java.util.logging.Level.SEVERE, ioe
.getMessage());
}

byte[] bytearr = baos.toByteArray();
Deflater compress = new Deflater(Deflater.BEST_COMPRESSION);
// deflater.setLevel(Deflater.BEST_COMPRESSION);
compress.setInput(baos.toByteArray());
compress.finish();

byte[] tempStoring = new byte[bytearr.length];
compress.deflate(tempStoring);
byte[] compressedByte = new byte[compress.getTotalOut()];
System.arraycopy(tempStoring, 0, compressedByte, 0, compress.getTotalOut());

return compressedByte;

}

public static Object decompress(byte[] output) {

Inflater decompress = new Inflater();
decompress.setInput(output);
decompress.finished();

byte[] cardByteUC = new byte[1024];

try {
decompress.inflate(cardByteUC);
}
catch (Exception e) {
// TODO: handle exception
System.out.println("Exception()." + e);
}

byte[] uncompressByte = new byte[decompress.getTotalOut()];
System
.arraycopy(cardByteUC, 0, uncompressByte, 0, decompress.getTotalOut());
decompress.end();

Object object = null;
ByteArrayInputStream bains = new ByteArrayInputStream(uncompressByte);
try {
ObjectInputStream ois = new ObjectInputStream(bains);
object = ois.readObject();
}
catch (Exception e) {
// TODO: handle exception
System.out.println("Exception()." + e);
}
return object;
}

}
[ October 16, 2007: Message edited by: wo shi yao ming ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for posting back the solution - that might help other people who will find your post later.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!