Home Ask Login Register

Developers Planet

Your answer is one click away!

David Wolever February 2016

iOS compression_encode_buffer doesn't include zlib header?

I'm using compression_encode_buffer with COMPRESSION_ZLIB to zlib-compress data in an iOS app, but the result is missing the zlib header.

For example:

size = compression_encode_buffer(
    destinationBuffer, destinationBufferSize,
    sourceBuffer, sourceBufferSize, nil,

Will return the bytes:

AD 53 C1 8E 9B 30 …

While using, for example, Python's data.encode("zlib") on the same data will return:

78 9C AD 53 C1 8E 9B 30 …
^^ ^^ correct zlib header

What's up with that? Why isn't the header being included? And is there a "correct" way to add it?


Glenn Randers-Pehrson February 2016

The two bytes are a zlib header, not a magic number. Most likely you are missing the final 4 bytes of the stream (the ADLER32 checksum) as well, and only have the "deflate" datastream that you expected to be wrapped in a zlib datatstream. The iOS documentation says as much:

The encoded format is the raw DEFLATE format as described
in IETF RFC 1951 Using the ZLIB library

They should have called the compression method "DEFLATE" not "ZLIB".

See this related question about dealing with ZLIB vs DEFLATE data.

Post Status

Asked in February 2016
Viewed 2,936 times
Voted 8
Answered 1 times


Leave an answer

Quote of the day: live life