Skip to content

Commit

Permalink
mistakes were made
Browse files Browse the repository at this point in the history
  • Loading branch information
santisq committed Jan 9, 2025
1 parent da7333e commit e39c33d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 249 deletions.
239 changes: 0 additions & 239 deletions Get-ZipEntry.md

This file was deleted.

16 changes: 16 additions & 0 deletions docs/en-US/Get-ZipEntry.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
29 changes: 22 additions & 7 deletions src/PSCompression/Commands/GetZipEntryCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,31 @@ protected override void BeginProcessing()

protected override void ProcessRecord()
{
IEnumerable<ZipEntryBase> entries;
if (Stream is not null)
{
ZipEntryBase CreateFromStream(ZipArchiveEntry entry, bool isDirectory) =>
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())
Expand All @@ -102,14 +115,16 @@ ZipEntryBase CreateFromFile(ZipArchiveEntry entry, bool isDirectory) =>

try
{
IEnumerable<ZipEntryBase> 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));
Expand Down
12 changes: 12 additions & 0 deletions src/PSCompression/Exceptions/ExceptionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions tests/ZipEntryCmdlets.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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' {
Expand Down

0 comments on commit e39c33d

Please sign in to comment.