【问题标题】:Node module unzip file with password带密码的节点模块解压文件
【发布时间】:2018-07-31 21:14:11
【问题描述】:

我正在寻找节点包来解压缩受密码保护的 zip 文件。 截至目前我已经看过here

但是上面提供了打开文件和解析文件的功能。我只需要在一个地方提取文件。

【问题讨论】:

    标签: node.js npm unzip


    【解决方案1】:

    如果我正确理解了您的问题,并且您仍在寻找答案 在同一个包中检查这些函数:

    Unzipper File Wise Extracting Options

    Method specific extract

    这些可以帮助您将文件(全部或选定的)提取到所需位置

    【讨论】:

      【解决方案2】:

      如果您想从目录中解压缩受密码保护的文件 -

      const unzipper = require("unzipper");
      
      const unzipAndUnlockZipFile = async (filepath, password) => {
        try {
          const zipDirectory = await unzipper.Open.file(filepath); // unzip a 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 (password is optional, do not pass this if the file is not password-protected)
      
          console.log(extracted.toString()); // file content
        } catch (e) {
          console.log(e);
        }
      };
      
      const zipFilePath = "./src/application.zip";
      const password = "1234";
      unzipAndUnlockZipFile(zipFilePath, password);
      

      如果您想解压缩并从缓冲区中提取文件,则解决方案如下所示 -

      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 (password is optional, do not pass this if the file is not password-protected)
      
          console.log(extracted.toString()); // file content
        } catch (e) {
          console.log(e);
        }
      };
      
      const zippedFileBase64 = "{{BASE64}}";
      const password = "1234";
      unzipAndUnlockZipFileFromBuffer(zippedFileBase64, password);
      

      【讨论】:

        猜你喜欢
        • 2023-03-31
        • 1970-01-01
        • 2020-11-04
        • 2018-02-26
        • 1970-01-01
        • 2013-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多