Search code examples
node.jsdirectorybufferunzip

How to unzip a buffer to a directory with Nodejs with minimum dependencies?


I'm receiving a buffer from an API with a Get request. The buffer contains a directory/file structure.

I want to extract the buffer to the file system to a specific base folder.

I'm currently using unzipper, but it fails on Nodejs v19.8.0 and v19.8.1 (it was working flawlessly so far).

My current code is:

function unzipBufferToFolder(data, folder, callback)
{
    unzip.Open.buffer(data)
        .then(d => d.extract({path: folder}), reportError)
        .then(callback, reportError)
}

Unzipper comes with a ton of dependencies, and some of them fail in the newer Nodejs version. It unzips the files without error messages. However, the unzipped files are broken.

How to unzip a buffer to a directory in Nodejs with minimum dependencies?


Solution

  • ADM-ZIP can extract a ZIP buffer to a folder with zero dependencies and zero promises.

    const AdmZip = require('adm-zip')
    
    function unzipBufferToFolder(data, folder)
    {
        new AdmZip(data).extractAllTo(folder, /** overwrite **/ true)
    }