Compression and decompression of data using zlib in Nodejs

前端 未结 3 1589
你的背包
你的背包 2020-12-25 11:33

Can someone please explain to me how the zlib library works in Nodejs?

I\'m fairly new to Nodejs, and I\'m not yet sure how to use buffers and streams.

My si

3条回答
  •  猫巷女王i
    2020-12-25 12:10

    For anybody stumbling on this in 2016 (and also wondering how to serialize compressed data to a string rather than a file or a buffer) - it looks like zlib (since node 0.11) now provides synchronous versions of its functions that do not require callbacks:

    var zlib = require('zlib');
    var input = "Hellow world";
    
    var deflated = zlib.deflateSync(input).toString('base64');
    var inflated = zlib.inflateSync(new Buffer(deflated, 'base64')).toString();
    
    console.log(inflated);
    

提交回复
热议问题