Nodejs Zlib Unzip Binary Data From Socket

- 1 answer

I receive some binary data via socket in nodejs, zipped using zlib. I do not have access to the source that origins the messages.

when I try to unzip the data I got "sometimes" errors from zlib.

below is an example, I coded two message in HEX for convenience. I am sure they are both zlib compressed (the header "789c" give me that confidence), but I cannot understand why the test1 message works and the test2 message don't.

Maybe a dictionary is needed? Maybe a version missmatch between compression and decompression algorithm?

I feel I can exclude an issue on reading data, since both messages are read the same way.

The help of a zlib expert will be very appreciated.

let test1 = "789c4d8e410e82301444f79ea2f9fb021f51206971e5c2ad7a01da7e840894d06a20c6bb8bbad0e54c66f29ed84d5dcbee34bac6f61230886057ac4439d1e0f777eabd2b84b2662e84b6bda7c933dd364b2dc139e5815d6e8d91f040aa22131bcdb7581a9e6c50f11c938c27695646b45e672a534f60aeb6c361d9a7e936e2790c3f701c2006086121e84365ba267d75b74e02b0ba74a7d9795a0202b35545e3791e48c2f17d08bf7ee1bff3ea051dd44446";
let test2 = "789c4d8e410e82301444f79ca2f9fb0205059ab4b072e156bd006d3f42544a683110e3dd455de8722633794f54f3ed4aee38bacef6125818435506a29e71f0bb3bf6de954259b39442dbdee3ec89be766b2dc139e5819ca7ce4878285d37292b38cd52cde826ce38e55b8334df2aad8d490aaeea2710d7da61bfeef33c8b294fe0074e42c642065129f04325ba457d71d34d0290b676c7c5795c5c0303629b06c7d332a084c3fb107dfda27fe7e0054ca744b6";

console.log(zlib.unzipSync(Buffer.from(test1, "hex")).toString()); // correct output
console.log(zlib.unzipSync(Buffer.from(test2, "hex")).toString()); // ERROR: data check


The second test message is, in fact, invalid. Either it was corrupted in transit (including within your code), or it was corrupted when it was made.

One thing to check for is, if you are on a Windows operating system, whether you are using binary mode to read the file. If not, then it would in fact be possible for some inputs to be corrupted and not others.