使用 GitLab API 和 Node.js 下载 GitLab 存储库存档
Download GitLab repository archive using GitLab API and Node.js
我想从我的 GitLab 存储库下载(不是克隆)存档,但我收到此错误
incorrect header check (Zlib._handle.onerror)
这是我的功能:
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。我尝试设置一些 headers 但没有成功。源参数类似于:https://gitlab.com/api/v3/projects/:ID/repository/archive?token=XXYYZZ
有什么帮助吗?
问题是 https
模块未正确下载您的文件,导致 tar.gz
模块提取错误。
您可以使用 request
模块与 tar.gz
配合 createWriteStream
将提取直接通过管道传输到目标文件夹:
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
版本(参见 this issue):
npm install tar.gz@1.0.2
我想从我的 GitLab 存储库下载(不是克隆)存档,但我收到此错误
incorrect header check (Zlib._handle.onerror)
这是我的功能:
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。我尝试设置一些 headers 但没有成功。源参数类似于:https://gitlab.com/api/v3/projects/:ID/repository/archive?token=XXYYZZ
有什么帮助吗?
问题是 https
模块未正确下载您的文件,导致 tar.gz
模块提取错误。
您可以使用 request
模块与 tar.gz
配合 createWriteStream
将提取直接通过管道传输到目标文件夹:
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
版本(参见 this issue):
npm install tar.gz@1.0.2