From 335526e33dae32dd4debbb29794298a754576ffb Mon Sep 17 00:00:00 2001 From: Aoba Xu Date: Sat, 10 Jun 2023 15:35:00 +0800 Subject: [PATCH] Add EntryEncoding --- .../Microsoft.PowerShell.Archive.psm1 | 17 ++++++++++++++--- Tests/EncodedWith936.archive | Bin 0 -> 863 bytes .../Pester.Commands.Cmdlets.Archive.Tests.ps1 | 13 +++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 Tests/EncodedWith936.archive 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 0000000000000000000000000000000000000000..f6411a398a2e0cc17d7f81b15f0ce25a840ce14c GIT binary patch literal 863 zcmWIWW@Zs#U|`^2_*if%to-W&=WZqjhA`^?w2_gKnbN#b9Yhr`E`)3;n>{I?I?`HYdE91AtmkYC*&mGfxaALwTOCf`Uq6a6w zHMv!PU()QR*3=0X_Ut>}^V-o_G3$iyYW4LGIh&tbbS=Iz@BSQ1KE`>s(trH=dD>jV z=k=QHwGCHxZ7%gSD63ldthY@4v29y|4bQEYhoxm=jal!iUa($!@Y@BxUh7vZO|5BF z&6~^Q*xx9cq#0cnl**WB(zn5sr^7kq`mxM6?-qQ#w{4lD!;741%a@vG{#=+HSY5%D zkrH@UP3*j}_ww0>eP>ug_FR9Ebtplfan84cGp9KS83bF$Sp^m9_diR?6EU%Sf9Sa2 zp}0MMX)8JBqdVTZe&mxIld7&3PiSl}kljlwPTqa!f>7><%Dwjl##4YT%?O)8E z?eTZ!G^xDoPg8dCef#p_=z>N69;vBZ{Tz^9m}~qz|1R69fcpI77voOGW-mWCLG%Ay zKKJMOeBb)pzCFG-nOEa`EbH8df8OudK1qI7VN3JAT;7>yMck+CjxJhqfW3xcndHNq zyFNenU;X*^N;corn5)*yEIPQ%y#9$uo=-Ta)g#XN#%1rc+fo~xH*UJu9K)#n{e$zA zNTxs6-9D~jTKULBWOht}PnXfz?K3sv6FZxi9%0w}kgA-e+bEGA(G=biRxov)VT$*} z?yC7mE_z*2UAS%u6Pxt1tciKW%Mvz+N=FuoOi;|zJT=uxD(=IkI>)FpjQ=|sL$aqm zKO*X$)6FL0*%FfX&1=uP?1k;wXHfQJ+)3pCC*q77+Qbkoy$?-1h0@Slc!E%ty(hoQOBIXf(=jJ8*It1 zu;>mc-m~@xyHd)WryOnOTJrrJ;eWXUycwC~8E~a&TVP5D0R=G0fR?_wpyG@S5)5kP lI=2#M-(;-aE$*L*MB~f@0p6@^AX6EEuo6gL0A?cw1^|yZdNBY1 literal 0 HcmV?d00001 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"