检查Zip文件是否受密码保护

时间:2015-06-09 19:44:26

标签: powershell zip

我使用PowerShell提取多个ZIP文件。其中一些文件受密码保护。要求是跳过受密码保护的文件。当此代码遇到受密码保护的文件时,它会显示一个输入密码的框。有什么方法可以在我提取之前检查ZIP文件是否受密码保护?我也尝试了DotNetZip,但无法找到确定文件是否受密码保护的方法。

$shell = new-object -com shell.application
$zip = $shell.NameSpace("C:\ZipFile.zip")
foreach ($item in $zip.items()) {
  $shell.Namespace("C:\ExtractedFiles").copyhere($item)
}

更新

我可以使用DotNetZip检测加密文件。如果我能在没有DotNetZip的情况下做到这一点会更好。

[System.Reflection.Assembly]::LoadFrom("C:\Ionic.Zip.dll") 
$zipfile = [Ionic.Zip.ZipFile]::Read($file.FullName) 
$encFlag = $false
foreach ($file in $zipfile) {
  if ($file.UsesEncryption -eq $true) {
    $encFlag = $True
  }
}
Write-Host "Enctryption: " $encFlag

1 个答案:

答案 0 :(得分:1)

您可以将NOERRORUI标志指定为CopyHere方法的第二个参数:

$shell = new-object -com shell.application
$zip = $shell.NameSpace("C:\ZipFile.zip")
foreach ($item in $zip.items()) {
  $shell.Namespace("C:\ExtractedFiles").copyhere($item, 1024)
}

这将默默跳过受密码保护的zip文件的内容。