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
bobmoran
Posts: 25
Registered: ‎03-24-2009
My Device: Not Specified

ZLIB Inflate

Hi,

 

For my app i require to deflate and then inflate some data.

I have been looking at ZLibInputStream, ZLibOutputStream

I can deflate the data ok with the following code:

 

 

public String deflate (String xmlReq)
{ // deflate
ByteArrayOutputStream out = null;
ZLibOutputStream zout = null;

try { // try zip data
out = new ByteArrayOutputStream();
zout = new ZLibOutputStream(out);
zout.write(xmlReq.getBytes(), 0, xmlReq.getBytes().length);
zout.flush();

return out.toByteArray().toString();
} // try zip data
catch(IOException ioe) { // ioexception
System.out.println(ioe);
} // ioexception
catch(Exception ex) { // exception
System.out.println(ex);
} // exception
finally { // finally, try close streams
try { // try close streams
if(zout!=null)
zout.close();
if(out!=null)
out.close();
} // try close streams
catch(Exception e){}
} // finally, try close streams

return "";
} // deflate

 

However when i try to inflate the data i get an error:

 

"incorrect header check"

 

Here is my code for inflation:

 

public String inflate (String deflatedStr)
{ // inflate
String result = "";
ByteArrayInputStream in = null;
ZLibInputStream zin = null;
byte[] bytes = new byte[600];

try { // try inflate
in = new ByteArrayInputStream(deflatedStr.getBytes());
zin = new ZLibInputStream(in);
zin.read(bytes, 0, 600);

return bytes.toString();
} // try inflate
catch (Exception e) { // exception
System.out.println ("Exception: "+e.getMessage());
} // exception
finally { // finally, close streams
try { // try close streams
if(zin!=null)
zin.close();
if(in!=null)
in.close();
} // try close streams
catch (Exception e) {}
} // finally, close streams

return result;
} // inflate

 

I'd appreciate any help.

Best regards,

Bob.

 

 

Please use plain text.
Administrator
MSohm
Posts: 14,229
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: ZLIB Inflate

How are you receiving your data?  Do you get the same error if you supply the data as a byte array instead of a String?
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: ZLIB Inflate

I think the problem is in your ZLibInputStream initialization.You have to indicate whether header and trailer are included or not during initialization. 

 

You have to initialize ZlibInputStream like below:

ZLibInputStream(InputStream inputStream,boolean noWrap)

 

 

From javadoc:

"If noWrap is true, the decompressor will assume that the ZLib 16 bit header and the 32 bit trailer checksum are not included in the underlying input stream. This mode is necessary for PKZip/Info-ZIP and GZIP compatibility".

 

 

 

Regards

Bikas

 

 

 

Please use plain text.
Developer
bobmoran
Posts: 25
Registered: ‎03-24-2009
My Device: Not Specified

Re: ZLIB Inflate

I have tried receiving the data as byte[] as well as String.

Neither worked for me.

 

I have also tried various initializations ZlibInputStream, again with no joy.

 

However i have found an alternate solution using jzlib.

This is an open source re-implementation of zlib in pure java.

Anyone interested can take a look here:

http://www.jcraft.com/jzlib/

 

Thanks for yere help regardless.

 

Best Regards,

Bob.

Please use plain text.
Contributor
arirushan
Posts: 13
Registered: ‎03-30-2010
My Device: 8520
My Carrier: 3

Re: ZLIB Inflate

Did you find solution for this ?

 

Best Regards,

 

Ari

Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: ZLIB Inflate

Looking at this code, there is an obvious problem because the code converts the bytes to a String and then back to bytes again.  This will use some sort of encoding when converting, presumably ISO-8859-1.  In this case that will corrupt the ZLIB data.  Try running the code but instead of trying to pass a String around, pass the compressed bytes, then try to inflate the compressed bytes.

 

Generally, however I would recommend that you use GZIP processing, which does the same thing except it adds a GZIP header.  So the compressed data is slightly larger (only slightly) than it would be if you used ZLIB and the GZIP format is understood across a wide variety of systems. 

Please use plain text.