使用GitLab API和Node.js下载GitLab存储库存档
我想从我的GitLab存储库下载(而不是克隆)档案,但是我得到这个错误
不正确的标题检查(Zlib._handle.onerror)
这是我的function:
var fs = require('fs'); var url = require('url'); var https = require('https'); var path = require('path'); var targz = require('tar.gz'); function downloadFile(source, destination, name) { var options = { host: url.parse(source).host, port: 443, path: url.parse(source).pathname }; var file = fs.createWriteStream(destination + path.sep + name); https.get(options, function(res) { res.on('data', function(data) { file.write(data); }).on('end', function() { file.end(); console.log('File ' + name + ' downloaded to ' + destination); targz().extract(destination + '/' + name, destination) .then(function(){ console.log('Job done!'); }) .catch(function(err){ console.log('Something is wrong ', err.stack); }); }); }); }
下载的文件是tar.gz的types。 我尝试设置一些标题,但不成功。 源参数是这样的: https ://gitlab.com/api/v3/projects/:ID/repository/archive?token =XXYYZZ
请帮忙吗?
问题是你的文件没有被https
模块正确下载,导致tar.gz
模块的提取错误。
您可以使用createWriteStream
与tar.gz
协调的request
模块将提取pipe道直接传送到目标文件夹:
var request = require('request'); var targz = require('tar.gz'); function downloadFile(source, destination, cb) { var read = request.get(source); var write = targz().createWriteStream(destination); read.pipe(write); write.on('finish', function() { cb(null); }); write.on('error', function(err) { cb(err); }); } var source = "https://gitlab.com/api/v3/projects/:ID/repository/archive?token=XXYYZZ"; var destination = "/home/user/some/dir"; downloadFile(source, destination, function(err) { if (err) { console.log('Something is wrong ', err.stack); } else { console.log('Job done!'); } });
请注意,对于要发送的finish
事件,您将需要tar.gz
版本1.0.2
(请参阅此问题 ):
npm install tar.gz@1.0.2