【问题标题】:Check if a Zip file is password protected检查 Zip 文件是否受密码保护
【发布时间】:2015-08-24 19:49:22
【问题描述】:

我正在使用 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

【问题讨论】:

    标签: powershell zip


    【解决方案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 文件的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 2014-12-30
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多