2016-03-10 37 views

回答

1

对于Nodejs中的用法,您需要传递类Buffer的实例作为要与zlib进行比较的数据字典。

https://github.com/nodejs/node/blob/master/lib/zlib.js#L347

if (opts.dictionary) { 
    if (!(opts.dictionary instanceof Buffer)) { 
     throw new Error('Invalid dictionary: it should be a Buffer instance'); 
    } 
    } 

请参考下面这个例子: How to find a good/optimal dictionary for zlib 'setDictionary' when processing a given set of data?

根据这一点,你可以做到以下几点:

var zlib = require('zlib'); 
var result = zlib.deflateSync('The dictionary should consist of strings (byte sequences) that are likely to be encountered later in the data to be  compressed, with the most commonly used strings preferably put towards the end of the dictionary. Using a dictionary is most useful when the data to be compressed is short and can be predicted with good accuracy; the data can then be compressed better than with the default empty dictionary.',  {'dictionary': new Buffer('rdsusedusefulwhencanismostofstringscompresseddatatowithdictionarybethe', 'utf8') }); 
console.log(result); 
+0

你能为我提供了一个代码示例如何缩小用字典? – Luka

+0

我更新了答案 – Rabea

+0

链接解释了如何生成一个字典和为什么,所以我没有看到我在这里复制它的原因,也有一个实际生成它的例子,这就是为什么我使用相同的缓冲区他们的例子 – Rabea