Skip to content

Commit

Permalink
Added retry when unzip fail because of AV (#1822)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hollow1838 authored and r15ch13 committed Mar 12, 2018
1 parent 7e178ae commit 071af50
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,49 @@ function env($name,$global,$val='__get') {
else { [environment]::setEnvironmentVariable($name,$val,$target) }
}

function unzip($path,$to) {
if(!(test-path $path)) { abort "can't find $path to unzip"}
function isFileLocked([string]$path) {
$file = New-Object System.IO.FileInfo $path

if ((Test-Path -Path $path) -eq $false) {
return $false
}

try {
$stream = $file.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)
if ($stream) {
$stream.Close()
}
return $false
}
catch {
# file is locked by a process.
return $true
}
}

function unzip($path, $to) {
if (!(test-path $path)) { abort "can't find $path to unzip"}
try { add-type -assembly "System.IO.Compression.FileSystem" -ea stop }
catch { unzip_old $path $to; return } # for .net earlier than 4.5
$retries = 0
while ($retries -le 10) {
if ($retries -eq 10) {
if (7zip_installed) {
extract_7zip $path $to $false
return
} else {
abort "Unzip failed: Windows can't unzip because a process is locking the file.`nRun 'scoop install 7zip' and try again."
}
}
if (isFileLocked $path) {
write-host "Waiting for $path to be unlocked by another process... ($retries/10)"
$retries++
Start-Sleep -s 2
} else {
break
}
}

try {
[io.compression.zipfile]::extracttodirectory($path,$to)
} catch [system.io.pathtoolongexception] {
Expand Down

0 comments on commit 071af50

Please sign in to comment.