Address code review: Replace dynamic with IArchiveProgressInfo interface

Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-11-30 12:07:17 +00:00
parent e2df7894f9
commit 0fdf9c74a8
3 changed files with 31 additions and 17 deletions

View File

@@ -8,7 +8,7 @@ using SharpCompress.Readers;
namespace SharpCompress.Archives;
public abstract class AbstractArchive<TEntry, TVolume> : IArchive
public abstract class AbstractArchive<TEntry, TVolume> : IArchive, IArchiveProgressInfo
where TEntry : IArchiveEntry
where TVolume : IVolume
{
@@ -41,7 +41,7 @@ public abstract class AbstractArchive<TEntry, TVolume> : IArchive
/// <summary>
/// Gets the progress reporter for this archive, if one was set via ReaderOptions.
/// </summary>
internal IProgress<ProgressReport>? Progress => ReaderOptions.Progress;
IProgress<ProgressReport>? IArchiveProgressInfo.Progress => ReaderOptions.Progress;
private static Stream CheckStreams(Stream stream)
{
@@ -93,7 +93,7 @@ public abstract class AbstractArchive<TEntry, TVolume> : IArchive
}
}
internal void EnsureEntriesLoaded()
void IArchiveProgressInfo.EnsureEntriesLoaded()
{
_lazyEntries.EnsureFullyLoaded();
_lazyVolumes.EnsureFullyLoaded();
@@ -118,7 +118,7 @@ public abstract class AbstractArchive<TEntry, TVolume> : IArchive
"ExtractAllEntries can only be used on solid archives or 7Zip archives (which require random access)."
);
}
EnsureEntriesLoaded();
((IArchiveProgressInfo)this).EnsureEntriesLoaded();
return CreateReaderForSolidExtraction();
}
@@ -136,7 +136,7 @@ public abstract class AbstractArchive<TEntry, TVolume> : IArchive
{
get
{
EnsureEntriesLoaded();
((IArchiveProgressInfo)this).EnsureEntriesLoaded();
return Entries.All(x => x.IsComplete);
}
}

View File

@@ -17,10 +17,10 @@ public static class IArchiveEntryExtensions
throw new ExtractionException("Entry is a file directory and cannot be extracted.");
}
var archive = archiveEntry.Archive as dynamic;
archive.EnsureEntriesLoaded();
var progressInfo = archiveEntry.Archive as IArchiveProgressInfo;
progressInfo?.EnsureEntriesLoaded();
IProgress<ProgressReport>? progress = GetProgress(archiveEntry.Archive);
IProgress<ProgressReport>? progress = progressInfo?.Progress;
using var entryStream = archiveEntry.OpenEntryStream();
if (progress is null)
@@ -55,10 +55,10 @@ public static class IArchiveEntryExtensions
throw new ExtractionException("Entry is a file directory and cannot be extracted.");
}
var archive = archiveEntry.Archive as dynamic;
archive.EnsureEntriesLoaded();
var progressInfo = archiveEntry.Archive as IArchiveProgressInfo;
progressInfo?.EnsureEntriesLoaded();
IProgress<ProgressReport>? progress = GetProgress(archiveEntry.Archive);
IProgress<ProgressReport>? progress = progressInfo?.Progress;
using var entryStream = archiveEntry.OpenEntryStream();
if (progress is null)
@@ -92,12 +92,6 @@ public static class IArchiveEntryExtensions
}
}
private static IProgress<ProgressReport>? GetProgress(IArchive archive)
{
// Try to get progress from the concrete archive type
return (archive as dynamic)?.Progress as IProgress<ProgressReport>;
}
private static long? GetEntrySizeSafe(IArchiveEntry entry)
{
try

View File

@@ -0,0 +1,20 @@
using System;
using SharpCompress.Common;
namespace SharpCompress.Archives;
/// <summary>
/// Internal interface for archives that support progress reporting.
/// </summary>
internal interface IArchiveProgressInfo
{
/// <summary>
/// Gets the progress reporter for this archive, if one was set.
/// </summary>
IProgress<ProgressReport>? Progress { get; }
/// <summary>
/// Ensures all entries are loaded from the archive.
/// </summary>
void EnsureEntriesLoaded();
}