在Powershell中,如何在ZipFile.OpenRead之后关闭句柄?
我正在制作一个Powershell(3.0版,.NETFramework 4.5版)脚本来更新一些文件。然而,其中一些需要首先检查 我打开一个JAR文件,获得一个条目的内容,代码如下:在Powershell中,如何在ZipFile.OpenRead之后关闭句柄?,powershell,powershell-3.0,Powershell,Powershell 3.0,我正在制作一个Powershell(3.0版,.NETFramework 4.5版)脚本来更新一些文件。然而,其中一些需要首先检查 我打开一个JAR文件,获得一个条目的内容,代码如下: [Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') $zentry = [IO.Compression.ZipFile]::OpenRead($moduleJarPath).Entries | Where-O
[Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')
$zentry = [IO.Compression.ZipFile]::OpenRead($moduleJarPath).Entries | Where-Object {$_.FullName -match '.*pom.xml'}
使用System.IO.StreamReader读取条目,读取后立即在finally块中关闭
在脚本的后面,我将用一个新的JAR文件更新JAR文件,该文件可能具有相同的名称。在这种情况下,脚本失败,原因是:
Copy-Item : The process cannot access the file 'E:\path\to\my\artifact.jar' because it is being used by another process.
看起来锁是由我自己的脚本持有的,这似乎是合乎逻辑的,因为我刚刚访问了JAR。我想关闭句柄,这样我的脚本就可以覆盖JAR了
在IO.Compression.ZipFile中,没有“关闭”方法
我怎样才能开锁
谢谢!:)
ZipFile.OpenRead()
返回的ZipArchive,实现了IDisposable
,因此您可以调用Dispose()
,例如
$zipFile = [IO.Compression.ZipFile]::OpenRead($moduleJarPath)
$zentry = $zipFile.Entries | Where-Object {$_.FullName -match '.*pom.xml'}
...
$zipFile.Dispose()