0
如果我要放氣串s
我可以做壓縮在node.js中使用zlib的字典數據的
var d = zlib.deflateSync(s);
我注意到在documentation under Class Options,我可以集字典,但我不知道該怎麼用它。
如何用字典壓縮字符串?
如果我要放氣串s
我可以做壓縮在node.js中使用zlib的字典數據的
var d = zlib.deflateSync(s);
我注意到在documentation under Class Options,我可以集字典,但我不知道該怎麼用它。
如何用字典壓縮字符串?
對於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);
你能爲我提供了一個代碼示例如何縮小用字典? – Luka
我更新了答案 – Rabea
鏈接解釋瞭如何生成一個字典和爲什麼,所以我沒有看到我在這裏複製它的原因,也有一個實際生成它的例子,這就是爲什麼我使用相同的緩衝區他們的例子 – Rabea