Unknown why do I get a :data_error on :zlib.unzip?

Hi there!

I have a zipped, base64 encoded string of xml data. If I decode it, & unzip it with elixir I get a data_error:

raw = "UEsDBBQAAgAIAOiJM1JK4iCoeQIAAMcHAAAoAAAAODdjMjdkMjUtOTA2My00NjgzLTlkOGUtOGMxMGU3NjZiMDUwLnhtbI1U23LaMBB9z1cweQ++4WAY6hmgtzw0yTQJ0z51BF6MqJFTSYSWr+/akmz5wrR+8fjs2d2zxyvN9qfAC6cL4HSzk4Pfh4yJKd2f3l3vpHydOs7pdBpSIQlLCOEJsCHLHIw7wQ/XWRNBhdjs4EAc9brWFcqq/1WjZJrs+GowmClFn4EkwAuggrTIZZ5APBrdzpwu3ENfARcU4qBF13BPwtNx/aaCbiunjthp8+0ZWKHVx2ccBJHJqgI2+4HJN8JSRN1gNDLUGu0RdIeWSbqlGyJNa6SggdNm5MvSdaNocjsPw8nM6cbtzPcklfATGGVp7Lu+d+N6N57OsmNKjvMvPVrwN5HYvupei3pNaoI39Ieu6tcbbxRQfe1kd+jr5HasobgBG7BeLS17mVGcqGm9YPEEHxefetMQtEmfYJ3nXEJC5PHQsrdAfEwujHU9Y2zFMyV7SlTFRUZwstiruRqxafeENFsblmBWRMfmiAMvAytOQShZFmoKORcrlXVWqDkDiVkiflRkG2pMWAusBjuTVCSc4Lovij1L7BE7scZRK87IGyckTeCR58lxI4FVyjocTbFMUJSvsAVe7DAgeU2h2IYoQnDse0E4CaOx0dPHbFfrdNEmLYmENEeb41GoPKqRDre4vL4/eS+aWN1ltpOXxnnO4UT3ZzyudyzF6UWit08da7dy9wKxXe/hUNxFHY2rPDseIC7MsT7brA/AdkCTOAoUzXy3eR85/DqWzsYjxbSQzuhtSVopnu1iIPun6PLPf17x/tbnrvxoM8q9fJEccMMV7b55YJyLPcwaIX+TM8kJ/hS/WhkbvWrWurSbPfHmfhuCualaN3L8F1BLAQI/AxQAAgAIAOiJM1JK4iCoeQIAAMcHAAAoAAAAAAAAAAAAAAC2gQAAAAA4N2MyN2QyNS05MDYzLTQ2ODMtOWQ4ZS04YzEwZTc2NmIwNTAueG1sUEsFBgAAAAABAAEAVgAAAL8CAAAAAA=="

iex(3)> {:ok, zipped} = raw |> Base.decode64()                                                                                                                                                                                                {:ok,                                                                                                                                                                                                                                          <<80, 75, 3, 4, 20, 0, 2, 0, 8, 0, 232, 137, 51, 82, 74, 226, 32, 168, 121, 2,                                                                                                                                                                  0, 0, 199, 7, 0, 0, 40, 0, 0, 0, 56, 55, 99, 50, 55, 100, 50, 53, 45, 57, 48,                                                                                                                                                                 54, 51, 45, 52, 54, 56, 51, ...>>}       

iex(3)> zipped |> :zlib.unzip
** (ErlangError) Erlang error: :data_error
    :zlib.inflate_nif(#Reference<0.3987041084.225837063.197866>, 8192, 16384, 0)
    :zlib.dequeue_all_chunks_1/3
    :zlib.inflate/3
    :zlib.unzip/1

However, if I write the zipped data to file, and unzip it in a folder manager (like thunar on ubuntu), I can see the xml data just fine.

iex(4)> File.write("jw315.zip", zipped)                                                                                                                                                                                                       :ok                                   

xml

I am new to elixir, what am I doing wrong?

Have you tried saving the base64-decoded data to a file and feed it to the gzip program?

1 Like

I have tried zipped |> :zlib.gunzip
But got the same :data_error

I didn’t mean that. I meant to save the base64-decoded data to a file, on your disk?

Are you sure you gunzipped your xml as opposed to zipped? Or in extensions: Are you sure you’re dealing with a .gz file and not a .zip file? For the latter you need :zip.unzip.

1 Like

I will tomorrow.
Thx for helping :+1:

I am getting the data from a post request, not from file. The data is zipped and base64 encoded in the post body.
(I am sure it is zipped and not gzipped because I do that myself.) I can unzip it just fine if I save the decoded string to a zip file in a folder manager. But unzipping it in Elixir just doesn’t work.

I did that already, as posted in my question.
After decoding, the string I have left is the zipped content.
Since I could not unzip it with elixir, I write the base64 decoded string --which should be the zipped content-- to a file ( called jw315.zip).
If I open that with a folder manager that can unzip it (like thunar e.g.) I see the the correct unzipped content.
So I so not know why I can unzip it with any unzip program, but not with :zlib.unzip.

In my question it says gunzip, but I tried unzip first, gunzip later.

Sorry I saw that I posted gunzip, but I am using unzip. I tried with gunzip also, pasted the wrong output in my question, edited.

Most unzip programs decide which algorithm to used based on the file’s contents, so they will correctly expand the file in your example with the PKZip algorithm because it starts with the bytes [?P, ?K, 3, 4].

The functions in :zlib are not what you want - you want :zip

4 Likes

Thx! Will try this asap

That was it! Thx for your help!

Thx for your help. Solution was that I had to use :zip and not :zlib

1 Like

Turned out I had to use :zip.unzip instead of :zlib.unzip.
Thanks for your help, much appreciated!

2 Likes