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"
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)
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With