Question:
I am trying to get this Powershell code to work:
1 2 3 4 5 6 7 |
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:
1 2 |
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:
1 2 |
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:
1 2 3 4 5 |
[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" |
Answer:
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:
1 2 3 |
[System.IO.Compression.ZipFile]::CreateFromDirectory($startPath, $zipPath) [System.IO.Compression.ZipFile]::ExtractToDirectory($zipPath, $extractPath) |