简体   繁体   English

PHP的gzuncompress node.js中的function

[英]PHP's gzuncompress function in node.js

So I've got some data that's been compressed with PHP's gzcompress method: http://us2.php.net/manual/en/function.gzcompress.php所以我得到了一些用 PHP 的 gzcompress 方法压缩的数据: http://us2.php.net/manual/en/function.gzcompress.php

How can I decode this data from node.js??如何从 node.js 解码这些数据?

I've tried "compress", "zlib" and several other node compression libraries, but none of them seem to reckognize the data. 我试过“compress”、“zlib”和其他几个节点压缩库,但似乎没有一个能识别数据。 For example, zlib just gives me "Error: incorrect header check" 例如,zlib 只是给我“错误:不正确的 header 检查”

Answer: Turns out that "zlib" is the way to go.答:原来“zlib”是通往 go 的方式。 We had an additional issue with binary data from memcache.我们遇到了来自 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.如果您在 node.js 缓冲区 object 中有二进制数据,并且您调用 toString() 而不是.toString('binary'),它会得到各种加扰,因为内容被转义或转义序列被解释或其他。 Unfortunately, all the memcache plugins I've tried to date assume string data from memcache, and are not disciplined about handling it properly.不幸的是,迄今为止我尝试过的所有 memcache 插件都假定来自 memcache 的字符串数据,并且没有规范地正确处理它。

Best ZLIB module I've found:我发现的最佳 ZLIB 模块:

https://github.com/kkaefer/node-zlib 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'));

FYI, this question is VERY similar to a related question about Java: PHP's gzuncompress function in Java?仅供参考,这个问题与有关 Java: PHP's gzuncompress function in Java 的相关问题非常相似?

Stealing from another post ( Which compression method to use in PHP? )从另一个帖子中窃取( 在 PHP 中使用哪种压缩方法?

  • gzencode() uses the fully self-contained gzip format, same as the gzip command line tool gzencode() 使用完全自包含的 gzip 格式,与 gzip 命令行工具相同
  • gzcompress() uses the raw ZLIB format. gzcompress() 使用原始 ZLIB 格式。 It is similar to gzencode but has different header data, etc. I think it was intended for streaming.它与 gzencode 类似,但具有不同的 header 数据等。我认为它是用于流式传输的。
  • gzdeflate() uses the raw DEFLATE algorithm on its own, which is the basis for both the other formats. gzdeflate() 自己使用原始的 DEFLATE 算法,这是其他两种格式的基础。

Thus, "zlib" would be the correct choice.因此,“zlib”将是正确的选择。 This is NOT cross-compatible with gzip.这与 gzip 不交叉兼容。

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

php: php:

<?php 
$data = 'HelloWorld';
$gzcompress = gzcompress($data);
$gzcompress_base64_encode = base64_encode($gzcompress);
echo "Compressing: {$data}\n";
echo $gzcompress."\n";
echo $gzcompress_base64_encode."\n";
echo "--- inverse ---\n";
echo gzuncompress(base64_decode($gzcompress_base64_encode))."\n";

nodejs:节点:

const zlib = require('zlib');
var data = 'HelloWorld';
var z1 = zlib.deflateSync( Buffer.from(data));

var gzcompress = z1.toString();
var gzcompress_base64_encode = z1.toString('base64');
console.log('Compressing '+data);
console.log(gzcompress);
console.log(gzcompress_base64_encode);
console.log('--- inverse ---');
console.log(zlib.inflateSync(Buffer.from(gzcompress_base64_encode,'base64')).toString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM