06-01-2009 08:38 AM
Hi,
I am compressing and decompressing data on Blackberry 8100 device using GZip algorithm.
I am using following code:
try { GZIPOutputStream gCompress = null; try { ByteArrayOutputStream compressByte = new ByteArrayOutputStream(); gCompress = new GZIPOutputStream(compressByte,9); gCompress.write(inputstring.getBytes()); //gCompress.flush(); compressedBytes = compressByte.toByteArray(); System.out.println("compressedBytes : " + new String(compressedBytes)); compressedString = new String(compressedBytes); } catch (IOException ex) { ex.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); }
The server is not able to decompress data .
Please help.
Thanks a lot.
Solved! Go to Solution.
06-01-2009 09:14 AM
You probably have to explicitly state that the content is GZIP so that the server will recognize it and unzip the payload.
Try setting the "Content-Encoding" header to "gzip"
06-01-2009 10:11 AM
I think you are doing a byte to String conversion which you should not be doing and which may be screwing up your data. The output from a compression is not likely to be easily represented as Unicode characters and you actually want to transmit the bytes, so I think this is not needed.
Here is a reworked version of the sample in the API, which I believe to be inaccurate, anyway: Give this a try and remember that you need to send the bytes, not the chars.
public static byte[] compress( byte[] data ) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzipStream = new GZIPOutputStream( baos, GZIPOutputStream.COMPRESSION_BEST, GZIPOutputStream.MAX_LOG2_WINDOW_LENGTH ); gzipStream.write( data ); gzipStream.close(); return baos.toByteArray(); } catch(IOException ioe) { return null; } }
01-10-2012 05:18 AM
Hi, using this i convert one audio file in to input stream and after compression the size of the return byte Array is actually more than what i give as parameter ...
any idea ?
01-10-2012 06:01 AM
How to write these Compressed data in files ... can anyone gice sample code for compressing a File ?