locked
Issue with System.IO.Compression and .net 4.6.1 RRS feed

  • Question

  • Hello!

    I have a powershell v2 script with .net 4.5 which compresses old files. Like this:
    [reflection.assembly]::LoadWithPartialName("System.IO.Compression.FileSystem")
    ...
    $files = Get-Childitem ...
    ...
    $zip = [System.IO.Compression.ZipFile]::Open($arcpath + $curname, "Create")
    foreach ($onefile in $files) {
      [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $onefile.FullName, $onefile.Name, "Optimal") | out-null
    }
    
    It worked fine until I have installed .net 4.6.1 (which is needed for another project).

    Now it throws an error:
    The following exception occurred while retrieving member "Open": "Could not load file or assembly 'System.IO.Compression, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)"
    At line:1 char:7
    + $zip = <<<<  [System.IO.Compression.ZipFile]::Open('C:\Temp\1.zip', "Create")
         + CategoryInfo          : NotSpecified: (:) [], ExtendedTypeSystemException
         + FullyQualifiedErrorId : CatchFromBaseGetMember

    I installed WMF 5.1 to use native ps5 compression functions. But it also throws an error:
    PS C:\Temp\> Compress-Archive -Path $files -DestinationPath C:\Temp\Script\test.zip -CompressionLevel Optimal
    Add-Type : Could not load file or assembly 'System.IO.Compression, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:469 char:9
    +         Add-Type -AssemblyName System.IO.Compression
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Add-Type], BadImageFormatException
        + FullyQualifiedErrorId : System.BadImageFormatException,Microsoft.PowerShell.Commands.AddTypeCommand

    How can I fix it?
    Tuesday, August 8, 2017 6:32 AM

Answers

  • Probaly there was an issue with .NET 4.6.1 installation. Deintalled .NET 4.6.1, installed 4.6.2 and the issue has gone.
    • Marked as answer by Alterant Wednesday, August 9, 2017 9:20 AM
    Wednesday, August 9, 2017 9:20 AM

All replies

  • Works for me:

    Add-Type -AssemblyName System.IO.Compression.FileSystem
    [System.IO.Compression.ZipFile]::Open( ... )


    \_(ツ)_/

    Tuesday, August 8, 2017 6:50 AM
  • For me it also worked until .net 4.6.1

    Now in ps5 the command

    Add-Type -AssemblyName System.IO.Compression.FileSystem

    works without any issues, but the following command

    Compress-Archive -Path $files -DestinationPath C:\Temp\Script\t1.zip -CompressionLevel Optimal
    throws the error (see above).

    Tuesday, August 8, 2017 7:02 AM
  • Works fine for me.


    \_(ツ)_/

    Tuesday, August 8, 2017 7:07 AM
  • Probaly there was an issue with .NET 4.6.1 installation. Deintalled .NET 4.6.1, installed 4.6.2 and the issue has gone.
    • Marked as answer by Alterant Wednesday, August 9, 2017 9:20 AM
    Wednesday, August 9, 2017 9:20 AM