diff --git a/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1 b/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1 index e7dd78d..f1e5ae7 100644 --- a/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1 +++ b/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1 @@ -284,6 +284,11 @@ function Expand-Archive ValueFromPipelineByPropertyName=$false)] [switch] $Force, + [parameter (mandatory=$false, + ValueFromPipeline=$false, + ValueFromPipelineByPropertyName=$false)] + [System.Text.Encoding] $EntryEncoding, + [switch] $PassThru = $false ) @@ -292,7 +297,6 @@ function Expand-Archive { $isVerbose = $psboundparameters.ContainsKey("Verbose") $isConfirm = $psboundparameters.ContainsKey("Confirm") - $isDestinationPathProvided = $true if($DestinationPath -eq [string]::Empty) { @@ -401,7 +405,7 @@ function Expand-Archive } } - ExpandArchiveHelper $resolvedSourcePaths $resolvedDestinationPath ([ref]$expandedItems) $Force $isVerbose $isConfirm + ExpandArchiveHelper $resolvedSourcePaths $resolvedDestinationPath ([ref]$expandedItems) $EntryEncoding $Force $isVerbose $isConfirm $isArchiveFileProcessingComplete = $true } @@ -931,6 +935,7 @@ function ExpandArchiveHelper [string] $archiveFile, [string] $expandedDir, [ref] $expandedItems, + [System.Text.Encoding] $entryEncoding, [boolean] $force, [boolean] $isVerbose, [boolean] $isConfirm @@ -945,7 +950,13 @@ function ExpandArchiveHelper $archiveFileStreamArgs = @($archiveFile, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read) $archiveFileStream = New-Object -TypeName System.IO.FileStream -ArgumentList $archiveFileStreamArgs - $zipArchiveArgs = @($archiveFileStream, [System.IO.Compression.ZipArchiveMode]::Read, $false) + if ($entryEncoding -eq $null) { + $zipArchiveArgs = @($archiveFileStream, [System.IO.Compression.ZipArchiveMode]::Read, $false) + } + else + { + $zipArchiveArgs = @($archiveFileStream, [System.IO.Compression.ZipArchiveMode]::Read, $false, $entryEncoding) + } try { $zipArchive = New-Object -TypeName System.IO.Compression.ZipArchive -ArgumentList $zipArchiveArgs diff --git a/Tests/EncodedWith936.archive b/Tests/EncodedWith936.archive new file mode 100644 index 0000000..f6411a3 Binary files /dev/null and b/Tests/EncodedWith936.archive differ diff --git a/Tests/Pester.Commands.Cmdlets.Archive.Tests.ps1 b/Tests/Pester.Commands.Cmdlets.Archive.Tests.ps1 index 7c3c619..a8c05d9 100644 --- a/Tests/Pester.Commands.Cmdlets.Archive.Tests.ps1 +++ b/Tests/Pester.Commands.Cmdlets.Archive.Tests.ps1 @@ -38,6 +38,9 @@ Describe "Test suite for Microsoft.PowerShell.Archive module" -Tags "BVT" { $preCreatedArchivePath = Join-Path $script:TestSourceRoot "TrailingSpacer.archive" Copy-Item $preCreatedArchivePath $TestDrive$($DS)TrailingSpacer.zip -Force + + $preCreatedArchivePath = Join-Path $script:TestSourceRoot "EncodedWith936.archive" + Copy-Item $preCreatedArchivePath $TestDrive$($DS)EncodedWith936.zip -Force } AfterAll { @@ -1179,6 +1182,16 @@ Describe "Test suite for Microsoft.PowerShell.Archive module" -Tags "BVT" { Compare-Object -ReferenceObject $extractedList -DifferenceObject $sourceList -PassThru | Should Be $null } + It "Validate Expand-Archive works with zip files without utf8 filenames" { + $archivePath = "$TestDrive$($DS)EncodingWith936.zip" + $destinationPath = "$TestDrive$($DS)EncodingWith936" + $filePath = "$TestDrive$($DS)EncodingWith936$($DS)LICENSE - 副本" + + Expand-Archive -Path $archivePath -DestinationPath $destinationPath -EntryEncoding ([system.text.encoding]::GetEncoding(936)) + Test-Path $filePath | Should Be $true + } + + # trailing spaces give this error on Linux: Exception calling "[System.IO.Compression.ZipFileExtensions]::ExtractToFile" with "3" argument(s): "Could not find a part of the path '/tmp/02132f1d-5b0c-4a99-b5bf-707cef7681a6/TrailingSpacer/Inner/TrailingSpace/test.txt'." It "Validate Expand-Archive works with zip files where the contents contain trailing whitespace" -skip:(!$IsWindows) { $archivePath = "$TestDrive$($DS)TrailingSpacer.zip"