string compression with gzip: no exception but output does not equal input
Hey folks,
i'm really having a hard time with a - propably - easy to solve problem.
I want too:
-> compress a string using gzip
-> decompress the string
I've written a simple main-class to test this, but unfortunately the resulting output string doesnt equal the input string which is the good old "foo"....
Here is my simple main:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
publicclass TestGzip{
publicstaticvoid main(String[] args)throws UnsupportedEncodingException{
byte[] baFileContent =new String("foo").getBytes();
// print out
System.out.println("original string as int: ");
String output ="";
for(int i = 0; i < baFileContent.length; i++){
output += baFileContent[i] +" ";
}
System.out.println(output);
/*
* compression
*/
ByteArrayOutputStream baos =new ByteArrayOutputStream();
GZIPOutputStream zos;
try{
zos =new GZIPOutputStream(baos);
for(int i = 0; i < baFileContent.length; i++){
zos.write(baFileContent[i]);
}
zos.close();
baos.close();
}catch (IOException e){
System.out.println(e.getMessage());
}
byte[] baFileContentCompressed = baos.toByteArray();
// print out
System.out.println("string compressed as int: ");
output ="";
for(int i = 0; i < baFileContentCompressed.length; i++){
output += baFileContentCompressed[i] +" ";
}
System.out.println(output);
/*
* decompression
*/
ByteArrayInputStream bais =new ByteArrayInputStream(baFileContentCompressed);
GZIPInputStream zis =null;
byte[] buffer =newbyte[ 8192 ];
// the result
byte[] baFileContentDecompressed =null;
baos =new ByteArrayOutputStream();
try{
zis =new GZIPInputStream(bais);
for(int len; (len = zis.read(buffer)) != -1; ){
baos.write(len);
}
zis.close();
bais.close();
baos.close();
baFileContentDecompressed = baos.toByteArray();
}catch (IOException e){
System.out.println(e.getMessage());
}
//print out
System.out.println("string decompressed as int: ");
output ="";
for(int i = 0; i < baFileContentDecompressed.length; i++){
output += baFileContentDecompressed[i] +" ";
}
System.out.println(output);
}
}
The output is:
original string asint:
102 111 111
string compressed asint:
31 -117 8 0 0 0 0 0 0 0 75 -53 -49 7 0 33 101 115 -116 3 0 0 0
string decompressed asint:
3
As you can see:
- input is "102 111 111"
- but the output is "3"
I'm pretty sure the error lies in the decompression code segment:
/*
* decompression
*/
ByteArrayInputStream bais =new ByteArrayInputStream(baFileContentCompressed);
GZIPInputStream zis =null;
byte[] buffer =newbyte[ 8192 ];
// the result
byte[] baFileContentDecompressedDecoded =null;
baos =new ByteArrayOutputStream();
try{
zis =new GZIPInputStream(bais);
for(int len; (len = zis.read(buffer)) != -1; ){
baos.write(len);
}
zis.close();
bais.close();
baos.close();
baFileContentDecompressedDecoded = baos.toByteArray();
}catch (IOException e){
System.out.println(e.getMessage());
}
but i just don't see any errors.....
Any help will be greatly appreciated!

