Skip to content

Commit

Permalink
(GH-616) Minor fixes for 7z stdout capturing.
Browse files Browse the repository at this point in the history
Only print 7z's stdout in verbose mode, buffer the file list while
writing to <packagename>Install.zip.txt, and don't create that file at
all if the archive was empty.
  • Loading branch information
Erlkoenig90 committed Feb 8, 2016
1 parent 93797b3 commit 4a2b56d
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions src/chocolatey.resources/helpers/functions/Get-ChocolateyUnzip.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,52 @@ param(
if ($zipExtractLogFullPath) {
# Redirect stdout for processing by choco
$process.StartInfo.RedirectStandardOutput = $true
$process.Start() | Out-Null

[void] $process.Start()

# Capture 7z's output into a StringBuilder and write it out in blocks, to improve I/O performance.
$sb = New-Object -TypeName "System.Text.StringBuilder";

# Remember whether anything was written to the file
$written = $false
# Open the file once
$wStream = new-object IO.FileStream($zipExtractLogFullPath, [IO.FileAccess]::Write)
$sWriter = new-object IO.StreamWriter($wStream)

# Read each line from 7z's stdout synchroneously (ReadLine blocks).
# Since stderr is not redirected, it gets automatically printed to the console, avoiding deadlocks.
while(($process.StandardOutput -ne $null) -and (($line = $process.StandardOutput.ReadLine()) -ne $null)) {
if($line.StartsWith("Extracting")) {
# This is a line indicating an extracted file
$file = $destination + "\" + $line.Substring(12)
# Save the filename
Add-Content $zipExtractLogFullPath $file
# Save the filename into the StringBuffer
[void] $sb.Append($file + "`r`n")

$written = $true;
# Write out every 1MiB blocks, to save memory
if ($sb.Length -ge 1048576) {
$sWriter.Write($sb)
[void] $sb.Clear();
}
}
# Print the line, such that it looks as if stdout was not redirected
Write-Debug $line
Write-Verbose $line
}
# Write remaining buffered lines
if ($sb.Length -gt 0) {
$sWriter.Write($sb)
[void] $sb.Clear();
}
# Close file
$sWriter.Close();
$wStream.Close();

# If nothing was written at all, we don't need the file - remove it
if (!$written) {
Remove-Item -Force $zipExtractLogFullPath
}
} else {
# If we don't want to capture the file list, just execute 7z without stdout redirection
$process.Start() | Out-Null
[void] $process.Start()
}

# Wait for 7z to finish. Even if 7z has closed its stdout, and all lines have been read, the process might not have quit yet.
Expand Down

0 comments on commit 4a2b56d

Please sign in to comment.