Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

315
Visualizações
Download GitLab repository archive using GitLab API and Node.js

I would like to download (not clone) archive from my GitLab repository, but I get this error

incorrect header check (Zlib._handle.onerror)

This is my 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);
                });
        });
    });
}

The file which is download is type of tar.gz. I try to set some headers but unsuccessful. Source param is like: https://gitlab.com/api/v3/projects/:ID/repository/archive?token=XXYYZZ

Any help please?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The issue is that your file is not correctly downloaded by https module which result in extraction error from tar.gz module.

You can use request module coordinated with tar.gz with createWriteStream to pipe the extraction directly to the destination folder :

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!');
    }
});

Note that, for the finish event to be dispatched you will need version 1.0.2 of tar.gz (see this issue) :

npm install tar.gz@1.0.2
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda