Nodejs中使用zlib对数据进行压缩和解压

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7625251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:38:49  来源:igfitidea点击:

Compression and decompression of data using zlib in Nodejs

stringnode.jscompressionzlib

提问by Eli

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

有人可以向我解释 zlib 库是如何在 Nodejs 中工作的吗?

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

我对 Nodejs 还很陌生,我还不确定如何使用缓冲区和流。

My simple scenario is a string variable, and I want to either zip or unzip (deflate or inflate, gzip or gunzip, etc') the string to another string.

我的简单场景是一个字符串变量,我想将字符串压缩或解压缩(压缩或膨胀、gzip 或 gunzip 等)到另一个字符串。

I.e. (how I would expect it to work)

即(我希望它如何工作)

var zlib = require('zlib');
var str = "this is a test string to be zipped";
var zip = zlib.Deflate(str); // zip = [object Object]
var packed = zip.toString([encoding?]); // packed = "packedstringdata"
var unzipped = zlib.Inflate(packed); // unzipped = [object Object]
var newstr = unzipped.toString([again - encoding?]); // newstr = "this is a test string to be zipped";

Thanks for the helps :)

感谢您的帮助:)

采纳答案by broofa

Update: Didn't realize there was a new built-in 'zlib' module in node 0.5. My answer below is for the 3rd party node-zlib module. Will update answer for the built-in version momentarily.

更新:没有意识到节点 0.5 中有一个新的内置“zlib”模块。我下面的回答是针对第 3 方node-zlib 模块的。将立即更新内置版本的答案。

Update 2: Looks like there may be an issue with the built-in 'zlib'. The sample code in the docs doesn't work for me. The resulting file isn't gunzip'able (fails with "unexpected end of file" for me). Also, the API of that module isn't particularly well-suited for what you're trying to do. It's more for working with streams rather than buffers, whereas the node-zlib module has a simpler API that's easier to use for Buffers.

更新 2:看起来内置的“zlib”可能存在问题。文档中的示例代码对我不起作用。生成的文件不可压缩(对我来说“文件意外结束”失败)。此外,该模块的 API 并不是特别适合您要执行的操作。它更多地用于处理流而不是缓冲区,而 node-zlib 模块具有更简单的 API,更易于用于缓冲区。



An example of deflating and inflating, using 3rd party node-zlib module:

使用 3rd 方 node-zlib 模块放气和充气的示例:

$ node

> // Load zlib and create a buffer to compress
> var zlib = require('zlib');
> var input = new Buffer('lorem ipsum dolor sit amet', 'utf8')

> // What's 'input'?
> input
<Buffer 6c 6f 72 65 6d 20 69 70 73 75 6d 20 64 6f 6c 6f 72 20 73 69 74 20 61 6d 65 74>

> // Compress it
> zlib.deflate(input)
<SlowBuffer 78 9c cb c9 2f 4a cd 55 c8 2c 28 2e cd 55 48 c9 cf c9 2f 52 28 ce 2c 51 48 cc 4d 2d 01 00 87 15 09 e5>

> // Compress it and convert to utf8 string, just for the heck of it
> zlib.deflate(input).toString('utf8')
'x???/J?U?,(.?UH???/R(?,QH?M-\u0001\u0000?\u0015\t?'

> // Compress, then uncompress (get back what we started with)
> zlib.inflate(zlib.deflate(input))
<SlowBuffer 6c 6f 72 65 6d 20 69 70 73 75 6d 20 64 6f 6c 6f 72 20 73 69 74 20 61 6d 65 74>

> // Again, and convert back to our initial string
> zlib.inflate(zlib.deflate(input)).toString('utf8')
'lorem ipsum dolor sit amet'

回答by Maksym

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:

对于在 2016 年遇到此问题的任何人(并且还想知道如何将压缩数据序列化为字符串而不是文件或缓冲区)-看起来 zlib(自节点 0.11 起)现在提供了不需要回调的函数的同步版本:

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);

回答by Ben

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

broofa的答案是伟大的,而这正是我怎么会喜欢的东西的工作。对我来说,节点坚持回调。这最终看起来像:

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: <Buffer 78 9c cb c9 2f 4a cd 55 c8 2c 28 2e cd 55 48 c9 cf c9 2f 52 28 ce 2c 51 48 cc 4d 2d 01 00 87 15 09 e5>
in the inflate callback: <Buffer 6c 6f 72 65 6d 20 69 70 73 75 6d 20 64 6f 6c 6f 72 20 73 69 74 20 61 6d 65 74>
to string: lorem ipsum dolor sit amet