Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 230
Abhay Gupta Avatar asked Oct 26 '25 19:10

Abhay Gupta


1 Answers

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]
like image 112
mklement0 Avatar answered Oct 28 '25 10:10

mklement0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!