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

16,161

Solution 1

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)

Solution 2

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"

Solution 3

I encountered this issue on a Win 2008 R2 server. Turned out I needed to update to PowerShell version 3.0 to work. I finally figured it out when I tried this:

Add-Type -Path "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.IO.Compression.FileSystem.dll"

and got a message about an incompatible version.

Now my code is working fine:

Add-Type -AssemblyName "System.IO.Compression.FileSystem"
[System.IO.Compression.ZipFile]::CreateFromDirectory("C:\Temp\lrressults", "C:\OutEmail\LoadRestReportzip") 

Solution 4

How to load assemblies in Powershell.

An example with obsolete LoadWithPartialName:

$sourceFolder = "c:\1\"
$destinationArc = "c:\1.zip" 
[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourceFolder, $destinationArc)

Another example with Add-Type (to load an assembly, use the -AssemblyName parameter):

Add-type -AssemblyName "System.IO.Compression.FileSystem";
$startPath = "c:\1\";
$zipPath = "c:\1.zip";
[System.IO.Compression.ZipFile]::CreateFromDirectory($startPath, $zipPath);
Share:
16,161
David Klempfner
Author by

David Klempfner

Follow me on Medium: https://medium.com/@DavidKlempfner I'm a software developer at Rockend in Sydney, Australia. I have a passion for programming, mainly in C#/.NET. I'm particularly interested in low level concepts and what goes on "behind the scenes", which is what my Medium.com articles are about. Apply for a job at Rockend: https://grnh.se/1aaf5d911

Updated on July 08, 2022

Comments

  • David Klempfner
    David Klempfner almost 2 years

    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"