From eb738b44a8467179dab75bb433a7b8d910f6472a Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Wed, 4 Feb 2026 14:32:50 +0000 Subject: [PATCH] clean up naming --- .../Common/Zip/StreamingZipFilePart.cs | 8 +-- .../Zip/StreamingZipHeaderFactory.Async.cs | 20 +++--- .../Common/Zip/StreamingZipHeaderFactory.cs | 14 ++--- src/SharpCompress/Factories/GZipFactory.cs | 18 +++--- .../Factories/SevenZipFactory.cs | 2 +- src/SharpCompress/Factories/TarFactory.cs | 62 +++++++++---------- src/SharpCompress/IO/IStreamStack.cs | 6 +- .../Readers/ReaderFactory.Async.cs | 37 +++++++---- src/SharpCompress/Readers/ReaderFactory.cs | 15 +++-- src/SharpCompress/Readers/Tar/TarReader.cs | 54 ++++++++-------- 10 files changed, 127 insertions(+), 109 deletions(-) diff --git a/src/SharpCompress/Common/Zip/StreamingZipFilePart.cs b/src/SharpCompress/Common/Zip/StreamingZipFilePart.cs index 4147ef1b..28414bfa 100644 --- a/src/SharpCompress/Common/Zip/StreamingZipFilePart.cs +++ b/src/SharpCompress/Common/Zip/StreamingZipFilePart.cs @@ -31,11 +31,11 @@ internal sealed partial class StreamingZipFilePart : ZipFilePart return _decompressionStream; } - internal BinaryReader FixStreamedFileLocation(ref Stream rewindableStream) + internal BinaryReader FixStreamedFileLocation(ref Stream stream) { if (Header.IsDirectory) { - return new BinaryReader(rewindableStream); + return new BinaryReader(stream); } if (Header.HasData && !Skipped) @@ -49,12 +49,12 @@ internal sealed partial class StreamingZipFilePart : ZipFilePart if (_decompressionStream is DeflateStream deflateStream) { - rewindableStream.Position = 0; + stream.Position = 0; } Skipped = true; } - var reader = new BinaryReader(rewindableStream); + var reader = new BinaryReader(stream); _decompressionStream = null; return reader; } diff --git a/src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.Async.cs b/src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.Async.cs index c631be91..3ddaaa4b 100644 --- a/src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.Async.cs +++ b/src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.Async.cs @@ -60,7 +60,7 @@ internal sealed partial class StreamingZipHeaderFactory private sealed class StreamHeaderAsyncEnumerator : IAsyncEnumerator, IDisposable { private readonly StreamingZipHeaderFactory _headerFactory; - private readonly SharpCompressStream _rewindableStream; + private readonly SharpCompressStream _sharpCompressStream; private readonly AsyncBinaryReader _reader; private readonly CancellationToken _cancellationToken; private bool _completed; @@ -74,8 +74,8 @@ internal sealed partial class StreamingZipHeaderFactory _headerFactory = headerFactory; // Use EnsureSeekable to avoid double-wrapping if stream is already a SharpCompressStream, // and to preserve seekability for DataDescriptorStream which needs to seek backward - _rewindableStream = SharpCompressStream.EnsureSeekable(stream); - _reader = new AsyncBinaryReader(_rewindableStream, leaveOpen: true); + _sharpCompressStream = SharpCompressStream.EnsureSeekable(stream); + _reader = new AsyncBinaryReader(_sharpCompressStream, leaveOpen: true); _cancellationToken = cancellationToken; } @@ -110,7 +110,9 @@ internal sealed partial class StreamingZipHeaderFactory continue; } - var pos = _rewindableStream.CanSeek ? (long?)_rewindableStream.Position : null; + var pos = _sharpCompressStream.CanSeek + ? (long?)_sharpCompressStream.Position + : null; var crc = await _reader .ReadUInt32Async(_cancellationToken) @@ -178,7 +180,9 @@ internal sealed partial class StreamingZipHeaderFactory continue; } - var pos = _rewindableStream.CanSeek ? (long?)_rewindableStream.Position : null; + var pos = _sharpCompressStream.CanSeek + ? (long?)_sharpCompressStream.Position + : null; headerBytes = await _reader .ReadUInt32Async(_cancellationToken) @@ -239,9 +243,9 @@ internal sealed partial class StreamingZipHeaderFactory // For SeekableSharpCompressStream, seek back to just after the local header signature. // Plain SharpCompressStream cannot seek to arbitrary positions, so we skip this. // 4 = First 4 bytes of the entry header (i.e. 50 4B 03 04) - if (_rewindableStream is SeekableSharpCompressStream) + if (_sharpCompressStream is SeekableSharpCompressStream) { - _rewindableStream.Position = pos.Value + 4; + _sharpCompressStream.Position = pos.Value + 4; } } } @@ -293,7 +297,7 @@ internal sealed partial class StreamingZipHeaderFactory var nextHeaderBytes = await _reader .ReadUInt32Async(_cancellationToken) .ConfigureAwait(false); - ((IStreamStack)_rewindableStream).Rewind(sizeof(uint)); + ((IStreamStack)_sharpCompressStream).Rewind(sizeof(uint)); // Check if next data is PostDataDescriptor, streamed file with 0 length header.HasData = !IsHeader(nextHeaderBytes); diff --git a/src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.cs b/src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.cs index 74d95608..4ec08864 100644 --- a/src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.cs +++ b/src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.cs @@ -22,10 +22,10 @@ internal partial class StreamingZipHeaderFactory : ZipHeaderFactory { // Use EnsureSeekable to avoid double-wrapping if stream is already a SharpCompressStream, // and to preserve seekability for DataDescriptorStream which needs to seek backward - var rewindableStream = SharpCompressStream.EnsureSeekable(stream); + var sharpCompressStream = SharpCompressStream.EnsureSeekable(stream); while (true) { - var reader = new BinaryReader(rewindableStream); + var reader = new BinaryReader(sharpCompressStream); uint headerBytes = 0; if ( _lastEntryHeader != null @@ -39,7 +39,7 @@ internal partial class StreamingZipHeaderFactory : ZipHeaderFactory // removed requirement for FixStreamedFileLocation() - var pos = rewindableStream.CanSeek ? (long?)rewindableStream.Position : null; + var pos = sharpCompressStream.CanSeek ? (long?)sharpCompressStream.Position : null; var crc = reader.ReadUInt32(); if (crc == POST_DATA_DESCRIPTOR) @@ -87,10 +87,10 @@ internal partial class StreamingZipHeaderFactory : ZipHeaderFactory } //reader = ((StreamingZipFilePart)_lastEntryHeader.Part).FixStreamedFileLocation( - // ref rewindableStream + // ref sharpCompressStream //); - var pos = rewindableStream.CanSeek ? (long?)rewindableStream.Position : null; + var pos = sharpCompressStream.CanSeek ? (long?)sharpCompressStream.Position : null; headerBytes = reader.ReadUInt32(); @@ -132,7 +132,7 @@ internal partial class StreamingZipHeaderFactory : ZipHeaderFactory _lastEntryHeader.DataStartPosition = pos - _lastEntryHeader.CompressedSize; // 4 = First 4 bytes of the entry header (i.e. 50 4B 03 04) - rewindableStream.Position = pos.Value + 4; + sharpCompressStream.Position = pos.Value + 4; } } else @@ -176,7 +176,7 @@ internal partial class StreamingZipHeaderFactory : ZipHeaderFactory // Peek ahead to check if next data is a header or file data. // Use the IStreamStack.Rewind mechanism to give back the peeked bytes. var nextHeaderBytes = reader.ReadUInt32(); - ((IStreamStack)rewindableStream).Rewind(sizeof(uint)); + ((IStreamStack)sharpCompressStream).Rewind(sizeof(uint)); // Check if next data is PostDataDescriptor, streamed file with 0 length header.HasData = !IsHeader(nextHeaderBytes); diff --git a/src/SharpCompress/Factories/GZipFactory.cs b/src/SharpCompress/Factories/GZipFactory.cs index 261d2938..ae4ac8b8 100644 --- a/src/SharpCompress/Factories/GZipFactory.cs +++ b/src/SharpCompress/Factories/GZipFactory.cs @@ -107,28 +107,28 @@ public class GZipFactory /// internal override bool TryOpenReader( - SharpCompressStream rewindableStream, + SharpCompressStream sharpCompressStream, ReaderOptions options, out IReader? reader ) { reader = null; - if (GZipArchive.IsGZipFile(rewindableStream)) + if (GZipArchive.IsGZipFile(sharpCompressStream)) { - rewindableStream.Rewind(); - var testStream = new GZipStream(rewindableStream, CompressionMode.Decompress); + sharpCompressStream.Rewind(); + var testStream = new GZipStream(sharpCompressStream, CompressionMode.Decompress); if (TarArchive.IsTarFile(testStream)) { - rewindableStream.StopRecording(); - reader = new TarReader(rewindableStream, options, CompressionType.GZip); + sharpCompressStream.StopRecording(); + reader = new TarReader(sharpCompressStream, options, CompressionType.GZip); return true; } - rewindableStream.StopRecording(); - reader = OpenReader(rewindableStream, options); + sharpCompressStream.StopRecording(); + reader = OpenReader(sharpCompressStream, options); return true; } - rewindableStream.Rewind(); + sharpCompressStream.Rewind(); return false; } diff --git a/src/SharpCompress/Factories/SevenZipFactory.cs b/src/SharpCompress/Factories/SevenZipFactory.cs index a371cce1..f8f4b779 100644 --- a/src/SharpCompress/Factories/SevenZipFactory.cs +++ b/src/SharpCompress/Factories/SevenZipFactory.cs @@ -94,7 +94,7 @@ public class SevenZipFactory : Factory, IArchiveFactory, IMultiArchiveFactory #region reader internal override bool TryOpenReader( - SharpCompressStream rewindableStream, + SharpCompressStream sharpCompressStream, ReaderOptions options, out IReader? reader ) diff --git a/src/SharpCompress/Factories/TarFactory.cs b/src/SharpCompress/Factories/TarFactory.cs index bfba59b9..df091f59 100644 --- a/src/SharpCompress/Factories/TarFactory.cs +++ b/src/SharpCompress/Factories/TarFactory.cs @@ -49,18 +49,18 @@ public class TarFactory /// public override bool IsArchive(Stream stream, string? password = null) { - var rewindableStream = new SharpCompressStream(stream); - rewindableStream.StartRecording(); + var sharpCompressStream = new SharpCompressStream(stream); + sharpCompressStream.StartRecording(); foreach (var wrapper in TarWrapper.Wrappers) { - rewindableStream.Rewind(); - if (wrapper.IsMatch(rewindableStream)) + sharpCompressStream.Rewind(); + if (wrapper.IsMatch(sharpCompressStream)) { - rewindableStream.Rewind(); - var decompressedStream = wrapper.CreateStream(rewindableStream); + sharpCompressStream.Rewind(); + var decompressedStream = wrapper.CreateStream(sharpCompressStream); if (TarArchive.IsTarFile(decompressedStream)) { - rewindableStream.Rewind(); + sharpCompressStream.Rewind(); return true; } } @@ -76,21 +76,21 @@ public class TarFactory CancellationToken cancellationToken = default ) { - var rewindableStream = new SharpCompressStream(stream); - rewindableStream.StartRecording(); + var sharpCompressStream = new SharpCompressStream(stream); + sharpCompressStream.StartRecording(); foreach (var wrapper in TarWrapper.Wrappers) { - rewindableStream.Rewind(); - if (await wrapper.IsMatchAsync(rewindableStream, cancellationToken)) + sharpCompressStream.Rewind(); + if (await wrapper.IsMatchAsync(sharpCompressStream, cancellationToken)) { - rewindableStream.Rewind(); + sharpCompressStream.Rewind(); var decompressedStream = await wrapper.CreateStreamAsync( - rewindableStream, + sharpCompressStream, cancellationToken ); if (await TarArchive.IsTarFileAsync(decompressedStream, cancellationToken)) { - rewindableStream.Rewind(); + sharpCompressStream.Rewind(); return true; } } @@ -160,19 +160,19 @@ public class TarFactory public IReader OpenReader(Stream stream, ReaderOptions? options) { options ??= new ReaderOptions(); - var rewindableStream = new SharpCompressStream(stream); - rewindableStream.StartRecording(); + var sharpCompressStream = new SharpCompressStream(stream); + sharpCompressStream.StartRecording(); foreach (var wrapper in TarWrapper.Wrappers) { - rewindableStream.Rewind(); - if (wrapper.IsMatch(rewindableStream)) + sharpCompressStream.Rewind(); + if (wrapper.IsMatch(sharpCompressStream)) { - rewindableStream.Rewind(); - var decompressedStream = wrapper.CreateStream(rewindableStream); + sharpCompressStream.Rewind(); + var decompressedStream = wrapper.CreateStream(sharpCompressStream); if (TarArchive.IsTarFile(decompressedStream)) { - rewindableStream.StopRecording(); - return new TarReader(rewindableStream, options, wrapper.CompressionType); + sharpCompressStream.StopRecording(); + return new TarReader(sharpCompressStream, options, wrapper.CompressionType); } } } @@ -188,20 +188,20 @@ public class TarFactory { cancellationToken.ThrowIfCancellationRequested(); options ??= new ReaderOptions(); - var rewindableStream = new SharpCompressStream(stream); - rewindableStream.StartRecording(); + var sharpCompressStream = new SharpCompressStream(stream); + sharpCompressStream.StartRecording(); foreach (var wrapper in TarWrapper.Wrappers) { - rewindableStream.Rewind(); - if (await wrapper.IsMatchAsync(rewindableStream, cancellationToken)) + sharpCompressStream.Rewind(); + if (await wrapper.IsMatchAsync(sharpCompressStream, cancellationToken)) { - rewindableStream.Rewind(); - var decompressedStream = wrapper.CreateStream(rewindableStream); + sharpCompressStream.Rewind(); + var decompressedStream = wrapper.CreateStream(sharpCompressStream); if (await TarArchive.IsTarFileAsync(decompressedStream, cancellationToken)) { - rewindableStream.Rewind(); - rewindableStream.StopRecording(); - return new TarReader(rewindableStream, options, wrapper.CompressionType); + sharpCompressStream.Rewind(); + sharpCompressStream.StopRecording(); + return new TarReader(sharpCompressStream, options, wrapper.CompressionType); } } } diff --git a/src/SharpCompress/IO/IStreamStack.cs b/src/SharpCompress/IO/IStreamStack.cs index 5ecd056a..d156ab75 100644 --- a/src/SharpCompress/IO/IStreamStack.cs +++ b/src/SharpCompress/IO/IStreamStack.cs @@ -56,17 +56,17 @@ public static class StreamStackExtensions while (current != null) { - if (current is SharpCompressStream rewindableStream) + if (current is SharpCompressStream sharpCompressStream) { // Try to rewind within the buffer. If the position is outside the buffered // region, silently ignore (matching release behavior where streams without // buffering simply didn't rewind). - var targetPosition = rewindableStream.Position - count; + var targetPosition = sharpCompressStream.Position - count; if (targetPosition >= 0) { try { - rewindableStream.Position = targetPosition; + sharpCompressStream.Position = targetPosition; } catch (NotSupportedException) { diff --git a/src/SharpCompress/Readers/ReaderFactory.Async.cs b/src/SharpCompress/Readers/ReaderFactory.Async.cs index 9e46328c..53788d86 100644 --- a/src/SharpCompress/Readers/ReaderFactory.Async.cs +++ b/src/SharpCompress/Readers/ReaderFactory.Async.cs @@ -54,8 +54,8 @@ public static partial class ReaderFactory stream.NotNull(nameof(stream)); options ??= new ReaderOptions() { LeaveStreamOpen = false }; - var bStream = new SharpCompressStream(stream); - bStream.StartRecording(); + var sharpCompressStream = new SharpCompressStream(stream); + sharpCompressStream.StartRecording(); var factories = Factory.Factories.OfType(); @@ -68,20 +68,24 @@ public static partial class ReaderFactory ); if (testedFactory is IReaderFactory readerFactory) { - bStream.Rewind(); + sharpCompressStream.Rewind(); if ( await testedFactory.IsArchiveAsync( - bStream, + sharpCompressStream, cancellationToken: cancellationToken ) ) { - bStream.Rewind(); - bStream.StopRecording(); - return await readerFactory.OpenAsyncReader(bStream, options, cancellationToken); + sharpCompressStream.Rewind(); + sharpCompressStream.StopRecording(); + return await readerFactory.OpenAsyncReader( + sharpCompressStream, + options, + cancellationToken + ); } } - bStream.Rewind(); + sharpCompressStream.Rewind(); } foreach (var factory in factories) @@ -90,15 +94,22 @@ public static partial class ReaderFactory { continue; // Already tested above } - bStream.Rewind(); + sharpCompressStream.Rewind(); if ( factory is IReaderFactory readerFactory - && await factory.IsArchiveAsync(bStream, cancellationToken: cancellationToken) + && await factory.IsArchiveAsync( + sharpCompressStream, + cancellationToken: cancellationToken + ) ) { - bStream.Rewind(); - bStream.StopRecording(); - return await readerFactory.OpenAsyncReader(bStream, options, cancellationToken); + sharpCompressStream.Rewind(); + sharpCompressStream.StopRecording(); + return await readerFactory.OpenAsyncReader( + sharpCompressStream, + options, + cancellationToken + ); } } diff --git a/src/SharpCompress/Readers/ReaderFactory.cs b/src/SharpCompress/Readers/ReaderFactory.cs index d0d93768..d6b94951 100644 --- a/src/SharpCompress/Readers/ReaderFactory.cs +++ b/src/SharpCompress/Readers/ReaderFactory.cs @@ -34,8 +34,8 @@ public static partial class ReaderFactory stream.NotNull(nameof(stream)); options ??= new ReaderOptions() { LeaveStreamOpen = false }; - var bStream = SharpCompressStream.EnsureSeekable(stream); - bStream.StartRecording(); + var sharpCompressStream = SharpCompressStream.EnsureSeekable(stream); + sharpCompressStream.StartRecording(); var factories = Factories.Factory.Factories.OfType(); @@ -48,11 +48,11 @@ public static partial class ReaderFactory .Contains(options.ExtensionHint, StringComparer.CurrentCultureIgnoreCase) ); if ( - testedFactory?.TryOpenReader(bStream, options, out var reader) == true + testedFactory?.TryOpenReader(sharpCompressStream, options, out var reader) == true && reader != null ) { - bStream.Rewind(true); + sharpCompressStream.Rewind(true); return reader; } } @@ -63,8 +63,11 @@ public static partial class ReaderFactory { continue; // Already tested above } - bStream.Rewind(); - if (factory.TryOpenReader(bStream, options, out var reader) && reader != null) + sharpCompressStream.Rewind(); + if ( + factory.TryOpenReader(sharpCompressStream, options, out var reader) + && reader != null + ) { return reader; } diff --git a/src/SharpCompress/Readers/Tar/TarReader.cs b/src/SharpCompress/Readers/Tar/TarReader.cs index 8b380971..b5571a80 100644 --- a/src/SharpCompress/Readers/Tar/TarReader.cs +++ b/src/SharpCompress/Readers/Tar/TarReader.cs @@ -57,61 +57,61 @@ public partial class TarReader : AbstractReader { stream.NotNull(nameof(stream)); options = options ?? new ReaderOptions(); - var rewindableStream = SharpCompressStream.EnsureSeekable(stream); - long pos = rewindableStream.Position; - if (GZipArchive.IsGZipFile(rewindableStream)) + var sharpCompressStream = SharpCompressStream.EnsureSeekable(stream); + long pos = sharpCompressStream.Position; + if (GZipArchive.IsGZipFile(sharpCompressStream)) { - rewindableStream.Position = pos; - var testStream = new GZipStream(rewindableStream, CompressionMode.Decompress); + sharpCompressStream.Position = pos; + var testStream = new GZipStream(sharpCompressStream, CompressionMode.Decompress); if (TarArchive.IsTarFile(testStream)) { - rewindableStream.Position = pos; - return new TarReader(rewindableStream, options, CompressionType.GZip); + sharpCompressStream.Position = pos; + return new TarReader(sharpCompressStream, options, CompressionType.GZip); } throw new InvalidFormatException("Not a tar file."); } - rewindableStream.Position = pos; - if (BZip2Stream.IsBZip2(rewindableStream)) + sharpCompressStream.Position = pos; + if (BZip2Stream.IsBZip2(sharpCompressStream)) { - rewindableStream.Position = pos; + sharpCompressStream.Position = pos; var testStream = BZip2Stream.Create( - rewindableStream, + sharpCompressStream, CompressionMode.Decompress, false ); if (TarArchive.IsTarFile(testStream)) { - rewindableStream.Position = pos; - return new TarReader(rewindableStream, options, CompressionType.BZip2); + sharpCompressStream.Position = pos; + return new TarReader(sharpCompressStream, options, CompressionType.BZip2); } throw new InvalidFormatException("Not a tar file."); } - rewindableStream.Position = pos; - if (ZStandardStream.IsZStandard(rewindableStream)) + sharpCompressStream.Position = pos; + if (ZStandardStream.IsZStandard(sharpCompressStream)) { - rewindableStream.Position = pos; - var testStream = new ZStandardStream(rewindableStream); + sharpCompressStream.Position = pos; + var testStream = new ZStandardStream(sharpCompressStream); if (TarArchive.IsTarFile(testStream)) { - rewindableStream.Position = pos; - return new TarReader(rewindableStream, options, CompressionType.ZStandard); + sharpCompressStream.Position = pos; + return new TarReader(sharpCompressStream, options, CompressionType.ZStandard); } throw new InvalidFormatException("Not a tar file."); } - rewindableStream.Position = pos; - if (LZipStream.IsLZipFile(rewindableStream)) + sharpCompressStream.Position = pos; + if (LZipStream.IsLZipFile(sharpCompressStream)) { - rewindableStream.Position = pos; - var testStream = new LZipStream(rewindableStream, CompressionMode.Decompress); + sharpCompressStream.Position = pos; + var testStream = new LZipStream(sharpCompressStream, CompressionMode.Decompress); if (TarArchive.IsTarFile(testStream)) { - rewindableStream.Position = pos; - return new TarReader(rewindableStream, options, CompressionType.LZip); + sharpCompressStream.Position = pos; + return new TarReader(sharpCompressStream, options, CompressionType.LZip); } throw new InvalidFormatException("Not a tar file."); } - rewindableStream.Position = pos; - return new TarReader(rewindableStream, options, CompressionType.None); + sharpCompressStream.Position = pos; + return new TarReader(sharpCompressStream, options, CompressionType.None); } #endregion OpenReader