Skip to main content

Mike Kreuzer

Download and unzip using Node

17 September 2015

I unjustly maligned Node for not being able to unzip files properly. ~~Or possibly I was right, and some combination of RVM, Ruby, Homebrew, NVM, Node or iojs and unzip weren't playing nicely together.~~

Update 24 September: turns out there are definite bugs in the Node unzip package. Spaces in files names, uncompressed archives, possibly other things & they fall over. Pity.

Update 29 September: The adm-zip module (so far!) has worked for me. As a bonus there's another module which uses adm-zip that already does what I was using as a test, downloading & unzipping Github repos: github-download

Node 4.0.0 is actually working pretty well, for the small number of hoops I'm asking it to jump through. Whatever was going on downloading and unzipping files is working now. ~~So another Node code fragment, in case it's useful to anyone:~~

~~Update 15 September 2018: I removed the embedded gist that was here, the link hasn't changed.~~

Update November 2023: Here it is again, as I leave Github: :::JavaScript /jslint node: true / 'use strict';

var fs = require('fs'),
    path = require('path'),

    fstream = require('fstream'),
    mkdirp = require('mkdirp'),
    request = require('request'),
    unzip = require('unzip');

function downloadAndUnzip(fromURL, toDir, zipFileName) {
    mkdirp(toDir, function (dirErr) {
        if (dirErr) {
        } else {
            var zipFilePath = path.join(toDir, zipFileName);
                .on('error', function (err) {
                .on('finish', function () {
                    var readStream = fs.createReadStream(zipFilePath),
                        writeStream = fstream.Writer(toDir);

// eg