Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
tejaswini
Posts: 211
Registered: ‎04-29-2009
My Device: Not Specified
Accepted Solution

Compress and Decompress Data using GZip

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.

 

Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Compress and Decompress Data using GZip

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"

 

 

 

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Compress and Decompress Data using GZip

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; } }

 

 

 

New Contributor
mihirpmehta
Posts: 5
Registered: ‎12-23-2011
My Device: 9900

Re: Compress and Decompress Data using GZip

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 ?

New Contributor
mihirpmehta
Posts: 5
Registered: ‎12-23-2011
My Device: 9900

Re: Compress and Decompress Data using GZip

How to write these Compressed data in files ... can anyone gice sample code for compressing a File ?