Search code examples
javascriptnode.jselectronzipzlib

Uncaught Error: incorrect header check in zlib in node.js


I am making an Electron app that downloads a zip file using fetch and fs, and then extracts it to a folder using zlib. However, I keep getting this error:

Uncaught Error: incorrect header check at __node_internal_genericNodeError (node:internal/errors:867:15) at Zlib.zlibOnError [as onerror] (node:zlib:189:17)

After looking it up for an hour and even trying to ask an AI to find the answer, the response I kept getting was that the zip file must be corrupted. However, after checking both the original zip file and the downloaded one, neither are corrupted. What's wrong? Here's my script, if it helps:

if(!fs.existsSync(destinationFolder)){
  fs.mkdirSync(destinationFolder);
}
fs.createReadStream(zipFileLocation).pipe(zlib.createUnzip()).pipe(fs.createWriteStream(destinationFolder));

Solution

  • The "Unzip" object associated with zlib is poorly named. It will decompress zlib or deflate streams. If what you have is actually a zip file, then zlib cannot extract anything from it on its own. You would need to decode the zip file structure, and then feed just the deflate compressed data to zlib.

    A quick google search turned up adm-zip as something that can work with zip files.