1

I’m having a problem with using zip compression in a Powershell script. The code snippet in question is:

$zipfile = $targetFile
$file   = 'Script.ps1'

$stream = New-Object IO.FileStream($zipfile, [IO.FileMode]::Open)
$mode   = [System.IO.Compression.ZipArchiveMode]::Update
$zip    = New-Object IO.Compression.ZipArchive($stream, $mode)

($zip.Entries | ? { $file -contains $_.Name }) | % { $_.Delete() }

#   Add a newer Script.ps1 file with the new Comment Based Help template.
$newFile = "$PSScriptRoot\$file"
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip,$newFile,"Script.ps1","optimal")

#   Clean up.
$zip.Dispose()
$stream.Close()
$stream.Dispose()

The code attempts to delete a file from the archive and then add a newer version of the same file. When I run the script, I receive the following:

[ERROR] Unable to find type [System.IO.Compression.ZipArchiveMode]. Make sure that the [ERROR] assembly that contains this type is loaded. [ERROR] At C:\xxxxx\xxxxx\xxxxx\PowerShellIDEInstallers\PowerShel [ERROR] lIDEInstallers\VSInstallCBH.ps1:141 char:2 [ERROR] + $mode = [System.IO.Compression.ZipArchiveMode]::Update [ERROR] +
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [ERROR] + CategoryInfo : InvalidOperation: (System.IO.Compression.ZipArch [ERROR] iveMode:TypeName) [], RuntimeException [ERROR] + FullyQualifiedErrorId : TypeNotFound [ERROR]

However, if I run it again, it will work correctly. I found a few posts (this and this) that spoke of similar problems. I’m currently using:

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

At the top of the script. I also found this post which looked promising, but, did not work. I should also add that the problem occurs in the ISE, Visual Studio, and the command prompt. The code will work if I run it a second time in any of the environments.

I’m baffled and at a loss. Can anyone tell me why this is happening?

2 Answers 2

3

You're close. You need to load one more assembly in this case. Use:

Add-Type -AssemblyName System.IO.Compression
Sign up to request clarification or add additional context in comments.

Comments

2

Dave’s answer was a partial clue to the resolution. Changing the Add-Type command as he suggested made things better. However, the code still failed because the change he suggested now caused the command:

[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip,$newFile,"Script.ps1","optimal")

to fail.

I was able to correct that and fix the problem once I found:

To use the extension methods, you must reference the System.IO.Compression.FileSystem assembly in your project.

With Dave’s suggestion, I simply added the following to correct my problem:

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

The code now works correctly the first time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.