B4J Question [SOLVED] zlib decompression

bdunkleysmith

Active Member
Licensed User
Longtime User
As part of my continuing work on a project accessing an API referenced in this post, I have encountered a problem in regard to zlib decompression.

The API documentation says:

To reduce transfer size, the content of some messages are sent in a compressed format. This compressed data is available in the compressedData field in the message. The format of this field is a base64 encoded zlib compressed JSON string.

Python example for decompressing import base64 import zlib

Python:
data = "eJyl0b1uwyAQAOBXQcy1hLHjn65RorZppQ6VKnU74GKj2hABHqLI716ztK6aenA24Lj7TncXiibocH5U9J5QKZgSUPOEbzKRpCmKBJRME8ZzDjLljDFO7wg9ofPW+CnlQhWUWSWLfDEnfpRwCqDNdD5C53EqI7SIalbEmgZ6jLcXbJUme90M6CzEiA/gAropGNyA4/QSzZqhXG/mbG4+I5AP3ffoejD/k5WAYj2ZbubkrtPgyd6hke1fMc7Yeh20jYXoln63gGJ5O8uTLuctbHU3NWHIwV5r4WfOHG8Q67n4BGeFhjwghAWxOpZwg1jNxQN03dCTV3Cf6BdIwGo9+Wuxby1a8q6bRptr4Dh+AT9i+9U="

compressed_data = base64.b64decode(data.encode())
output = zlib.decompress(compressed_data).decode()
print(output)


However my code:

B4X:
    Dim parser As JSONParser
    parser.Initialize(JSON)
    Dim root As Map = parser.NextObject
    Dim compressedData As String = root.Get("compressedData")  
    Dim compData() As Byte = compressedData.GetBytes("UTF8")
    Dim compress As CompressedStreams
    Dim decompressed() As Byte
    decompressed = compress.DecompressBytes(compData, "zlib")
    Dim decompressedData As String
    decompressedData = BytesToString(decompressed, 0, decompressed.Length, "UTF8")
    Log(decompressedData)

throws the following error:

Loading libs...
BDSPlayerBoard V1 started at 1/5/2021 2:4:8
Java version: 11.0.1
main._mqttpersons (java line: 3287)
java.util.zip.ZipException: incorrect header check
at java.base/java.util.zip.InflaterInputStream.read(InflaterInputStream.java:165)
at java.base/java.io.FilterInputStream.read(FilterInputStream.java:107)
at anywheresoftware.b4a.objects.streams.File.Copy2(File.java:349)
at anywheresoftware.b4a.randomaccessfile.CompressedStreams.DecompressBytes(CompressedStreams.java:152)
at b4j.example.main._mqttpersons(main.java:3287)
at b4j.example.main._mqtt_messagearrived(main.java:3225)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.BA$3.run(BA.java:247)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)

Pasting the compressedData string into an online zlib decompression tool returns a valid decompressed string.

Any suggestions on why my code is not returning a valid decompressed string, but throwing the error? What does "incorrect header check" indicate?
 
Solution
Amazing what happens after a good night's sleep when your head is clear - I'd forgotten to firstly do a BASE64 decode!

B4X:
    Dim parser As JSONParser
    parser.Initialize(JSON)
    Dim root As Map = parser.NextObject
    Dim compressedData As String = root.Get("compressedData")
    Dim compress As CompressedStreams
    Dim decompressedData As String
    Dim B64 As Base64
    Dim compressed(), decompressed() As Byte
    compressed = B64.DecodeStoB(compressedData)               
    decompressed = compress.DecompressBytes(compressed, "zlib")
    decompressedData = BytesToString(decompressed, 0, decompressed.Length, "UTF8")
    Log(decompressedData)

bdunkleysmith

Active Member
Licensed User
Longtime User
Amazing what happens after a good night's sleep when your head is clear - I'd forgotten to firstly do a BASE64 decode!

B4X:
    Dim parser As JSONParser
    parser.Initialize(JSON)
    Dim root As Map = parser.NextObject
    Dim compressedData As String = root.Get("compressedData")
    Dim compress As CompressedStreams
    Dim decompressedData As String
    Dim B64 As Base64
    Dim compressed(), decompressed() As Byte
    compressed = B64.DecodeStoB(compressedData)               
    decompressed = compress.DecompressBytes(compressed, "zlib")
    decompressedData = BytesToString(decompressed, 0, decompressed.Length, "UTF8")
    Log(decompressedData)
 
Upvote 0
Solution

fshahzad11

New Member
@
[IMG alt="bdunkleysmith"]https://b4x-4c17.kxcdn.com/android/forum/data/avatars/m/35/35677.jpg?1403771644[/IMG]

bdunkleysmith

Dear Sir,
The Below is successfully working code in example 1 you shared:

Example 1:

import zlib

import base64

