当前位置: 首页 > 知识库问答 >
问题:

在Node.js中用zlib实现带字典的数据压缩

丰智
2023-03-14
var d = zlib.deflateSync(s);

我在Class Options下的文档中注意到我可以设置dictionary,但我不知道如何使用它。

如何用字典缩放字符串?

共有1个答案

贲永思
2023-03-14

对于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');
    }
  }

请参考这个例子:当处理给定的数据集时,如何为zlib的“集合字典”找到一个好的/最优的字典?

 var zlib = require('zlib');
 var input = '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.';
 var dictionary = Buffer.from('rdsusedusefulwhencanismostofstringscompresseddatatowithdictionarybethe', 'utf8');
 var result = zlib.deflateSync(input, {dictionary: dictionary});
 console.log(result);
 类似资料:
  • 问题内容: 有人可以向我解释zlib库在Nodejs中如何工作吗? 我对Node.js很陌生,还不确定如何使用缓冲区和流。 我的简单情况是一个字符串变量,我想将字符串压缩或解压缩(压缩或膨胀,gzip或gunzip等)到另一个字符串。 即(我希望它如何工作) 感谢您的帮助:) 问题答案: 更新 :没意识到在节点0.5中有一个新的内置“ zlib”模块。我在下面的答案是针对第三方node- zlib

  • 处理内存中的数据 # zlib_memory.py import zlib import binascii original_data = b'This is the original text.' print('Original :', len(original_data), original_data) compressed = zlib.compress(original_dat

  • Stability: 2 - Stable zlib 模块提供通过 Gzip 和 Deflate/Inflate 实现的压缩功能,可以通过这样使用它 const zlib = require('zlib'); 压缩或者解压数据流(例如一个文件)通过zlib流将源数据流传输到目标流中来完成。 const gzip = zlib.createGzip(); const fs = require('f

  • 问题内容: 我试图用gzip发送文本,但我不知道如何发送。在示例中,代码使用fs,但是我不想发送文本文件,而只是发送字符串。 问题答案: 你在那儿。我可以衷心地同意,该文档还不足以使您了解如何执行此操作。 一个简化就是不使用;。 …似乎默认情况下会发送UTF-8。但是,当没有默认行为比其他行为更有意义并且我无法立即通过文档进行确认时,我个人宁愿走安全的道路。 同样,如果您需要传递JSON对象,则可

  • 我的javascript代码是 它运行时显示消息错误“不支持的压缩方法”。但是我尝试用在线工具http://i-tools.org/gzip解压,它返回正确的字符串。

  • 本文向大家介绍SpringBoot+Redis实现数据字典的方法,包括了SpringBoot+Redis实现数据字典的方法的使用技巧和注意事项,需要的朋友参考一下 前言 我们在日常的开发过程中针对一些字段采用整型的方式去代替某些具体的含义,比如性别0代表男,1代表女。如果只是一些不会变更的转译我们可以采用常量或者枚举类的方式来实现,但是事实上我们也会遇到那种可能需要变更的,显然这种场景下使用枚举类