简体   繁体   中英

Zlib: Node.js can't extract compressed data from python

I use python to compress string data and store the data in sqlite3. For my project I need also to use node.js to extract the data. The problem is, when i try to do that. I get a error from node.js:

{ Error: incorrect header check at Gunzip.zlibOnError (zlib.js:153:15) errno: -3, code: 'Z_DATA_ERROR' }

I tried to encode the string in Python with Base64 and utf8. Both doesn't make a difference.

In JavaScript i tried to skip some bits of the encode data, so that I may overcome the head check problem. That's also in vain.

Here is the compress code in python

import zlib, base64
import time
text = 'STACK OVERFLOW STACK OVERFLOW STACK OVERFLOW STACK OVERFLOW '
textToBytes = text.encode('utf-8')
code =  zlib.compress(textToBytes)

code = base64.b64encode(code)
print('code in base64:', code)

timestamp = time.time()
#Store in database
conn = sqlite3.connect('testsDummy.db')
c = conn.cursor()

c.execute("CREATE TABLE IF NOT EXISTS tests (id INTEGER PRIMARY KEY, timestamp REAL, code TEXT)")
conn.commit()
c.execute("INSERT INTO  tests (code ,timestamp) VALUES (?,?)",(code ,timestamp))
conn.commit()
conn.close()

Now this is code in node.js

function toArrayBuffer(buffer) {
  var arrayBuffer = new ArrayBuffer(buffer.length);
  var view = new Uint8Array(arrayBuffer);
  for (var i = 0; i < buffer.length; ++i) {
    view[i] = buffer[i];
  }
  return arrayBuffer;
}

app.get('/download/logfile/:timestamp', (req, res) => {
  var zlib = require('zlib');

  var db = new sqlite3.Database(path.join(__dirname, 'dependencies', 'testsDummy.db'));
  db.serialize(() => {
    db.all('SELECT code FROM tests WHERE timestamp=' + req.params.timestamp, (error, tests) => {
      db.close();

      if (tests[0].code== '' || tests[0].code== null) {
        res.send('No qdxm data collected.');
      }
      else {
        console.log(tests[0].code)
        var arrayBuffer = toArrayBuffer(Buffer.from(tests[0].code, 'base64'));
        zlib.gunzip(Buffer.from(arrayBuffer, 4), function (err, uncompressedMessage) {
          if (err) {
            console.log(err)
            res.send();
          }
          else {
            res.json({ uncompressedMessage: uncompressedMessage.toString() })
            console.log(uncompressedMessage.toString())
          }
        });
      }
    });
  });
});

zlib.compress in Python (without a wbits parameter) will generate the zlib format, whereas zlib.gunzip in Node.js is expecting the gzip format. Either use zlib.compress with wbits equal to 31 to get the gzip format, or use zlib.inflate in Node.js to decompress the zlib format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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