windowspowershellzip

Powershell: Unable to find type [System.IO.Compression.ZipArchiveMode]: TypeNotFound error


I am seeing following strange behavior with Windows Powershell.

Script1:

Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipArchiveMode]::Update

Output:

 PS C:\Users\Administrator> C:\Users\Administrator\Documents\Untitled1.ps1
Unable to find type [System.IO.Compression.ZipArchiveMode].
At C:\Users\Administrator\Documents\Untitled1.ps1:3 char:1
+ [System.IO.Compression.ZipArchiveMode]::Update
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.IO.Compression.ZipArchiveMode:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

Script2:

Add-Type -AssemblyName System.IO.Compression.FileSystem
[IO.Compression.ZipFile]::Open
[System.IO.Compression.ZipArchiveMode]::Update 

Output:

 PS C:\Users\Administrator> C:\Users\Administrator\Documents\Untitled1.ps1

OverloadDefinitions                                                                                                                                        
-------------------                                                                                                                                        
static System.IO.Compression.ZipArchive Open(string archiveFileName, System.IO.Compression.ZipArchiveMode mode)                                            
static System.IO.Compression.ZipArchive Open(string archiveFileName, System.IO.Compression.ZipArchiveMode mode, System.Text.Encoding entryNameEncoding)    
                                                                                                                                                           
Update

Why is Script1 not able to load [System.IO.Compression.ZipArchiveMode]::Update? How can I fix Script1 to ensure that [System.IO.Compression.ZipArchiveMode]::Update is loading properly?


Solution

  • You need to load the System.IO.Compression assembly too, not just System.IO.Compression.FileSystem:

    # Note: Only necessary in *Windows PowerShell*.
    # (These assemblies are automatically loaded in PowerShell (Core) 7+.)
    Add-Type -AssemblyName System.IO.Compression, System.IO.Compression.FileSystem
    
    # Verify that types from both assemblies were loaded.
    [System.IO.Compression.ZipArchiveMode]; [IO.Compression.ZipFile]