如何从https网址下载文件并解压缩.tgz文件?现在我下载文件,然后进行解压缩。但我想在记忆中这样做。我试过了
function downloadFile() {
var outStream = fs.createWriteStream(config.tgz_file_path);
return new Promise(function(resolve, reject){
var req = request(options);
req.on('response', function (res) {
if (res.statusCode !== 200) {
reject(new Error('Status not 200'));
}
var encoding = res.headers['content-encoding'];
if (encoding == 'gzip') {
res.pipe(zlib.createGunzip()).pipe(outStream);
} else if (encoding == 'deflate') {
res.pipe(zlib.createInflate()).pipe(outStream);
} else {
res.pipe(outStream);
}
res.on('end', function () {
resolve();
});
});
req.on('error', function(err) {
throw err;
});
});
}
function decompressFile() {
return new Promise(function(resolve, reject){
targz.decompress({
src: config.tgz_file_path,
dest: './data'
}, function(err){
if(err) {
reject(err);
} else {
resolve();
}
});
})
}