data = "eJyl0b1uwyAQAOBXQcy1hLHjn65RorZppQ6VKnU74GKj2hABHqLI716ztK6aenA24Lj7TncXiibocH5U9J5QKZgSUPOEbzKRpCmKBJRME8ZzDjLljDFO7wg9ofPW+CnlQhWUWSWLfDEnfpRwCqDNdD5C53EqI7SIalbEmgZ6jLcXbJUme90M6CzEiA/gAropGNyA4/QSzZqhXG/mbG4+I5AP3ffoejD/k5WAYj2ZbubkrtPgyd6hke1fMc7Yeh20jYXoln63gGJ5O8uTLuctbHU3NWHIwV5r4WfOHG8Q67n4BGeFhjwghAWxOpZwg1jNxQN03dCTV3Cf6BdIwGo9+Wuxby1a8q6bRptr4Dh+AT9i+9U="

compressed_data = base64.b64decode(data.encode())

output = zlib.decompress(compressed_data).decode()

print(output)


OutPut: {"entityId": "cb0dba92-253b-11eb-adc1-0242ac120002", "persons": {"da738c64-253b-11eb-adc1-0242ac120002": {"captain": false, "bib": "36", "name": "Mehdi Figueroa", "starter": true}, "da7390ec-253b-11eb-adc1-0242ac120002": {"captain": false, "bib": "40", "name": "Lea Zimmerman", "starter": true}, "da738ba6-253b-11eb-adc1-0242ac120002": {"captain": false, "bib": "15", "name": "Elias French", "starter": true, "position": "C"}, "da738eb2-253b-11eb-adc1-0242ac120002": {"captain": false, "bib": "37", "name": "Cillian Koch", "starter": true}, "da73902e-253b-11eb-adc1-0242ac120002": {"captain": false, "bib": "39", "name": "Jayden Heath", "starter": true}, "da738f7a-253b-11eb-adc1-0242ac120002": {"captain": false, "bib": "38", "name": "Kallum Parkes", "starter": true}, "da738ae8-253b-11eb-adc1-0242ac120002": {"captain": false, "bib": "5", "name": "Theo Wiggins", "starter": true}}}​

The following example 2 has the same code as in example 1 but generating error: How decompression can be made successful for example 2;

Example 2:

import zlib

import base64

data = "Lki49VBk7tfhyXk3WyzwMBqP1u1UCxKWLJZ/yBFdgWafU8Glyu97OhkFQje2Ds+KllTVTgl5oPXc9U79JHBYz9XN344ZNTpv9CMg9BX5y4VeuPDzW7985b4Aa2TUHfGSKBYsSezbbctkSAaRZVdhJ2sjqdZH3pylcJwzAHr+Sn+Wtnib/sJDLqdLGUK3LSbPSK8Q0KQfDP5lNfavdvg8kepBPbN+Wr/oB3mtuhRjcFXe4y9BRWQTi4qGYDQ8MMlIeanrx7IaHfOvSGyBKa0TVbkixzmX2i93l6qqvKaTwoREGfdZA2jZdzzIVZWlXYU/qPkrLy873BJ0ZhXg9TE6XQ==;MZfPM47CRpWLrPw0TRDnvla4aDkJsmV5TALa0slA1TDaZj2SLOQ4dlQpy754tkFkkqLqXjNXT/u2P9vEYE2rW98vErmaevsxVhNEbcqP3UQ2bJJEzI/0oSx4KSeqe+mL+vqRbFW+Maw1vZKb1KLUzsxXq50HgxAqFbc/E/oN46DpgGceIpv0H04a0seVyKd51fZImahHi4Y7XAvgp9Jex/wAryh3sC91dMY94uwGTSlrDLbggNmTZXOKcK+GB8e7coNt5jAPbaj69XHGGSEreek8RDFZJXCqWWXsrgF248/1aVkPJoKm7yaFeZn8/A6MT5YZvH3Jc9SEnkUKEBCFAg==;0SbhqVJFrTaGsFUqPVdyag==;p+yK2V8sgj4tK0OYCHS/TMU9N6mIhrolBq5oIMjtOsjL2NTne3/6roEb9tgrrwB/34ye8yTHMx5LgrooV5AMsGhh1+cQt4qRen4kLGRzLzYin2jYyeAFQqkJKFS77PtVeGrHvGC/o/bQ6cWl6k395eXDBqlI2+DV6WG052QRtQTXYTcfsneu1fYWbIRvlBF19WmGHRkrVJAaPcEpBlh00g=="

compressed_data = base64.b64decode(data.encode())

output = zlib.decompress(compressed_data).decode()

print(output)


Following error occured:

error: Error -3 while decompressing data: incorrect header check

Problem to be solved: How example two can give successful decompression?
 
Upvote 0

bdunkleysmith

Active Member
Licensed User
Longtime User
Thank you for your question @fshahzad11 however are you referring to the Python or B4X code I shared in the original post?

But in any event can I suggest to you as newly joined member that you create a new post in which to ask your question as per the protocol used in the Forum. Best not to append new questions to an existing post and it is more likely to result in assistance from the entire Forum community.
 
Upvote 0
Top