From e39c33d68cec9062a409c6b794a5940dba1bfcf2 Mon Sep 17 00:00:00 2001 From: Santiago Squarzon Date: Thu, 9 Jan 2025 19:55:49 -0300 Subject: [PATCH] mistakes were made --- Get-ZipEntry.md | 239 ------------------ docs/en-US/Get-ZipEntry.md | 16 ++ .../Commands/GetZipEntryCommand.cs | 29 ++- .../Exceptions/ExceptionHelpers.cs | 12 + tests/ZipEntryCmdlets.tests.ps1 | 8 +- 5 files changed, 55 insertions(+), 249 deletions(-) delete mode 100644 Get-ZipEntry.md diff --git a/Get-ZipEntry.md b/Get-ZipEntry.md deleted file mode 100644 index 5d966b9..0000000 --- a/Get-ZipEntry.md +++ /dev/null @@ -1,239 +0,0 @@ ---- -external help file: PSCompression.dll-Help.xml -Module Name: PSCompression -online version: https://github.com/santisq/PSCompression -schema: 2.0.0 ---- - -# Get-ZipEntry - -## SYNOPSIS -Lists zip entries from one or more specified Zip Archives. - -## SYNTAX - -### Path (Default) -``` -Get-ZipEntry [-Type ] [-Include ] [-Exclude ] [-Path] - [-ProgressAction ] [] -``` - -### Stream -``` -Get-ZipEntry -Stream [-Type ] [-Include ] [-Exclude ] - [-ProgressAction ] [] -``` - -### LiteralPath -``` -Get-ZipEntry [-Type ] [-Include ] [-Exclude ] -LiteralPath - [-ProgressAction ] [] -``` - -## DESCRIPTION -The \`Get-ZipEntry\` cmdlet lists entries from specified Zip paths. -It has built-in functionalities to filter entries and is the main entry point for the \`*-ZipEntry\` cmdlets in this module. - -## EXAMPLES - -### Example 1: List entries for a specified Zip file path -``` -PS ..\pwsh> Get-ZipEntry path\to\myZip.zip -``` - -### Example 2: List entries from all Zip files in the current directory -``` -PS ..\pwsh> Get-ZipEntry *.zip -``` - -The \`-Path\` parameter supports wildcards. - -### Example 3: List all `Archive` entries from a Zip file -``` -PS ..\pwsh> Get-ZipEntry path\to\myZip.zip -Type Archive -``` - -The \`-Type\` parameter supports filtering by \`Archive\` or \`Directory\`. - -### Example 4: Filtering entries with `-Include` and `-Exclude` parameters -``` -PS ..\pwsh> Get-ZipEntry .\PSCompression.zip -Include PSCompression/docs/en-us* - - Directory: /PSCompression/docs/en-US/ - -Type LastWriteTime CompressedSize Size Name ----- ------------- -------------- ---- ---- -Directory 2/22/2024 1:19 PM 0.00 B 0.00 B en-US -Archive 2/22/2024 1:19 PM 2.08 KB 6.98 KB Compress-GzipArchive.md -Archive 2/22/2024 1:19 PM 2.74 KB 8.60 KB Compress-ZipArchive.md -Archive 2/22/2024 1:19 PM 1.08 KB 2.67 KB ConvertFrom-GzipString.md -Archive 2/22/2024 1:19 PM 1.67 KB 4.63 KB ConvertTo-GzipString.md -Archive 2/22/2024 1:19 PM 1.74 KB 6.28 KB Expand-GzipArchive.md -Archive 2/22/2024 1:19 PM 1.23 KB 4.07 KB Expand-ZipEntry.md -Archive 2/22/2024 1:19 PM 1.53 KB 6.38 KB Get-ZipEntry.md -Archive 2/22/2024 1:19 PM 1.67 KB 5.06 KB Get-ZipEntryContent.md -Archive 2/22/2024 1:19 PM 2.20 KB 7.35 KB New-ZipEntry.md -Archive 2/22/2024 1:19 PM 961.00 B 2.62 KB PSCompression.md -Archive 2/22/2024 1:19 PM 1.14 KB 2.95 KB Remove-ZipEntry.md -Archive 2/22/2024 1:19 PM 741.00 B 2.16 KB Rename-ZipEntry.md -Archive 2/22/2024 1:19 PM 1.55 KB 5.35 KB Set-ZipEntryContent.md - -PS ..\pwsh> Get-ZipEntry .\PSCompression.zip -Include PSCompression/docs/en-us* -Exclude *en-US/Compress*, *en-US/Remove* - - Directory: /PSCompression/docs/en-US/ - -Type LastWriteTime CompressedSize Size Name ----- ------------- -------------- ---- ---- -Directory 2/22/2024 1:19 PM 0.00 B 0.00 B en-US -Archive 2/22/2024 1:19 PM 1.08 KB 2.67 KB ConvertFrom-GzipString.md -Archive 2/22/2024 1:19 PM 1.67 KB 4.63 KB ConvertTo-GzipString.md -Archive 2/22/2024 1:19 PM 1.74 KB 6.28 KB Expand-GzipArchive.md -Archive 2/22/2024 1:19 PM 1.23 KB 4.07 KB Expand-ZipEntry.md -Archive 2/22/2024 1:19 PM 1.53 KB 6.38 KB Get-ZipEntry.md -Archive 2/22/2024 1:19 PM 1.67 KB 5.06 KB Get-ZipEntryContent.md -Archive 2/22/2024 1:19 PM 2.20 KB 7.35 KB New-ZipEntry.md -Archive 2/22/2024 1:19 PM 961.00 B 2.62 KB PSCompression.md -Archive 2/22/2024 1:19 PM 741.00 B 2.16 KB Rename-ZipEntry.md -Archive 2/22/2024 1:19 PM 1.55 KB 5.35 KB Set-ZipEntryContent.md -``` - -\> \[!NOTE\] \> \> - Inclusion and Exclusion patterns are applied to the entries relative path. -\> - Exclusions are applied after the inclusions. - -## PARAMETERS - -### -Type -Lists entries of a specified type, \`Archive\` or \`Directory\`. - -```yaml -Type: ZipEntryType -Parameter Sets: (All) -Aliases: -Accepted values: Directory, Archive - -Required: False -Position: Named -Default value: None -Accept pipeline input: False -Accept wildcard characters: False -``` - -### -Exclude -Specifies an array of one or more string patterns to be matched as the cmdlet lists entries. -Any matching item is excluded from the output. -Wildcard characters are accepted. - -\> \[!NOTE\] \> Inclusion and Exclusion patterns are applied to the entries relative path. -Exclusions are applied after the inclusions. - -```yaml -Type: String[] -Parameter Sets: (All) -Aliases: - -Required: False -Position: Named -Default value: None -Accept pipeline input: False -Accept wildcard characters: True -``` - -### -Include -Specifies an array of one or more string patterns to be matched as the cmdlet lists entries. -Any matching item is included in the output. -Wildcard characters are accepted. - -\> \[!NOTE\] \> Inclusion and Exclusion patterns are applied to the entries relative path. -Exclusions are applied after the inclusions. - -```yaml -Type: String[] -Parameter Sets: (All) -Aliases: - -Required: False -Position: Named -Default value: None -Accept pipeline input: False -Accept wildcard characters: True -``` - -### -LiteralPath -Specifies a path to one or more Zip compressed files. -Note that the value is used exactly as it's typed. -No characters are interpreted as wildcards. - -```yaml -Type: String[] -Parameter Sets: LiteralPath -Aliases: PSPath - -Required: True -Position: Named -Default value: None -Accept pipeline input: True (ByPropertyName) -Accept wildcard characters: False -``` - -### -Path -Specifies a path to one or more Zip compressed files. -Wildcards are accepted. - -```yaml -Type: String[] -Parameter Sets: Path -Aliases: - -Required: True -Position: 0 -Default value: None -Accept pipeline input: True (ByValue) -Accept wildcard characters: True -``` - -### -ProgressAction -{{ Fill ProgressAction Description }} - -```yaml -Type: ActionPreference -Parameter Sets: (All) -Aliases: proga - -Required: False -Position: Named -Default value: None -Accept pipeline input: False -Accept wildcard characters: False -``` - -### -Stream -{{ Fill Stream Description }} - -```yaml -Type: Stream -Parameter Sets: Stream -Aliases: RawContentStream - -Required: True -Position: Named -Default value: None -Accept pipeline input: True (ByPropertyName, ByValue) -Accept wildcard characters: False -``` - -### CommonParameters -This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). - -## INPUTS - -### String -You can pipe paths to this cmdlet. -Output from \`Get-ChildItem\` or \`Get-Item\` can be piped to this cmdlet. - -## OUTPUTS - -### ZipEntryDirectory -### ZipEntryFile -## NOTES - -## RELATED LINKS diff --git a/docs/en-US/Get-ZipEntry.md b/docs/en-US/Get-ZipEntry.md index 7011e2d..e8144dc 100644 --- a/docs/en-US/Get-ZipEntry.md +++ b/docs/en-US/Get-ZipEntry.md @@ -256,6 +256,22 @@ Accept pipeline input: True (ByValue) Accept wildcard characters: True ``` +### -Stream + +{{ Fill Stream Description }} + +```yaml +Type: Stream +Parameter Sets: Stream +Aliases: RawContentStream + +Required: True +Position: 0 +Default value: None +Accept pipeline input: True (ByPropertyName, ByValue) +Accept wildcard characters: False +``` + ### CommonParameters This cmdlet supports the common parameters. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). diff --git a/src/PSCompression/Commands/GetZipEntryCommand.cs b/src/PSCompression/Commands/GetZipEntryCommand.cs index ef0812a..13c3028 100644 --- a/src/PSCompression/Commands/GetZipEntryCommand.cs +++ b/src/PSCompression/Commands/GetZipEntryCommand.cs @@ -69,6 +69,7 @@ protected override void BeginProcessing() protected override void ProcessRecord() { + IEnumerable entries; if (Stream is not null) { ZipEntryBase CreateFromStream(ZipArchiveEntry entry, bool isDirectory) => @@ -76,11 +77,23 @@ ZipEntryBase CreateFromStream(ZipArchiveEntry entry, bool isDirectory) => ? new ZipEntryDirectory(entry, Stream) : new ZipEntryFile(entry, Stream); - using ZipArchive zip = new(Stream, ZipArchiveMode.Read, true); - WriteObject( - GetEntries(zip, CreateFromStream), - enumerateCollection: true); - return; + try + { + using (ZipArchive zip = new(Stream, ZipArchiveMode.Read, true)) + { + entries = GetEntries(zip, CreateFromStream); + } + WriteObject(entries, enumerateCollection: true); + return; + } + catch (InvalidDataException exception) + { + ThrowTerminatingError(exception.ToInvalidZipArchive()); + } + catch (Exception exception) + { + WriteError(exception.ToOpenError("InputStream")); + } } foreach (string path in EnumerateResolvedPaths()) @@ -102,14 +115,16 @@ ZipEntryBase CreateFromFile(ZipArchiveEntry entry, bool isDirectory) => try { - IEnumerable entries; using (ZipArchive zip = ZipFile.OpenRead(path)) { entries = GetEntries(zip, CreateFromFile); } - WriteObject(entries, enumerateCollection: true); } + catch (InvalidDataException exception) + { + ThrowTerminatingError(exception.ToInvalidZipArchive()); + } catch (Exception exception) { WriteError(exception.ToOpenError(path)); diff --git a/src/PSCompression/Exceptions/ExceptionHelpers.cs b/src/PSCompression/Exceptions/ExceptionHelpers.cs index afdcb53..4e4a57a 100644 --- a/src/PSCompression/Exceptions/ExceptionHelpers.cs +++ b/src/PSCompression/Exceptions/ExceptionHelpers.cs @@ -62,6 +62,18 @@ internal static ErrorRecord ToEntryNotFoundError(this EntryNotFoundException exc internal static ErrorRecord ToEnumerationError(this Exception exception, object item) => new(exception, "EnumerationError", ErrorCategory.ReadError, item); + internal static ErrorRecord ToInvalidZipArchive(this InvalidDataException exception) => + new( + new InvalidDataException( + "Specified path or stream is not a valid zip archive, " + + "might be compressed using an unsupported method, " + + "or could be corrupted.", + exception), + "InvalidZipArchive", + ErrorCategory.InvalidData, + null); + + internal static void ThrowIfNotFound( this ZipArchive zip, string path, diff --git a/tests/ZipEntryCmdlets.tests.ps1 b/tests/ZipEntryCmdlets.tests.ps1 index 48b3e78..370a2a7 100644 --- a/tests/ZipEntryCmdlets.tests.ps1 +++ b/tests/ZipEntryCmdlets.tests.ps1 @@ -129,16 +129,18 @@ Describe 'ZipEntry Cmdlets' { } It 'Should throw when not targetting a FileSystem Provider Path' { - { Get-ZipEntry function:\* } | Should -Throw + { Get-ZipEntry function:\* } | + Should -Throw -ExceptionType ([System.NotSupportedException]) } It 'Should throw when the path is not a Zip' { { Get-ZipEntry $file.FullName } | - Should -Throw -ExceptionType ([System.ArgumentException]) + Should -Throw -ExceptionType ([System.IO.InvalidDataException]) } It 'Should throw if the path is not a file' { - { Get-ZipEntry $pwd.FullName } | Should -Throw + { Get-ZipEntry $pwd.FullName } | + Should -Throw -ExceptionType ([System.ArgumentException]) } It 'Can list zip file entries' {