我有一个问题,我不能理解。
我正在尝试在JavaScript(nodejs)中使用zlib对base64字符串进行编译
const {inflateSync} = require('zlib')
const base64Data = "7ZW9DoIwEIDf5WYwvesfdnfWRAbFOBDDQIxgoE6Edxd9AXuTDLdc0uQbrl/zpRMc+rGNbd9BuExQto9mjPXjCQFIkc7VNkdXIgVrgvYbhVhYchVksOvi0DYjhAnwM46xjq/lCPuuHOrbfUFOEFQG5++sljlnQOmoSUdRMVjGtsjZwTHYgmGMcTdi6CXNYBkeyKezmvEWhuHBMPa1Np11DGee4aFI9jDP2e9KtcZCGYlUIpVIVxup1ei9lq9UKpVK11upt+iNIqlUKpVK/1PpdX4D"
try{
const decoded_data = inflateSync(Buffer.from(base64Data, 'base64'));
console.log(decoded_data.toString());
}catch (error) {
console.error(error)
}
但它给了我一个错误
Error: incorrect header check
at Zlib.zlibOnError [as onerror] (node:zlib:189:17)
at processChunkSync (node:zlib:457:12)
at zlibBufferSync (node:zlib:178:12)
at syncBufferWrapper (node:zlib:792:14)
at Object.<anonymous> (/Users/docs/index.js:15:26)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12) {
errno: -3,
code: 'Z_DATA_ERROR'
}
Process finished with exit code 0
还需要补充吗?
我做错了什么吗?
我的理解是,窗口大小位是错误的,但JavaScript不允许负窗口,因为Python允许
感谢所有
在Python中,我有下面的脚本来从base64中删除一些数据,它工作得很好
import base64
import zlib
data="7ZW9DoIwEIDf5WYwvesfdnfWRAbFOBDDQIxgoE6Edxd9AXuTDLdc0uQbrl/zpRMc+rGNbd9BuExQto9mjPXjCQFIkc7VNkdXIgVrgvYbhVhYchVksOvi0DYjhAnwM46xjq/lCPuuHOrbfUFOEFQG5++sljlnQOmoSUdRMVjGtsjZwTHYgmGMcTdi6CXNYBkeyKezmvEWhuHBMPa1Np11DGee4aFI9jDP2e9KtcZCGYlUIpVIVxup1ei9lq9UKpVK11upt+iNIqlUKpVK/1PpdX4D"
decoded_data = zlib.decompress(base64.b64decode(data), -zlib.MAX_WBITS)
print(decoded_data)
结果是这样
{
"Position":[
{
"Timestamp":"2023-09-16T12:54:37.0118526Z",
"Entries":{
"1":{"Status":"OnTrack","X":0,"Y":0,"Z":0},
"2":{"Status":"OnTrack","X":0,"Y":0,"Z":0}
}
}
....
]
}
1条答案
按热度按时间e4eetjau1#
使用
inflateRawSync
代替inflateSync
。然后它会工作。(By方式:我认为
unzipSync
也应该工作,给出下面的描述,但由于某种原因,unzipSync也抛出Error: incorrect header check
。参考文献:
https://nodejs.org/api/zlib.html:
InflateRaw
解压缩数据块。Unzip
解压缩数据块。关于https://docs.python.org/3/library/zlib.html:
wbits参数控制历史缓冲区的大小(或“窗口大小”),以及预期的头部和尾部格式。它类似于compressobj()的参数,但接受更多的值范围: