Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use System.IO.Compression.FileSystem.dll

Tags:

powershell

zip

I am trying to get this Powershell code to work:

     Add-Type -Path 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.IO.Compression.FileSystem.dll'
     $startPath = "D:\nade\CBA\Zip\StartPath"
     $zipPath = "D:\nade\CBA\Zip\result.zip"
     $extractPath = "D:\nade\CBA\Zip\Extract"
     [System.IO.Compression.FileSystem.ZipFile]::CreateFromDirectory($startPath, $zipPath)
     [System.IO.Compression.FileSystem.ZipFile]::ExtractToDirectory($zipPath, $extractPath)

However I get the following error:

Unable to find type [System.IO.Compression.FileSystem.ZipFile]. Make sure that the assembly that contains this type is loaded.

I have tried using the other DLL located here:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\System.IO.Compression.FileSystem.dll

However I still get the same error.

How can I correctly import this library?

EDIT: I have tried all of the following, none of them worked:

[System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem")
Add-Type -Path 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\System.IO.Compression.FileSystem.dll'
Add-Type -Path 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.IO.Compression.FileSystem.dll'
Add-type -AssemblyName "System.IO.Compression.FileSystem"
like image 291
David Klempfner Avatar asked Dec 03 '22 17:12

David Klempfner


2 Answers

Turns out I was using an incorrect namespace.

ZipFile is located in the System.IO.Compression namespace, whereas the assembly is called System.IO.Compression.FileSystem.dll.

Ie. it had nothing to do with loading the assembly, I just need to use the correct namespace:

     [System.IO.Compression.ZipFile]::CreateFromDirectory($startPath, $zipPath)
     [System.IO.Compression.ZipFile]::ExtractToDirectory($zipPath, $extractPath)
like image 162
David Klempfner Avatar answered Dec 06 '22 20:12

David Klempfner


I ran into a similar error and could not get it to work (Windows Server 2008R2). After installing .NET 4.5 and Powershell 3.0 (as found in some forums) it still would not work. It only worked after I added both these lines:

Add-Type -assembly "System.IO.Compression" Add-Type -assembly "System.IO.Compression.Filesystem"

like image 32
Daap Avatar answered Dec 06 '22 18:12

Daap