Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

313
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!