1

I was wondering if it is possible to use https.get() from the Node standard library to download a zip and directly extract it into a subfolder.

I have found many solutions that download the zip first and extract it afterwards. But is there a way to do it directly?

This was my attempt:

const zlib = require("node:zlib");
const fs = require("fs");
const { pipeline } = require("node:stream");
const https = require("https");

const DOWNLOAD_URL =
  "https://downloadserver.com/data.zip";
const unzip = zlib.createUnzip();
const output = fs.createWriteStream("folderToExtract");

https.get(DOWNLOAD_URL, (res) => {
  pipeline(res, unzip, output, (error) => {
    if (error) console.log(error);
  });
});

But I get this error:

Error: incorrect header check
at Zlib.zlibOnError [as onerror] (node:zlib:189:17) {
errno: -3,
code: 'Z_DATA_ERROR'
}

I am curious, is this even possible?

Apollo
  • 1,296
  • 2
  • 11
  • 24

2 Answers2

3

Most unzippers start at the end of the zip file, reading the central directory there and using that to find the entries to unzip. This requires having the entire zip file accessible at the start.

What you'd need is a streaming unzipper, which starts at the beginning of the zip file. You can try unzip-stream and see if it meets your needs.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
0

I think this is similar to Simplest way to download and unzip files in Node.js cross-platform?

An answer in the above discussion using same package: enter image description here

You're getting the error probably because zlib only support gzip files and not zip

Aahan Agarwal
  • 391
  • 2
  • 6