Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZipEntry cmdlets support for reading from Stream #40

Merged
merged 15 commits into from
Jan 10, 2025
Prev Previous commit
Next Next commit
good shit
santisq committed Jan 9, 2025
commit 9b1fdfba5e57379a58829e3770aaed1c12103b2f
15 changes: 9 additions & 6 deletions src/PSCompression/Commands/GetZipEntryCommand.cs
Original file line number Diff line number Diff line change
@@ -78,7 +78,7 @@ ZipEntryBase CreateFromStream(ZipArchiveEntry entry, bool isDirectory) =>

using ZipArchive zip = new(Stream, ZipArchiveMode.Read, true);
WriteObject(
EnumerateEntries(zip, CreateFromStream),
GetEntries(zip, CreateFromStream),
enumerateCollection: true);
return;
}
@@ -102,10 +102,13 @@ ZipEntryBase CreateFromFile(ZipArchiveEntry entry, bool isDirectory) =>

try
{
using ZipArchive zip = ZipFile.OpenRead(path);
WriteObject(
EnumerateEntries(zip, CreateFromFile),
enumerateCollection: true);
IEnumerable<ZipEntryBase> entries;
using (ZipArchive zip = ZipFile.OpenRead(path))
{
entries = GetEntries(zip, CreateFromFile);
}

WriteObject(entries, enumerateCollection: true);
}
catch (Exception exception)
{
@@ -114,7 +117,7 @@ ZipEntryBase CreateFromFile(ZipArchiveEntry entry, bool isDirectory) =>
}
}

private IEnumerable<ZipEntryBase> EnumerateEntries(
private IEnumerable<ZipEntryBase> GetEntries(
ZipArchive zip,
Func<ZipArchiveEntry, bool, ZipEntryBase> createMethod)
{
12 changes: 9 additions & 3 deletions tests/ZipEntryCmdlets.tests.ps1
Original file line number Diff line number Diff line change
@@ -10,7 +10,8 @@ Describe 'ZipEntry Cmdlets' {
BeforeAll {
$zip = New-Item (Join-Path $TestDrive test.zip) -ItemType File -Force
$file = New-Item ([System.IO.Path]::Combine($TestDrive, 'someFile.txt')) -ItemType File -Value 'foo'
$zip, $file | Out-Null
$uri = 'https://www.powershellgallery.com/api/v2/package/PSCompression'
$zip, $file, $uri | Out-Null
}

Context 'New-ZipEntry' -Tag 'New-ZipEntry' {
@@ -113,8 +114,13 @@ Describe 'ZipEntry Cmdlets' {

Context 'Get-ZipEntry' -Tag 'Get-ZipEntry' {
It 'Can list entries in a zip archive' {
@($zip | Get-ZipEntry).Count -gt 0 |
Should -BeGreaterThan 0
$zip | Get-ZipEntry |
Should -BeOfType ([PSCompression.ZipEntryBase])
}

It 'Can list entries from a Stream' {
Invoke-WebRequest $uri | Get-ZipEntry |
Should -BeOfType ([PSCompression.ZipEntryBase])
}

It 'Should throw when not targetting a FileSystem Provider Path' {