Nodejs Zlib Unzip Binary Data From Socket
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
Answer
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.
Related Questions
- → Maximum call stack exceeded when instantiating class inside of a module
- → Browserify api: how to pass advanced option to script
- → Node.js Passing object from server.js to external modules?
- → gulp-rename makes copies, but does not replace
- → requiring RX.js in node.js
- → Remove an ObjectId from an array of objectId
- → Can not connect to Redis
- → React: How to publish page on server using React-starter-kit
- → Express - better pattern for passing data between middleware functions
- → Can't get plotly + node.js to stream data coming through POST requests
- → IsGenerator implementation
- → Async/Await not waiting
- → (Socket.io on nodejs) Updating div with mysql data stops without showing error