Issue with System.IO.Compression

0

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:

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?

powershell
zip
asked on Stack Overflow Aug 8, 2017 by Alterant • edited Aug 8, 2017 by Ansgar Wiechers

2 Answers

0

Deinstalled NET 4.6.1, installed NET 4.6.2. Issue was fixed.

answered on Stack Overflow Aug 9, 2017 by Alterant
0

Finally the issue was discovered. We use a custom solution which is using NLog. NLog adds a reference to System.IO.Compression to the solution. During the installation of the solution it deploys the dll (probably the old one or in some improper way). We excluded dll from the installer and now everything work fine.

answered on Stack Overflow Oct 15, 2017 by Alterant

User contributions licensed under CC BY-SA 3.0