Check if a Zip file is password protected

2.1k views Asked by At

I am using PowerShell to extract multiple ZIP files. Some of these files are password protected. The requirement is to skip the files that are password protected. When this code encounters a password protected file, it displays a box to enter the password. Is there any way to check if the ZIP file is password protected before I extract it? I tried DotNetZip too, but was not able to find a way to determine if the file is password protected.

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

Update:

I am able to detect encrypted files using DotNetZip. It would be better if I can do this without 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

There are 1 answers

0
Ansgar Wiechers On

You could specify the NOERRORUI flag as the second argument to the CopyHere method:

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

This will silently skip the contents of password-protected zip files.