我正在编写一个脚本,尝试使用两种文件下载方法下载文件。 我将不做详细介绍,但是生成的函数如下所示:
function Download-FileRobust($url, $targetFile) {
try {
Download-File $url $targetFile
}
catch {
Download-FileWget $url $targetFile
}
}
当Download-File
函数失败时,PowerShell不会在位置$targetFile
处松开所创建文件的句柄,并且Download-FileWget
无法写入该位置。
我已经习惯了Python,所以花了我很长时间才弄清楚问题所在。
其他两个函数的源代码如下:
function Download-FileWget($url, $targetFile){
$wgetDir = (Get-ChildItem -Path "$env:userprofile\Downloads\wget*win32").FullName
if($env:Path -notlike "*$wgetDir*"){
$env:Path = "$wgetDir;$env:Path"
}
Invoke-Expression "wget '$url' -O '$targetFile'"
}
function Download-File($url, $targetFile){
$uri = New-Object "System.Uri" "$url"
$request = [System.Net.HttpWebRequest]::Create($uri)
$request.set_Timeout(15000) #15 second timeout
$response = $request.GetResponse()
$totalLength = [System.Math]::Floor($response.get_ContentLength()/1024)
$responseStream = $response.GetResponseStream()
$targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create
$buffer = new-object byte[] 10KB
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $count
while ($count -gt 0){
$targetStream.Write($buffer, 0, $count)
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $downloadedBytes + $count
Write-Progress -activity "Downloading file '$($url.split('/') | Select -Last 1)'" -status "Downloaded ($([System.Math]::Floor($downloadedBytes/1024))K of $($totalLength)K): " -PercentComplete ((([System.Math]::Floor($downloadedBytes/1024)) / $totalLength) * 100)
}
Write-Progress -activity "Finished downloading file '$($url.split('/') | Select -Last 1)'"
$targetStream.Flush()
$targetStream.Close()
$targetStream.Dispose()
$responseStream.Dispose()
}
答案 0 :(得分:1)
在Download-File
函数内部,您实际上从不执行任何操作来确保$targetStream
被正确冲洗和处置。您需要将流包装在try/catch/finally
块中:
function Download-File {
param([string]$url, [string]$targetFile)
$uri = New-Object "System.Uri" "$url"
$request = [System.Net.HttpWebRequest]::Create($uri)
$request.set_Timeout(15000) #15 second timeout
$response = $request.GetResponse()
$totalLength = [System.Math]::Floor($response.get_ContentLength()/1024)
$responseStream = $response.GetResponseStream()
try {
$targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create
try {
$buffer = new-object byte[] 10KB
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $count
while ($count -gt 0){
$targetStream.Write($buffer, 0, $count)
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $downloadedBytes + $count
Write-Progress -activity "Downloading file '$($url.split('/') | Select -Last 1)'" -status "Downloaded ($([System.Math]::Floor($downloadedBytes/1024))K of $($totalLength)K): " -PercentComplete ((([System.Math]::Floor($downloadedBytes/1024)) / $totalLength) * 100)
}
Write-Progress -activity "Finished downloading file '$($url.split('/') | Select -Last 1)'"
}
catch {
throw
}
finally {
if($targetStream){
$targetStream.Flush()
$targetStream.Close()
$targetStream.Dispose()
}
}
}
catch {
throw
}
finally {
$responseStream.Dispose()
}
}