1

I've got a data buffer compressed by the php function gzcompress and I need to uncompress it in 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

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?

Jesús Fuentes
  • 919
  • 2
  • 11
  • 33

2 Answers2

2

You are confusing formats. Your confusion is aided by the horrible naming of the functions in PHP. PHP's gzcompress() produces the zlib format, whereas node.js's gunzip is expecting the gzip format. 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.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
1

I was trying to minify a json output from a shell command. 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:

const zlib = require('zlib');

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