Node JS无法提取Windows中的zip文件

我正在尝试在窗口终端中解压缩受密码保护的zip文件。但是我得到了错误 “错误:'zip'无法识别为内部或外部命令, 可操作的程序或批处理文件。”

var spawn = require('child_process').spawn;
let filePath = "XXX/XX";
let password= "abc";
extractZipWithPassword(filePath,password)
function extractZipWithPassword(filePath,password) {
    console.log("Inside here:::::::::::::::::::",filePath);
        var dir = spawn('zip',['-P',password,'-j','-',filePath],{shell:true});
        dir.stderr.on('data',(data) => {
            console.log('Error: '+data);
            return filePath;
           })
        dir.on('close',(code) => {
            console.log("On closing:::::::::::::::")
            return filePath;
        });
}
chenruibo 回答:Node JS无法提取Windows中的zip文件

zip不是Powershell内置结构。您将要使用Expand-Archive cmdlet。

Expand-Archive -LiteralPath C:\Archives\Draft.Zip -DestinationPath C:\Reference

但是,Expand-Archive不适用于受密码保护的存档。不过,就我而言,我使用了7zip来提取它们(请注意,您必须首先确保当前工作目录是目标解压缩目录):

spawn('7z',['x',filePath,'-p' + password],{ shell: true,cwd: destinationUnpackPath })

上面要解释的几件事:

  • -pPASSWORD会将PASSWORD替换为实际密码。因此,如果您的密码是“ 12345”,则传递的参数将是-p12345
  • 请注意,我在cwd选项中添加了spawn,以确保正确设置了当前工作目录。 7z x会将存档提取到cwd
  • 上面的代码中未定义
  • destinationUnpackPath,但应将其设置为目标提取路径
本文链接:https://www.f2er.com/3107741.html

大家都在问