我正在使用以下代码在NodeJ中玩zlib
。
const input = 'This is test'
const encoding = 'base64'
zlib.gzip(input, (err, buffer) => {
if (!err) {
const stringified = buffer.toString(encoding)
console.log(stringified)
const bufferToDeCompress = Buffer.from(stringified, encoding)
zlib.unzip(bufferToDeCompress, (err, unzippedBuffer) => {
if (!err) {
console.log(unzippedBuffer.toString())
} else {
// handle error
}
});
} else {
// handle error
}
});
因此,我正在尝试压缩和解压缩字符串。以上代码适用于utf8
以外的所有编码。如果我更改为utf8
。它将引发以下错误。
{ Error: incorrect header check at Zlib.zlibOnError [as onerror]
(zlib.js:153:17) errno: -3, code: 'Z_DATA_ERROR' }
有人碰到过这个吗?