简体   繁体   English

在JavaScript中解压缩Gzip缓冲区

[英]Uncompressing Gzip buffer in JavaScript

I've got a data buffer compressed by the php function gzcompress and I need to uncompress it in js (nodejs). 我有一个由php函数gzcompress压缩的数据缓冲区,我需要在js(nodejs)中解压缩它。

gzcompress(serialize($slot[$i]['advanced_details']),8)

I've tried Class: zlib.Gunzip from https://nodejs.org/api/zlib.html#zlib_class_zlib_gunzip 我已经尝试过https://nodejs.org/api/zlib.html#zlib_class_zlib_gunzip中的 Class:zlib.Gunzip

But it throws: 但是它抛出:

{ [Error: incorrect header check] errno: -3, code: 'Z_DATA_ERROR' }

Full buffer here 此处的完整缓冲区

My code: 我的代码:

nodeZlib.gunzip(rows[0]['Slot'+(i+1)+'AdvancedDetails'], 8, function(error, data) {
                if(!error) {
                    console.log = data.toString();
                } else {
                    console.log('Error unzipping:');
                    console.log(error);
                }
            });

What am I doing wrong? 我究竟做错了什么?

You are confusing formats. 您在混淆格式。 Your confusion is aided by the horrible naming of the functions in PHP. PHP函数的可怕命名助您一臂之力。 PHP's gzcompress() produces the zlib format, whereas node.js's gunzip is expecting the gzip format. PHP的gzcompress()产生zlib格式,而node.js的gunzip期望使用gzip格式。 You could use gzencode() in PHP instead to generate the gzip format, or you could use node.js's zlib.inflate to decompress the zlib format. 您可以改用PHP中的gzencode()生成gzip格式,也可以使用node.js的zlib.inflate解压缩zlib格式。

I was trying to minify a json output from a shell command. 我试图缩小shell命令的json输出。 It was too long without encoding it. 没有编码过长。

The solution I arrived to: 我到达的解决方案:

$content = ['sample', 'data'];

$data = base64_encode(gzencode(
    json_encode($content, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE),
    8,
    FORCE_DEFLATE
));

// then send $data to node

and retrieve the data in Node: 并在Node中检索数据:

const zlib = require('zlib');

let data = zlib.inflateSync(new Buffer(data.min, 'base64')).toString();

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

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