2
votes

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?

1

1 Answers

2
votes

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 [email protected]