节点js - 在内存中下载并解压缩tgz文件

时间:2016-11-16 14:33:07

标签: javascript node.js

如何从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();
            }
        });
    })
}

0 个答案:

没有答案