我正在尝试从Github下载tar.gz并将其写入文件系统。我目前的代码:
var https = require('https');
var fs = require('fs');
var zlib = require('zlib');
var file = fs.createWriteStream('zeromq-4.1.5.tar.gz');
var request = https.get('https://github.com/zeromq/zeromq4-1/releases/download/v4.1.5/zeromq-4.1.5.tar.gz', function(response) {
response.pipe(zlib.createGzip()).pipe(file);
});
当我运行tar xzf zeromq-4.1.5.tar.gz
时,我收到以下错误:
tar: Unrecognized archive format
tar: Error exit delayed from previous errors.
答案 0 :(得分:0)
问题是https.get
不遵循重定向。以下为我解决了这个问题:
var https = require('https');
var fs = require('fs');
var url = require('url');
var TAR_URL = 'https://github.com/' + process.env.ZMQ_REPO + '/releases/download/v' + process.env.ZMQ + '/zeromq-' + process.env.ZMQ + '.tar.gz';
var FILE_NAME = 'zeromq-' + process.env.ZMQ + '.tar.gz';
function writeToFile(response) {
response.pipe(fs.createWriteStream(FILE_NAME));
}
https.get(TAR_URL, function(response) {
if (response.statusCode > 300 && response.statusCode < 400 && response.headers.location) {
if (url.parse(response.headers.location).hostname) {
https.get(response.headers.location, writeToFile);
} else {
https.get(url.resolve(url.parse(TAR_URL).hostname, response.headers.location), writeToFile);
}
} else {
writeToFile(response);
}
});