【发布时间】:2018-02-14 17:53:25
【问题描述】:
我在 NODEJS 中使用 ZLIB 来压缩字符串。在压缩字符串时,我得到一个缓冲区。我想将该缓冲区作为 PUT 请求发送,但 PUT 请求拒绝 BUFFER,因为它只需要 STRING。我无法将 BUFFER 转换为 STRING,然后在接收端我无法解压缩该字符串,因此我可以获得原始数据。我不确定如何将缓冲区转换为字符串,然后将该字符串转换为缓冲区,然后解压缩缓冲区以获取原始字符串。
let zlib = require('zlib');
// compressing 'str' and getting the result converted to string
let compressedString = zlib.deflateSync(JSON.stringify(str)).toString();
//decompressing the compressedString
let decompressedString = zlib.inflateSync(compressedString);
最后一行导致输入无效。
我尝试将“compressedString”转换为缓冲区,然后将其解压缩,但也无济于事。
//converting string to buffer
let bufferedString = Buffer.from(compressedString, 'utf8');
//decompressing the buffer
//decompressedBufferString = zlib.inflateSync(bufferedString);
此代码还给出了异常,因为输入无效。
【问题讨论】:
-
您尝试过什么,您现在拥有的代码是什么?我们需要Minimal, Complete, Verifiable example 来帮助您解决具体问题。
-
@RickyM 我已经更新了这个问题。你能建议我一些解决方案或解决方法吗?
标签: node.js string compression buffer zlib