2011-10-02 197 views
18

有人可以請我解釋一下zlib庫如何在Nodejs中工作?在Nodejs中使用zlib壓縮和解壓數據

我對Nodejs相當陌生,我不確定如何使用緩衝區和流。

我的簡單場景是一個字符串變量,我想要壓縮或解壓縮(壓縮或膨脹,gzip或gunzip等')字符串到另一個字符串。

I.e. (我怎麼會想到它的工作)

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

感謝您的幫助:)

+0

鏈接到的zlib的NodeJS文件:http://nodejs.org/docs/v0.5.8/api /zlib.html – Eli

回答

19

更新:沒有意識到有一個新的內置「的zlib」在節點0.5模塊。我的回答如下第三方node-zlib module。將立即更新內置版本的答案。

更新2:看起來像是內置的'zlib'可能存在問題。文檔中的示例代碼不適用於我。生成的文件不是gunzip'able(對於我來說,因爲「文件意外結束」失敗)。另外,該模塊的API並不是特別適合你想要做的事情。更多的是使用流而不是緩衝區,而node-zlib模塊有一個更簡單的API,它更容易用於緩衝區。


的放氣和充氣,使用第三方節點的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' 
+0

任何獲得流和管道介紹的機會(他們是如何創建/使用的)? :)很好的答案,謝謝! – Eli

+0

對不起,真的沒有時間(而且不熟悉管道API)。無論如何可能更好地留下一個單獨的SO問題? – broofa

+0

認識到這是一箇舊帖子;)John Resig爲節點流放置了一個很棒的站點。 http://ejohn.org/blog/node-js-stream-playground/希望這可以幫助處於類似情況的人。 – arcseldon

0

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

}); 

這給:

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 
22

對於任何人在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); 
+0

爲什麼需要轉換爲'base64'? –

+4

說實話,我不知道,這是我在一天的實驗後得出的解決方案。如果沒有'base64'解壓縮會報告丟失或不正確的標題。我猜一些控制字符不會正確地序列化爲本地字符串。請注意,如果你壓縮到網絡或文件流 - 你不應該需要'base64'。 – Maksym

+0

如果輸入是緩衝區,爲什麼要膨脹一個異步函數?例如。爲什麼我們甚至需要一個inflateSync變體? – Kevin