【发布时间】:2016-06-29 22:43:26
【问题描述】:
我在 Powershell 脚本中使用 zip 压缩时遇到问题。有问题的代码 sn-p 是:
$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()
代码尝试从存档中删除文件,然后添加同一文件的更新版本。当我运行脚本时,我收到以下信息:
[错误] 找不到类型 [System.IO.Compression.ZipArchiveMode]。 确保包含此类型的 [ERROR] 程序集是 加载。 [错误] 在 C:\xxxxx\xxxxx\xxxxx\PowerShellIDEInstallers\PowerShel [错误] lIDEInstallers\VSInstallCBH.ps1:141 char:2 [错误] + $mode = [System.IO.Compression.ZipArchiveMode]::Update [错误] +
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~ [错误] + 类别信息:无效操作: (System.IO.Compression.ZipArch [错误] iveMode:TypeName) [], RuntimeException [错误] + FullyQualifiedErrorId:TypeNotFound [错误]
但是,如果我再次运行它,它将正常工作。 我发现一些帖子(this 和this)谈到了类似的问题。我目前正在使用:
Add-Type -AssemblyName System.IO.Compression.FileSystem
在脚本的顶部。我还发现 this post 看起来很有希望,但是没有用。我还应该补充一点,问题发生在 ISE、Visual Studio 和命令提示符中。如果我在任何环境中再次运行该代码,该代码将起作用。
我很困惑,不知所措。谁能告诉我为什么会这样?
【问题讨论】:
标签: .net powershell zip