'Unzip a password-protected archive using node-js

I need to read a content of a password-protected archive (zip is preferred) to a node-js app, without writing the protected content to a file

In addition, the app is cross-platform so solution such this doesn't help

I looked also here but there is no code in the answer



Solution 1:[1]

This solution will read the file buffer which you can get from base64 or by reading the zip file, after that unzipping and opening the password-protected file is done in-memory. I hope this helps -

const unzipper = require("unzipper");

const unzipAndUnlockZipFileFromBuffer = async (zippedFileBase64, password) => {
  try {
    const zipBuffer = Buffer.from(zippedFileBase64, "base64"); // Change base64 to buffer
    const zipDirectory = await unzipper.Open.buffer(zipBuffer); // unzip a buffered file
    const file = zipDirectory.files[0]; // find the file you want

    // if you want to find a specific file by path
    // const file = zipDirectory.files.find((f) => f.path === "filename");

    const extracted = await file.buffer(password); // unlock the file with the password

    console.log(extracted.toString()); // file content
  } catch (e) {
    console.log(e);
  }
};

const zippedFileBase64 = "{{BASE64}}";
const password = "1234";
unzipAndUnlockZipFileFromBuffer(zippedFileBase64, password);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nakul Sharma