Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Powershell中,如何在ZipFile.OpenRead之后关闭句柄?_Powershell_Powershell 3.0 - Fatal编程技术网

在Powershell中,如何在ZipFile.OpenRead之后关闭句柄?

在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

我正在制作一个Powershell(3.0版,.NETFramework 4.5版)脚本来更新一些文件。然而,其中一些需要首先检查

我打开一个JAR文件,获得一个条目的内容,代码如下:

[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()