首页 > 解决方案 > 错误:curl 文件时使用 Gunzip 进行的标头检查不正确

问题描述

在 shell 中使用命令curl https://nodejs.org/dist/v0.1.100/node-v0.1.100.tar.gz | node stream-pipe.js检索文件node-v0.1.100.tar.gz并解压缩-修改-压缩它,当我尝试解压缩时zlib.createGunzip()出现错误:

Error: incorrect header check
    at Zlib.zlibOnError [as onerror] (node:zlib:190:17)
Emitted 'error' event on Gunzip instance at:
    at Gunzip.onerror (node:internal/streams/readable:769:14)
    at Gunzip.emit (node:events:369:20)
    at emitErrorNT (node:internal/streams/destroy:188:8)
    at emitErrorCloseNT (node:internal/streams/destroy:153:3)
    at processTicksAndRejections (node:internal/process/task_queues:81:21) {
  errno: -3,
  code: 'Z_DATA_ERROR'
}

代码:

const zlib=require('zlib')
const map=require('tar-map-stream')

const decompress=zlib.createGunzip()
const whoami=process.env.USER || process.env.USERNAME
const convert=map((header)=>{
    header.uname=whoami
    header.mtime=new Date()
    header.name=header.name.replace('node-v0.1.100','v0.1.100')
    return header
})
const compress=zlib.createGzip()

process.stdin.pipe(decompress).pipe(convert).pipe(compress).pipe(process.stdout)

如果我尝试在我的本地文件夹中获取文件,它运行良好:

const zlib=require('zlib')
const map=require('tar-map-stream')
const fs=require('fs')

const decompress=zlib.createGunzip()
const whoami='amico_delle_tette'
const convert=map((header)=>{
    header.uname=whoami
    header.mtime=new Date()
    header.name=header.name.replace('node-v0.1.100','v0.1.100')
    return header
})
const compress=zlib.createGzip()
const stream=fs.createReadStream('node-v0.1.100.tar.gz')
const out=fs.createWriteStream('q.tar.gz')
stream.pipe(decompress).pipe(convert).pipe(compress).pipe(out)

什么原因?

标签: node.js

解决方案


推荐阅读