0

I tried to download zip files using node-fetch.

File successfully download and I can read the json file inside the zip using adm-zip NPM package.

But some times getting error when reeding the downloaded file. The zip file is corrupted during download.

When unzipping from file, it goes to .cpgz format

async function download(url, location) {
    try {
        const res = await fetch(url);
        await new Promise(async (resolve, reject) => {
            const fileStream = fs.createWriteStream(location);
            res.body.pipe(fileStream);
            res.body.on("error", (err) => {
                reject(err);
            });
            fileStream.on("finish", function () {
                resolve();
            });
        });
        return
    } catch (e) {
        console.log("Err on download files ", e)
    }
}

Reading this file using const zip = new AdmZip(file); throwing error UnhandledPromiseRejectionWarning: Error: Invalid or unsupported zip format. No END header found

I just make console for in the first section of code

const res = await fetch(url);
console.log(res)

Successfull files return PassThrough { _readableState: ReadableState { ..... ..... writable: true,

But for failed files

Gunzip {
  _writeState: Uint32Array [ 0, 0 ],
  _readableState: ReadableState {
....
....
writable: false,

Anyone can help?

Ashwanth Madhav
  • 1,084
  • 1
  • 9
  • 21

1 Answers1

0

I would rewrite a bit your code, depending on this SO: Node.js + request - saving a remote file and serving it in a response

const request = require('request');

async function download(url, location) {
    try {

        const fileStream = fs.createWriteStream(location);
        
        await new Promise((resolve, reject) => {

          const res = request(url);

          res.on('data', function(chunk) {
            // instead of loading the file into memory
            // after the download, we can just pipe
            // the data as it's being downloaded
            fileStream.write(chunk);
          });

          res.on('end', function() {
            resolve();
          });
        });
        
        return
    } catch (e) {
        console.log("Err on download files ", e)
    }
}
Peter
  • 1,280
  • 8
  • 15