2017-08-11 17 views
0

我想在Node中解壓縮gzip文件,但我遇到以下錯誤。節點Zlib不正確的標題檢查

Error: incorrect header check at Zlib._handle.onerror (zlib.js:370:17)

這裏是代碼的原因的問題。

'use strict' 

const fs = require('fs'); 
const request = require('request'); 
const zlib = require('zlib'); 
const path = require('path'); 

var req = request('https://wiki.mozilla.org/images/f/ff/Example.json.gz').pipe(fs.createWriteStream('example.json.gz')); 

req.on('finish', function() { 
    var readstream = fs.createReadStream(path.join(__dirname, 'example.json.gz')); 
    var writestream = fs.createWriteStream('example.json'); 

    var inflate = zlib.createInflate(); 
    readstream.pipe(inflate).pipe(writestream); 
}); 
//Note using file system because files will eventually be much larger 

我是否缺少明顯的東西?如果不是,我怎麼才能確定是什麼在拋出錯誤?

回答

2

該文件是gzipped,因此您需要使用zlib.Gunzip而不是zlib.Inflate

另外,流在內存使用方面非常有效,所以如果你想不存儲。廣州本地文件先進行檢索時,可以使用這樣的事情:

request('https://wiki.mozilla.org/images/f/ff/Example.json.gz') 
    .pipe(zlib.createGunzip()) 
    .pipe(fs.createWriteStream('example.json')); 

否則,可以修改您現有的代碼:

var gunzip = zlib.createGunzip(); 
readstream.pipe(gunzip).pipe(writestream);