我正在尝试编写一个脚本,该脚本从Internet获取zip文件,并使用流将单个文件从中提取到磁盘。
问题是提取的文件太大了。
如果有人能帮我找到我的代码有什么问题,我会很高兴的。我非常想用流来获得答案。
下载的文件(截至今天)为:https://bitbucket.org/rude/love/downloads/love-0.10.2-win64.zip
我的代码:
{{1}}
答案 0 :(得分:1)
### Download current LOVE framework executable ###
# By default PowerShell supports only SSL3 and TLS1.0, add TLS1.1 and TLS1.2 support.
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
# Get current version of the LOVE framework
$request = Invoke-WebRequest "https://love2d.org/"
$downloadUri = (Select-String -InputObject $request.Content -Pattern "https://bitbucket\.org/rude/love/downloads/love-.*-win64\.zip").Matches[0].Value
$loveBinaryStream = (Invoke-WebRequest $downloadUri).RawContentStream
# Extract love.exe
Add-Type -AssemblyName System.IO.Compression
Add-Type -AssemblyName system.io.compression.filesystem # for the System.IO.Compression.ZipFileExtensions
$zipArchive = New-Object System.IO.Compression.ZipArchive($loveBinaryStream)
$zipEntry = $zipArchive.Entries | ? { $_.Name -eq "love.exe" }
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($zipEntry, "path_to_file")
答案 1 :(得分:0)
我认为这是某种编码问题。
无论如何,用这个代替:
$stream = $zipEntry.Open()
$sr = New-Object System.IO.StreamReader($stream)
$sw = New-Object System.IO.StreamWriter("love.exe")
$sw.Write($sr.ReadToEnd())
$sw.Close()
$sr.Close()
$stream.Close()
这个
# Save to disk
$sw = [System.IO.FileStream]::new($tempLovePath, [System.IO.FileMode]::Create)
$stream = $zipEntry.Open()
$stream.CopyTo($sw)
$stream.Close()
$sw.Close()
解决了这个问题。