I'm making a Powershell (Version 3.0, .Net framework 4.5) script that updates some files. However, some of them need to be inspected first.
I open a JAR file and get an entry's content with the following code:
[Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')
$zentry = [IO.Compression.ZipFile]::OpenRead($moduleJarPath).Entries | Where-Object {$_.FullName -match '.*pom.xml'}
The entry is read using a System.IO.StreamReader, that is closed right after the read, in a finally block.
Further down the script, I'm going to update the JAR file with a newer one, that might have the same name. In that case, the script fails with:
Copy-Item : The process cannot access the file 'E:\path\to\my\artifact.jar' because it is being used by another process.
It looks like the lock is held by my own script, which seems logical since I just accessed the JAR. I'd like to close the handle so that my script can overwrite the JAR.
In the IO.Compression.ZipFile documentation, there are no "close" method.
How can I release the lock?
Thanks! :)
ZipArchive
, which is returned by ZipFile.OpenRead()
, implements IDisposable
so you would call Dispose()
e.g.
$zipFile = [IO.Compression.ZipFile]::OpenRead($moduleJarPath)
$zentry = $zipFile.Entries | Where-Object {$_.FullName -match '.*pom.xml'}
...
$zipFile.Dispose()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With