Skip to content

Commit 0941ed5

Browse files
author
Andrew
authored
Merge pull request #20 from SteveL-MSFT/master
Addresses issue #12 where archives contains items with trailing whitespace
2 parents bb05026 + 7bc632f commit 0941ed5

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ function Expand-Archive
414414
{
415415
# delete the expanded file/directory as the archive
416416
# file was not completly expanded.
417-
$expandedItems | % { Remove-Item $_ -Force -Recurse }
417+
$expandedItems | % { Remove-Item "$_" -Force -Recurse }
418418
}
419419
}
420420
elseif ($PassThru -and $expandedItems.Count -gt 0)
@@ -1007,6 +1007,9 @@ function ExpandArchiveHelper
10071007
# If the Parent directory of the current entry in the archive file does not exist, then create it.
10081008
if($parentDirExists -eq $false)
10091009
{
1010+
# note that if any ancestor of this directory doesn't exist, we don't recursively create each one as New-Item
1011+
# takes care of this already, so only one DirectoryInfo is returned instead of one for each parent directory
1012+
# that only contains directories
10101013
New-Item $currentArchiveEntryFileInfo.DirectoryName -Type Directory -Confirm:$isConfirm | Out-Null
10111014

10121015
if(!(Test-Path -LiteralPath $currentArchiveEntryFileInfo.DirectoryName -PathType Container))
@@ -1056,6 +1059,11 @@ function ExpandArchiveHelper
10561059

10571060
if(!$hasNonTerminatingError)
10581061
{
1062+
# The ExtractToFile() method doesn't handle whitespace correctly, strip whitespace which is consistent with how Explorer handles archives
1063+
# There is an edge case where an archive contains files whose only difference is whitespace, but this is uncommon and likely not legitimate
1064+
[string[]] $parts = $currentArchiveEntryPath.Split([System.IO.Path]::DirectorySeparatorChar) | % { $_.Trim() }
1065+
$currentArchiveEntryPath = [string]::Join([System.IO.Path]::DirectorySeparatorChar, $parts)
1066+
10591067
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($currentArchiveEntry, $currentArchiveEntryPath, $false)
10601068

10611069
# Add the expanded file path to the $expandedItems array,

Tests/Pester.Commands.Cmdlets.Archive.Tests.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ Describe "Test suite for Microsoft.PowerShell.Archive module" -Tags "BVT" {
2626

2727
$preCreatedArchivePath = Join-Path $script:TestSourceRoot "SamplePreCreatedArchive.archive"
2828
Copy-Item $preCreatedArchivePath $TestDrive\SamplePreCreatedArchive.zip -Force
29+
30+
$preCreatedArchivePath = Join-Path $script:TestSourceRoot "TrailingSpacer.archive"
31+
Copy-Item $preCreatedArchivePath $TestDrive\TrailingSpacer.zip -Force
2932
}
3033

3134
function Add-CompressionAssemblies {
@@ -1058,5 +1061,20 @@ Describe "Test suite for Microsoft.PowerShell.Archive module" -Tags "BVT" {
10581061

10591062
Compare-Object -ReferenceObject $extractedList -DifferenceObject $sourceList -PassThru | Should Be $null
10601063
}
1064+
1065+
It "Validate Expand-Archive works with zip files where the contents contain trailing whitespace" {
1066+
$archivePath = "$TestDrive\TrailingSpacer.zip"
1067+
$destinationPath = "$TestDrive\TrailingSpacer"
1068+
# we can't just compare the output and the results as you only get one DirectoryInfo for directories that only contain directories
1069+
$expectedPaths = "$TestDrive\TrailingSpacer\Inner\TrailingSpace","$TestDrive\TrailingSpacer\Inner\TrailingSpace\test.txt"
1070+
1071+
$contents = Expand-Archive -Path $archivePath -DestinationPath $destinationPath -PassThru
1072+
1073+
$contents.Count | Should Be $expectedPaths.Count
1074+
1075+
for ($i = 0; $i -lt $expectedPaths.Count; $i++) {
1076+
$contents[$i].FullName | Should Be $expectedPaths[$i]
1077+
}
1078+
}
10611079
}
10621080
}

Tests/TrailingSpacer.archive

156 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)