我正在使用 Node.JS 并且我正在尝试转换这个看起来像这样的字符串
{"blocks":[{"id":"block","block":"event","args":{"items":[]},"action":"Join"},{"id":"block","block":"player_action","args":{"items":[{"item":{"id":"txt","data":{"name":"hi"}},"slot":0},{"item":{"id":"bl_tag","data":{"option":"Add spaces","tag":"Text Value Merging","action":"SendMessage","block":"player_action"}},"slot":25},{"item":{"id":"bl_tag","data":{"option":"Regular","tag":"Alignment Mode","action":"SendMessage","block":"player_action"}},"slot":26}]},"action":"SendMessage"}]}
使用 zlib 导入 Gzip 我已经尝试过像这样在 Stack Overflow 上关注一些答案
var deflated = zlib.deflateSync('{"blocks":[{"id":"block","block":"event","args":{"items":[]},"action":"Join"},{"id":"block","block":"player_action","args":{"items":[{"item":{"id":"txt","data":{"name":"hi"}},"slot":0},{"item":{"id":"bl_tag","data":{"option":"Add spaces","tag":"Text Value Merging","action":"SendMessage","block":"player_action"}},"slot":25},{"item":{"id":"bl_tag","data":{"option":"Regular","tag":"Alignment Mode","action":"SendMessage","block":"player_action"}},"slot":26}]},"action":"SendMessage"}]}').toString('base64');
var inflated = zlib.inflateSync(new Buffer(deflated, 'base64')).toString();
console.log(inflated);
但后来我得到了错误:
{"blocks":[{"id":"block","block":"event","args":{"items":[]},"action":"Join"},{"id":"block","block":"player_action","args":{"items":[{"item":{"id":"txt","data":{"name":"hi"}},"slot":0},{"item":{"id":"bl_tag","data":{"option":"Add spaces","tag":"Text Value Merging","action":"SendMessage","block":"player_action"}},"slot":25},{"item":{"id":"bl_tag","data":{"option":"Regular","tag":"Alignment Mode","action":"SendMessage","block":"player_action"}},"slot":26}]},"action":"SendMessage"}]}
(node:25548) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
这就是我想要实现的(这是在 base64 之后)
H4sIAAAAAAAA/6VRsQrCMBD9lXKzgwg6ZHMVuqi4iJRrc8RgmpQmFaX03720RSviIE7JvXvv3rukhdy44uJBHFvQEsRQw2w8BdCVbOAaa8UsJgUqI/3UMVYE7SyTNk5b6GZfRlQG71RnI/tz1HDrkSgPt+gnMWCELJbE4FlDx47euABi3ltNNbnJAqqJzFVjtLWUia+wIM/dyBGwp1tIDmgaSlKqlbZR+NxlR1am5D0q+rrDK8pi+UOWLanGYP0MsjZa2ZLfN0mdpD9CrLq375iqufMAPTQAnOUBAAA=
(来自网站)
答案 0 :(得分:0)
除了警告不要使用 Buffer
ctor which is documented as deprecated 并且正如 derpischer 评论的那样,您可以使用 Buffer.from
进行修复:
TLDR: gzip != deflate -- 但 abs(gzip-deflate) zlib 库和访问它的 nodejs 模块支持多种相似且相关但不同且不相同的格式,所有格式均使用最初为 (PK)ZIP 开发的 deflate/inflate 压缩算法,以及较新的 Brotli 算法(此处不相关)。 zlib 支持:(1) 'raw' 充气/放气; (2) 'zlib' 格式,它添加了一个简单的头文件和一个完整性检查; (3) 'gzip' 格式,它添加了不同的标头和不同的完整性检查。请参阅 How are zlib, gzip and zip related? What do they have in common and how are they different? 以了解 zlib 的作者之一马克·阿德勒 (Mark Adler) 对这种情况的解释。 nodejs 通过对 zlib 格式使用名称 {In,De}Flate
和 {in,de}flate[Sync]
来稍微混淆这一点; {In,De}FlateRaw
和 {in,de}flateRaw[Sync]
用于原始格式;和 G[un]zip
和 g[un]zip[Sync]
用于 gzip 格式。您正在使用 zlib 格式的方法,因此您获得的是 zlib 格式而不是 gzip 格式;要获取 gzip 格式,请使用 the methods for gzip format。