Zlib:Node.js 无法从 python 中提取压缩数据
Zlib: Node.js can't extract compressed data from python
我使用 python 压缩字符串数据并将数据存储在 sqlite3 中。对于我的项目,我还需要使用 node.js 来提取数据。问题是,当我尝试这样做时。我从 node.js:
得到一个错误
{ 错误:header 检查不正确
在 Gunzip.zlibOnError (zlib.js:153:15) 错误号:-3,代码:'Z_DATA_ERROR' }
我尝试使用 Base64 和 utf8 对 Python 中的字符串进行编码。两者都没有区别。
在 JavaScript 中,我试图跳过一些编码数据位,这样我就可以克服头部检查问题。那也是徒劳。
这里是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()
现在这是 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())
}
});
}
});
});
});
Python 中的 zlib.compress
(没有 wbits
参数)将生成 zlib 格式,而 Node.js 中的 zlib.gunzip
需要 gzip 格式。使用 zlib.compress
且 wbits
等于 31 来获取 gzip 格式,或者使用 zlib.inflate
in Node.js 来解压缩 zlib 格式。
我使用 python 压缩字符串数据并将数据存储在 sqlite3 中。对于我的项目,我还需要使用 node.js 来提取数据。问题是,当我尝试这样做时。我从 node.js:
得到一个错误{ 错误:header 检查不正确 在 Gunzip.zlibOnError (zlib.js:153:15) 错误号:-3,代码:'Z_DATA_ERROR' }
我尝试使用 Base64 和 utf8 对 Python 中的字符串进行编码。两者都没有区别。
在 JavaScript 中,我试图跳过一些编码数据位,这样我就可以克服头部检查问题。那也是徒劳。
这里是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()
现在这是 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
(没有 wbits
参数)将生成 zlib 格式,而 Node.js 中的 zlib.gunzip
需要 gzip 格式。使用 zlib.compress
且 wbits
等于 31 来获取 gzip 格式,或者使用 zlib.inflate
in Node.js 来解压缩 zlib 格式。