Node.js中PHP的gzuncompress函数 [英] PHP's gzuncompress function in node.js

查看:74
本文介绍了Node.js中PHP的gzuncompress函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我得到了一些用PHP的gzcompress方法压缩的数据: http://us2.php.net/manual/en/function.gzcompress.php

So I've got some data that's been compressed with PHP's gzcompress method: http://us2.php.net/manual/en/function.gzcompress.php

如何从node.js解码此数据?

How can I decode this data from node.js??

我尝试了"compress","zlib"和其他几个节点压缩库,但是它们似乎都无法识别数据.例如,zlib只是给我错误:错误的标头检查"

答案:事实证明,"zlib"是必经之路.我们还遇到了另一个问题,即内存缓存中的二进制数据.如果在node.js Buffer对象中有二进制数据,并且调用toString()而不是.toString('binary'),则它会发生各种混乱,因为发生了东西被转义或解释了转义序列等问题.不幸的是,我迄今尝试过的所有memcache插件都假定来自memcache的字符串数据,并且对如何正确处理它没有纪律.

Answer: Turns out that "zlib" is the way to go. We had an additional issue with binary data from memcache. If you have binary data in a node.js Buffer object and you call toString() instead of .toString('binary'), it get's all kinds of scrambled as stuff is escaped or escape sequences are interpreted or whatever. Unfortunately, all the memcache plugins I've tried to date assume string data from memcache, and are not disciplined about handling it properly.

我发现的最佳ZLIB模块:

Best ZLIB module I've found:

https://github.com/kkaefer/node-zlib

// first run "npm install zlib", then...
var zlib = require('zlib');
var gz = zlib.deflate(new Buffer("Hello World", 'binary')); // also another 'Buffer'
console.log(zlib.inflate(gz).toString('binary'));

仅供参考,这个问题与关于Java的相关问题非常相似: PHP在Java中的gzuncompress函数?

FYI, this question is VERY similar to a related question about Java: PHP's gzuncompress function in Java?

推荐答案

从另一篇文章中窃取(

Stealing from another post (Which compression method to use in PHP?)

  • gzencode()使用完全独立的gzip格式,与gzip命令行工具相同
  • gzcompress()使用原始ZLIB格式.它与gzencode相似,但是具有不同的标头数据等.我认为它是用于流式传输的.
  • gzdeflate()单独使用原始的DEFLATE算法,这是其他两种格式的基础.

因此,"zlib"将是正确的选择.这与gzip不交叉兼容.

Thus, "zlib" would be the correct choice. This is NOT cross-compatible with gzip.

尝试 https://github.com/kkaefer/node-zlib

这篇关于Node.js中PHP的gzuncompress函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