Download big file to temp one to avoid possible download failure and use corrupted file. (#490)

This commit is contained in:
Quanmao LIU 2016-07-08 09:51:16 +08:00 коммит произвёл GitHub
Родитель db95eb5de8
Коммит e75eb189e0
1 изменённых файлов: 10 добавлений и 1 удалений

Просмотреть файл

@ -65,6 +65,7 @@ function Replace-VariableInFile($variable, $value, $sourceFile, $targetFile)
function Download-File($url, $output)
{
$output = [System.IO.Path]::GetFullPath($output)
if (test-path $output)
{
Write-Output "[downloadtools.Download-File] $output exists. No need to download."
@ -83,7 +84,13 @@ function Download-File($url, $output)
-SourceIdentifier Web.DownloadProgressChanged -Action {
$Global:Data = $event
}
$wc.DownloadFileAsync($url, $output)
$tmpOutput = $output + ".tmp.download"
if (test-path $tmpOutput) {
Remove-Item $tmpOutput
}
$wc.DownloadFileAsync($url, $tmpOutput)
While (!($Global:downloadComplete)) {
$percent = $Global:Data.SourceArgs.ProgressPercentage
$totalBytes = $Global:Data.SourceArgs.TotalBytesToReceive
@ -92,6 +99,8 @@ function Download-File($url, $output)
Write-Progress -Activity ("Downloading file to {0} from {1}" -f $output,$url) -Status ("{0} bytes \ {1} bytes" -f $receivedBytes,$totalBytes) -PercentComplete $percent
}
}
Rename-Item $tmpOutput -NewName $output
Write-Progress -Activity ("Downloading file to {0} from {1}" -f $output, $url) -Status ("{0} bytes \ {1} bytes" -f $receivedBytes,$totalBytes) -Completed
Unregister-Event -SourceIdentifier Web.DownloadFileCompleted
Unregister-Event -SourceIdentifier Web.DownloadProgressChanged