Skip to content

Commit bb05026

Browse files
author
Andrew
committed
Merge branch 'KirkMunro-issue_6'
2 parents 4f559b7 + ad5ae76 commit bb05026

File tree

2 files changed

+76
-42
lines changed

2 files changed

+76
-42
lines changed

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
InvalidDestinationPath=The destination path '{0}' does not contain a valid archive file name.
2222
PreparingToCompressVerboseMessage=Preparing to compress...
2323
PreparingToExpandVerboseMessage=Preparing to expand...
24+
ItemDoesNotAppearToBeAValidZipArchive=File '{0}' does not appear to be a valid zip archive.
2425
'@
2526
}
2627

@@ -100,7 +101,7 @@ function Compress-Archive
100101
$destinationParentDir = '.'
101102
}
102103

103-
$achiveFileName = [system.IO.Path]::GetFileName($DestinationPath)
104+
$archiveFileName = [system.IO.Path]::GetFileName($DestinationPath)
104105
$destinationParentDir = GetResolvedPathHelper $destinationParentDir $false $PSCmdlet
105106

106107
if($destinationParentDir.Count -gt 1)
@@ -110,28 +111,19 @@ function Compress-Archive
110111
}
111112

112113
IsValidFileSystemPath $destinationParentDir | Out-Null
113-
$DestinationPath = Join-Path -Path $destinationParentDir -ChildPath $achiveFileName
114+
$DestinationPath = Join-Path -Path $destinationParentDir -ChildPath $archiveFileName
114115

115116
# GetExtension API does not validate for the actual existance of the path.
116117
$extension = [system.IO.Path]::GetExtension($DestinationPath)
117118

118-
# If user does not specify .Zip extension, we append it.
119+
# If user does not specify an extension, we append the .zip extension automatically.
119120
If($extension -eq [string]::Empty)
120121
{
121122
$DestinationPathWithOutExtension = $DestinationPath
122123
$DestinationPath = $DestinationPathWithOutExtension + $zipFileExtension
123124
$appendArchiveFileExtensionMessage = ($LocalizedData.AppendArchiveFileExtensionMessage -f $DestinationPathWithOutExtension, $DestinationPath)
124125
Write-Verbose $appendArchiveFileExtensionMessage
125126
}
126-
else
127-
{
128-
# Invalid file extension is specified for the zip file to be created.
129-
if($extension -ne $zipFileExtension)
130-
{
131-
$errorMessage = ($LocalizedData.InvalidZipFileExtensionError -f $extension, $zipFileExtension)
132-
ThrowTerminatingErrorHelper "NotSupportedArchiveFileExtension" $errorMessage ([System.Management.Automation.ErrorCategory]::InvalidArgument) $extension
133-
}
134-
}
135127

136128
$archiveFileExist = Test-Path -LiteralPath $DestinationPath -PathType Leaf
137129

@@ -910,18 +902,7 @@ function ValidateArchivePathHelper
910902
[string] $archiveFile
911903
)
912904

