Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Fix for Issue #88 #148

Open
wants to merge 1 commit into
base: servicing
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -292,7 +297,6 @@ function Expand-Archive
{
$isVerbose = $psboundparameters.ContainsKey("Verbose")
$isConfirm = $psboundparameters.ContainsKey("Confirm")

$isDestinationPathProvided = $true
if($DestinationPath -eq [string]::Empty)
{
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -931,6 +935,7 @@ function ExpandArchiveHelper
[string] $archiveFile,
[string] $expandedDir,
[ref] $expandedItems,
[System.Text.Encoding] $entryEncoding,
[boolean] $force,
[boolean] $isVerbose,
[boolean] $isConfirm
Expand All @@ -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
Expand Down
Binary file added Tests/EncodedWith936.archive
Binary file not shown.
13 changes: 13 additions & 0 deletions Tests/Pester.Commands.Cmdlets.Archive.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
Expand Down