Compression and decompression of data using zlib in Nodejs

前端 未结 3 1596
你的背包
你的背包 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条回答
  •  没有蜡笔的小新
    2020-12-25 12:14

    broofa's answer is great, and that's exactly how I'd like things to work. For me node insisted on callbacks. This ended up looking like:

    var zlib = require('zlib');
    var input = new Buffer('lorem ipsum dolor sit amet', 'utf8')
    
    
    zlib.deflate(input, function(err, buf) {
        console.log("in the deflate callback:", buf);
    
        zlib.inflate(buf, function(err, buf) {
                console.log("in the inflate callback:", buf);
                console.log("to string:", buf.toString("utf8") );
        });
    
    });
    

    which gives:

    in the deflate callback: 
    in the inflate callback: 
    to string: lorem ipsum dolor sit amet
    

提交回复
热议问题