913-
if([System.IO.File]::Exists($archiveFile))
914-
{
915-
$extension = [system.IO.Path]::GetExtension($archiveFile)
916-
917-
# Invalid file extension is specifed for the zip file.
918-
if($extension -ne $zipFileExtension)
919-
{
920-
$errorMessage = ($LocalizedData.InvalidZipFileExtensionError -f $extension, $zipFileExtension)
921-
ThrowTerminatingErrorHelper "NotSupportedArchiveFileExtension" $errorMessage ([System.Management.Automation.ErrorCategory]::InvalidArgument) $extension
922-
}
923-
}
924-
else
905+
if(-not [System.IO.File]::Exists($archiveFile))
925906
{
926907
$errorMessage = ($LocalizedData.PathNotFoundError -f $archiveFile)
927908
ThrowTerminatingErrorHelper "PathNotFound" $errorMessage ([System.Management.Automation.ErrorCategory]::InvalidArgument) $archiveFile
@@ -954,7 +935,28 @@ function ExpandArchiveHelper
954935
$archiveFileStream = New-Object -TypeName System.IO.FileStream -ArgumentList $archiveFileStreamArgs
955936

956937
$zipArchiveArgs = @($archiveFileStream, [System.IO.Compression.ZipArchiveMode]::Read, $false)
957-
$zipArchive = New-Object -TypeName System.IO.Compression.ZipArchive -ArgumentList $zipArchiveArgs
938+
try
939+
{
940+
$zipArchive = New-Object -TypeName System.IO.Compression.ZipArchive -ArgumentList $zipArchiveArgs
941+
}
942+
catch [System.IO.InvalidDataException]
943+
{
944+
# Failed to open the file for reading as a zip archive. Wrap the exception
945+
# and re-throw it indicating it does not appear to be a valid zip file.
946+
$exception = $_.Exception
947+
if($null -ne $_.Exception -and
948+
$null -ne $_.Exception.InnerException)
949+
{
950+
$exception = $_.Exception.InnerException
951+
}
952+
# Load the WindowsBase.dll assembly to get access to the System.IO.FileFormatException class
953+
[System.Reflection.Assembly]::Load('WindowsBase,Version=4.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35')
954+
$invalidFileFormatException = New-Object -TypeName System.IO.FileFormatException -ArgumentList @(
955+
($LocalizedData.ItemDoesNotAppearToBeAValidZipArchive -f $archiveFile)
956+
$exception
957+
)
958+
throw $invalidFileFormatException
959+
}
958960

959961
if($zipArchive.Entries.Count -eq 0)
960962
{

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

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,16 @@ Describe "Test suite for Microsoft.PowerShell.Archive module" -Tags "BVT" {
261261
$path = @("$TestDrive", "HKLM:\SOFTWARE")
262262
CompressArchiveInValidPathValidator $path $TestDrive "HKLM:\SOFTWARE" "PathNotFound,Compress-Archive"
263263

264-
$invalidUnZipFileFormat = "$TestDrive\Sample.unzip"
265-
CompressArchiveInValidArchiveFileExtensionValidator $TestDrive "$invalidUnZipFileFormat" ".unzip"
264+
# The tests below are no longer valid. You can have zip files with non-zip extensions. Different archive
265+
# formats should be added in a separate pull request, with a parameter to identify the archive format, and
266+
# default formats associated with specific extensions. Until then, as long as these cmdlets only support
267+
# Zip files, any file extension is supported.
268+
269+
#$invalidUnZipFileFormat = "$TestDrive\Sample.unzip"
270+
#CompressArchiveInValidArchiveFileExtensionValidator $TestDrive "$invalidUnZipFileFormat" ".unzip"
266271

267-
$invalidcabZipFileFormat = "$TestDrive\Sample.cab"
268-
CompressArchiveInValidArchiveFileExtensionValidator $TestDrive "$invalidcabZipFileFormat" ".cab"
272+
#$invalidcabZipFileFormat = "$TestDrive\Sample.cab"
273+
#CompressArchiveInValidArchiveFileExtensionValidator $TestDrive "$invalidcabZipFileFormat" ".cab"
269274
}
270275

271276
It "Validate error from Compress-Archive when archive file already exists and -Update parameter is not specified" {
@@ -642,6 +647,13 @@ Describe "Test suite for Microsoft.PowerShell.Archive module" -Tags "BVT" {
642647
$archive = Compress-Archive -Path $sourcePath -DestinationPath $destinationPath -PassThru
643648
$archive.FullName | Should Be $destinationPath
644649
}
650+
651+
It "Validate that Compress-Archive can create a zip archive that has a different extension" {
652+
$sourcePath = "$TestDrive\SourceDir\ChildDir-1\Sample-3.txt"
653+
$destinationPath = "$TestDrive\DifferentZipExtension.dat"
654+
Compress-Archive -Path $sourcePath -DestinationPath $destinationPath
655+
$destinationPath | Should Exist
656+
}
645657
}
646658

647659
Context "Expand-Archive - Parameter validation test cases" {
@@ -854,19 +866,23 @@ Describe "Test suite for Microsoft.PowerShell.Archive module" -Tags "BVT" {
854866
}
855867
}
856868

857-
It "Invoke Expand-Archive with unsupported archive format" {
858-
$sourcePath = "$TestDrive\Sample.cab"
859-
$destinationPath = "$TestDrive\UnsupportedArchiveFormatDir"
860-
try
861-
{
862-
Expand-Archive -Path $sourcePath -DestinationPath $destinationPath -Force
863-
throw "Failed to detect unsupported archive format at $sourcePath"
864-
}
865-
catch
866-
{
867-
$_.FullyQualifiedErrorId | Should Be "NotSupportedArchiveFileExtension,Expand-Archive"
868-
}
869-
}
869+
# The test below is no longer valid. You can have zip files with non-zip extensions. Different archive
870+
# formats should be added in a separate pull request, with a parameter to identify the archive format, and
871+
# default formats associated with specific extensions. Until then, as long as these cmdlets only support
872+
# Zip files, any file extension is supported.
873+
#It "Invoke Expand-Archive with unsupported archive format" {
874+
# $sourcePath = "$TestDrive\Sample.cab"
875+
# $destinationPath = "$TestDrive\UnsupportedArchiveFormatDir"
876+
# try
877+
# {
878+
# Expand-Archive -Path $sourcePath -DestinationPath $destinationPath -Force
879+
# throw "Failed to detect unsupported archive format at $sourcePath"
880+
# }
881+
# catch
882+
# {
883+
# $_.FullyQualifiedErrorId | Should Be "NotSupportedArchiveFileExtension,Expand-Archive"
884+
# }
885+
#}
870886

871887
It "Invoke Expand-Archive with archive file containing multiple files, directories with subdirectories and empty directories" {
872888
$sourcePath = "$TestDrive\SourceDir"
@@ -986,6 +1002,7 @@ Describe "Test suite for Microsoft.PowerShell.Archive module" -Tags "BVT" {
9861002
$sourcePath = "$TestDrive\SourceDir"
9871003
$archivePath = "$TestDrive\NoPassThruTestForExpand.zip"
9881004
$destinationPath = "$TestDrive\NoPassThruTest"
1005+
9891006
$sourceList = dir $sourcePath -Name
9901007

9911008
Add-CompressionAssemblies
@@ -1026,5 +1043,20 @@ Describe "Test suite for Microsoft.PowerShell.Archive module" -Tags "BVT" {
10261043

10271044
Compare-Object -ReferenceObject $extractedList -DifferenceObject $contents -PassThru | Should Be $null
10281045
}
1046+
1047+
It "Validate Expand-Archive works with zip files that have non-zip file extensions" {
1048+
$sourcePath = "$TestDrive\SourceDir"
1049+
$archivePath = "$TestDrive\NonZipFileExtension.dat"
1050+
$destinationPath = "$TestDrive\NonZipFileExtension"
1051+
$sourceList = dir $sourcePath -Name
1052+
1053+
Add-CompressionAssemblies
1054+
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcePath, $archivePath)
1055+
1056+
Expand-Archive -Path $archivePath -DestinationPath $destinationPath
1057+
$extractedList = dir $destinationPath -Name
1058+
1059+
Compare-Object -ReferenceObject $extractedList -DifferenceObject $sourceList -PassThru | Should Be $null
1060+
}
10291061
}
10301062
}

0 commit comments

Comments
 (0)