From 0d317bb4669a147006539d4665fc1e5a82aea3fd Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 28 Jul 2026 16:38:27 +0000 Subject: [PATCH 01/40] Initial plan From d301cb365fad2db630bc47f5d2dfdb63aba4cdcd Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 28 Jul 2026 16:45:31 +0000 Subject: [PATCH 02/40] Add format-specific ArchiveInformation details --- docs/API.md | 4 +- .../Archives/ArchiveFactory.Detection.cs | 128 +++++++++++++++++- .../Archives/ArchiveInformation.cs | 12 ++ .../SharpCompress.Test/ArchiveFactoryTests.cs | 51 +++++++ 4 files changed, 192 insertions(+), 3 deletions(-) diff --git a/docs/API.md b/docs/API.md index 80143e83..b2145189 100644 --- a/docs/API.md +++ b/docs/API.md @@ -56,6 +56,8 @@ if (info is not null) { Console.WriteLine($"Type: {info.Type}"); Console.WriteLine($"Supports random access: {info.SupportsRandomAccess}"); + Console.WriteLine($"ZIP data descriptor entries: {info.ZipDataDescriptorEntryCount}"); + Console.WriteLine($"Solid stream count: {info.SolidStreamCount}"); } var asyncInfo = await ArchiveFactory.GetArchiveInformationAsync( @@ -73,7 +75,7 @@ using (var archive = ArchiveFactory.OpenArchive(parts)) } ``` -`ArchiveInformation.SupportsRandomAccess` is `true` when the detected format supports `IArchive` random access. It is `false` for reader-only formats such as Ace, Arc, Arj, and standalone LZW, where `ReaderFactory.OpenReader` should be used instead. Compressed tar wrappers such as `.tar.gz` and `.tar.xz` are also reader-only; `ArchiveFactory.GetArchiveInformation` returns `null` for them and `ArchiveFactory.OpenArchive` does not open them as the outer compression wrapper. Use `ReaderFactory.OpenReader` or `TarReader.OpenReader` for those files. +`ArchiveInformation.SupportsRandomAccess` is `true` when the detected format supports `IArchive` random access. It is `false` for reader-only formats such as Ace, Arc, Arj, and standalone LZW, where `ReaderFactory.OpenReader` should be used instead. Compressed tar wrappers such as `.tar.gz` and `.tar.xz` are also reader-only; `ArchiveFactory.GetArchiveInformation` returns `null` for them and `ArchiveFactory.OpenArchive` does not open them as the outer compression wrapper. Use `ReaderFactory.OpenReader` or `TarReader.OpenReader` for those files. Format-specific details are available through `ArchiveInformation.ZipDataDescriptorEntryCount` (ZIP) and `ArchiveInformation.SolidStreamCount` (solid-capable formats such as 7z and RAR). ### Creating Archives diff --git a/src/SharpCompress/Archives/ArchiveFactory.Detection.cs b/src/SharpCompress/Archives/ArchiveFactory.Detection.cs index cf34e6e5..5674a462 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.Detection.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.Detection.cs @@ -2,8 +2,13 @@ using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Archives.Rar; +using SharpCompress.Archives.SevenZip; using SharpCompress.Archives.Tar; +using SharpCompress.Archives.Zip; using SharpCompress.Common; +using SharpCompress.Common.Zip; +using SharpCompress.Common.Zip.Headers; using SharpCompress.Factories; using SharpCompress.IO; using SharpCompress.Providers; @@ -86,7 +91,11 @@ public static partial class ArchiveFactory .ConfigureAwait(false); return factory is null ? null - : new ArchiveInformation(factory.KnownArchiveType, factory is IArchiveFactory); + : BuildArchiveInformation( + stream, + readerOptions ?? ReaderOptions.ForExternalStream, + factory + ); } internal static ValueTask FindFactoryAsync( @@ -254,7 +263,11 @@ public static partial class ArchiveFactory var factory = TryFindFactory(stream, readerOptions ?? ReaderOptions.ForExternalStream); return factory is null ? null - : new ArchiveInformation(factory.KnownArchiveType, factory is IArchiveFactory); + : BuildArchiveInformation( + stream, + readerOptions ?? ReaderOptions.ForExternalStream, + factory + ); } /// @@ -411,4 +424,115 @@ public static partial class ArchiveFactory LzwFactory => CompressionType.Lzw, _ => null, }; + + private static ArchiveInformation BuildArchiveInformation( + Stream stream, + ReaderOptions readerOptions, + IFactory factory + ) + { + var info = new ArchiveInformation(factory.KnownArchiveType, factory is IArchiveFactory); + var startPosition = stream.Position; + + try + { + var probeReaderOptions = readerOptions with { LeaveStreamOpen = true }; + switch (factory) + { + case ZipFactory: + TryPopulateZipDetails(info, stream, probeReaderOptions); + break; + case SevenZipFactory: + TryPopulateSevenZipDetails(info, stream, probeReaderOptions); + break; + case RarFactory: + TryPopulateRarDetails(info, stream, probeReaderOptions); + break; + } + } + finally + { + stream.Seek(startPosition, SeekOrigin.Begin); + } + + return info; + } + + private static void TryPopulateZipDetails( + ArchiveInformation info, + Stream stream, + ReaderOptions readerOptions + ) + { + try + { + info.ZipDataDescriptorEntryCount = GetZipDataDescriptorEntryCount( + stream, + readerOptions + ); + } + catch + { + // Keep archive detection resilient even when format-specific probing fails. + } + } + + private static void TryPopulateSevenZipDetails( + ArchiveInformation info, + Stream stream, + ReaderOptions readerOptions + ) + { + try + { + info.SolidStreamCount = GetSevenZipSolidStreamCount(stream, readerOptions); + } + catch + { + // Keep archive detection resilient even when format-specific probing fails. + } + } + + private static void TryPopulateRarDetails( + ArchiveInformation info, + Stream stream, + ReaderOptions readerOptions + ) + { + try + { + info.SolidStreamCount = GetRarSolidStreamCount(stream, readerOptions); + } + catch + { + // Keep archive detection resilient even when format-specific probing fails. + } + } + + private static int GetZipDataDescriptorEntryCount(Stream stream, ReaderOptions readerOptions) + { + using var archive = ZipArchive.OpenArchive(stream, readerOptions); + return archive + .Entries.OfType() + .SelectMany(entry => entry.Parts.OfType()) + .Count(part => + FlagUtility.HasFlag(part.Header.Flags, HeaderFlags.UsePostDataDescriptor) + ); + } + + private static int GetSevenZipSolidStreamCount(Stream stream, ReaderOptions readerOptions) + { + using var archive = SevenZipArchive.OpenArchive(stream, readerOptions); + return archive + .Entries.OfType() + .Where(entry => !entry.IsDirectory && entry.FilePart.Folder is not null) + .GroupBy(entry => entry.FilePart.Folder) + .Count(group => group.Skip(1).Any()); + } + + private static int GetRarSolidStreamCount(Stream stream, ReaderOptions readerOptions) + { + using var archive = RarArchive.OpenArchive(stream, readerOptions); + return archive.IsSolid ? 1 : 0; + } } diff --git a/src/SharpCompress/Archives/ArchiveInformation.cs b/src/SharpCompress/Archives/ArchiveInformation.cs index 75d3cb5a..81d4fc6d 100644 --- a/src/SharpCompress/Archives/ArchiveInformation.cs +++ b/src/SharpCompress/Archives/ArchiveInformation.cs @@ -25,6 +25,18 @@ public record ArchiveInformation /// public bool SupportsRandomAccess { get; set; } + /// + /// For ZIP archives, the number of entries that use post-data descriptor trailers. + /// This value is for non-ZIP formats. + /// + public int? ZipDataDescriptorEntryCount { get; set; } + + /// + /// For solid-capable archive formats, the number of solid compressed streams present. + /// This value is for formats that do not support solid entries. + /// + public int? SolidStreamCount { get; set; } + /// /// Creates a new archive information instance. /// diff --git a/tests/SharpCompress.Test/ArchiveFactoryTests.cs b/tests/SharpCompress.Test/ArchiveFactoryTests.cs index 199dff47..2c8aa42c 100644 --- a/tests/SharpCompress.Test/ArchiveFactoryTests.cs +++ b/tests/SharpCompress.Test/ArchiveFactoryTests.cs @@ -760,6 +760,57 @@ public class ArchiveFactoryTests : TestBase Assert.Equal(startPosition, stream.Position); } + [Theory] + [InlineData("Zip.none.datadescriptors.zip", true)] + [InlineData("Zip.deflate.zip", false)] + public void GetArchiveInformation_ZipDataDescriptorEntryCount( + string archiveName, + bool expectsDataDescriptorTrailers + ) + { + using var stream = File.OpenRead(GetTestArchivePath(archiveName)); + + var info = ArchiveFactory.GetArchiveInformation(stream); + + Assert.NotNull(info); + Assert.Equal(ArchiveType.Zip, info.Type); + Assert.NotNull(info.ZipDataDescriptorEntryCount); + Assert.Equal(expectsDataDescriptorTrailers, info.ZipDataDescriptorEntryCount > 0); + } + + [Theory] + [InlineData("7Zip.solid.7z", true)] + [InlineData("7Zip.nonsolid.7z", false)] + [InlineData("Rar.rar", false)] + public void GetArchiveInformation_SolidStreamCount(string archiveName, bool expectsSolidStreams) + { + using var stream = File.OpenRead(GetTestArchivePath(archiveName)); + + var info = ArchiveFactory.GetArchiveInformation(stream); + + Assert.NotNull(info); + Assert.NotNull(info.SolidStreamCount); + Assert.Equal(expectsSolidStreams, info.SolidStreamCount > 0); + } + + [Theory] + [InlineData("Zip.none.datadescriptors.zip", true)] + [InlineData("7Zip.solid.7z", true)] + public async ValueTask GetArchiveInformationAsync_IncludesFormatSpecificDetails( + string archiveName, + bool expectsAnyFormatSpecificDetails + ) + { + using var stream = File.OpenRead(GetTestArchivePath(archiveName)); + + var info = await ArchiveFactory.GetArchiveInformationAsync(stream); + + Assert.NotNull(info); + var hasZipDetail = info.ZipDataDescriptorEntryCount > 0; + var hasSolidDetail = info.SolidStreamCount > 0; + Assert.Equal(expectsAnyFormatSpecificDetails, hasZipDetail || hasSolidDetail); + } + private MemoryStream CreatePrefixedArchiveStream(string archiveName, int prefixLength) { var archiveBytes = File.ReadAllBytes(GetTestArchivePath(archiveName)); From f5289ef49e0adc6f34fc172b8e99aaae711acc5c Mon Sep 17 00:00:00 2001 From: Julian Xhokaxhiu Date: Wed, 29 Jul 2026 02:25:58 +0200 Subject: [PATCH 03/40] Improve LZMA decoding for Solid Archives --- .../Archives/SevenZip/SevenZipArchive.cs | 6 ++ .../Common/SevenZip/ArchiveDatabase.Async.cs | 68 +++++++++++++++++++ .../Common/SevenZip/ArchiveDatabase.cs | 67 ++++++++++++++++++ .../Common/SevenZip/SevenZipFilePart.cs | 35 ++++++---- .../IO/BufferedSubStream.Async.cs | 48 ++++++++++++- src/SharpCompress/IO/BufferedSubStream.cs | 22 ++++++ 6 files changed, 229 insertions(+), 17 deletions(-) diff --git a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs index 27822313..53e697a0 100644 --- a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs +++ b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs @@ -78,6 +78,12 @@ public partial class SevenZipArchive : AbstractArchive + /// Async counterpart of the caching GetFolderStream overload. Reuses a cached decoder + /// stream for the folder when possible instead of recreating and re-decoding from the start. + /// + internal async ValueTask GetFolderStreamAsync( + Stream stream, + CFolder folder, + IPasswordProvider pw, + long skipSize, + long entrySize, + CancellationToken cancellationToken + ) + { + if (_cachedFolder == folder && _cachedFolderStream != null) + { + if (skipSize >= _cachedFolderStreamPosition) + { + var delta = skipSize - _cachedFolderStreamPosition; + if (delta > 0) + { + await _cachedFolderStream + .SkipAsync(delta, cancellationToken) + .ConfigureAwait(false); + } + // Assume the caller will fully consume the returned entry stream, advancing the + // shared stream by entrySize bytes. + _cachedFolderStreamPosition = skipSize + entrySize; + return _cachedFolderStream; + } + + // Non-sequential (backward) access within the same folder requires restarting. + await DisposeCachedFolderStreamAsync().ConfigureAwait(false); + } + else if (_cachedFolderStream != null) + { + await DisposeCachedFolderStreamAsync().ConfigureAwait(false); + } + + var newStream = await GetFolderStreamAsync(stream, folder, pw, cancellationToken) + .ConfigureAwait(false); + if (skipSize > 0) + { + await newStream.SkipAsync(skipSize, cancellationToken).ConfigureAwait(false); + } + + _cachedFolder = folder; + _cachedFolderStream = newStream; + _cachedFolderStreamPosition = skipSize + entrySize; + return newStream; + } + + private async ValueTask DisposeCachedFolderStreamAsync() + { + if (_cachedFolderStream is IAsyncDisposable asyncDisposable) + { + await asyncDisposable.DisposeAsync().ConfigureAwait(false); + } + else + { +#pragma warning disable VSTHRD103 // Fallback for streams that do not support async disposal. + _cachedFolderStream?.Dispose(); +#pragma warning restore VSTHRD103 + } + _cachedFolderStream = null; + _cachedFolder = null; + } } diff --git a/src/SharpCompress/Common/SevenZip/ArchiveDatabase.cs b/src/SharpCompress/Common/SevenZip/ArchiveDatabase.cs index dbad5353..cdee70b1 100644 --- a/src/SharpCompress/Common/SevenZip/ArchiveDatabase.cs +++ b/src/SharpCompress/Common/SevenZip/ArchiveDatabase.cs @@ -163,4 +163,71 @@ internal partial class ArchiveDatabase pw ); } + + // Cache used to avoid re-decoding a solid folder from scratch for every file it contains. + // Without this, extracting N files from the same solid folder decodes O(N^2) bytes since each + // file's stream was previously created fresh from the folder start and skipped forward. + private CFolder? _cachedFolder; + private Stream? _cachedFolderStream; + private long _cachedFolderStreamPosition; + + /// + /// Returns a stream positioned at bytes into the decompressed + /// contents of . Reuses a cached decoder stream for the folder when + /// possible instead of recreating and re-decoding from the start. + /// + internal Stream GetFolderStream( + Stream stream, + CFolder folder, + IPasswordProvider pw, + long skipSize, + long entrySize + ) + { + if (_cachedFolder == folder && _cachedFolderStream != null) + { + if (skipSize >= _cachedFolderStreamPosition) + { + var delta = skipSize - _cachedFolderStreamPosition; + if (delta > 0) + { + _cachedFolderStream.Skip(delta); + } + // Assume the caller will fully consume the returned entry stream, advancing the + // shared stream by entrySize bytes. + _cachedFolderStreamPosition = skipSize + entrySize; + return _cachedFolderStream; + } + + // Non-sequential (backward) access within the same folder requires restarting. + _cachedFolderStream.Dispose(); + _cachedFolderStream = null; + _cachedFolder = null; + } + else if (_cachedFolderStream != null) + { + _cachedFolderStream.Dispose(); + _cachedFolderStream = null; + _cachedFolder = null; + } + + var newStream = GetFolderStream(stream, folder, pw); + if (skipSize > 0) + { + newStream.Skip(skipSize); + } + + _cachedFolder = folder; + _cachedFolderStream = newStream; + _cachedFolderStreamPosition = skipSize + entrySize; + return newStream; + } + + internal void DisposeCachedFolderStream() + { + _cachedFolderStream?.Dispose(); + _cachedFolderStream = null; + _cachedFolder = null; + _cachedFolderStreamPosition = 0; + } } diff --git a/src/SharpCompress/Common/SevenZip/SevenZipFilePart.cs b/src/SharpCompress/Common/SevenZip/SevenZipFilePart.cs index 65bb7503..cfedbe07 100644 --- a/src/SharpCompress/Common/SevenZip/SevenZipFilePart.cs +++ b/src/SharpCompress/Common/SevenZip/SevenZipFilePart.cs @@ -44,7 +44,6 @@ internal class SevenZipFilePart : FilePart { return Stream.Null; } - var folderStream = _database.GetFolderStream(_stream, Folder!, _database.PasswordProvider); var firstFileIndex = _database._folderStartFileIndex[_database._folders.IndexOf(Folder!)]; var skipCount = Index - firstFileIndex; @@ -53,11 +52,15 @@ internal class SevenZipFilePart : FilePart { skipSize += _database._files[firstFileIndex + i].Size; } - if (skipSize > 0) - { - folderStream.Skip(skipSize); - } - return new ReadOnlySubStream(folderStream, Header.Size, leaveOpen: false); + + var folderStream = _database.GetFolderStream( + _stream, + Folder!, + _database.PasswordProvider, + skipSize, + Header.Size + ); + return new ReadOnlySubStream(folderStream, Header.Size, leaveOpen: true); } internal override async ValueTask GetCompressedStreamAsync( @@ -68,9 +71,6 @@ internal class SevenZipFilePart : FilePart { return Stream.Null; } - var folderStream = await _database - .GetFolderStreamAsync(_stream, Folder!, _database.PasswordProvider, cancellationToken) - .ConfigureAwait(false); var firstFileIndex = _database._folderStartFileIndex[_database._folders.IndexOf(Folder!)]; var skipCount = Index - firstFileIndex; @@ -79,11 +79,18 @@ internal class SevenZipFilePart : FilePart { skipSize += _database._files[firstFileIndex + i].Size; } - if (skipSize > 0) - { - await folderStream.SkipAsync(skipSize, cancellationToken).ConfigureAwait(false); - } - return new ReadOnlySubStream(folderStream, Header.Size, leaveOpen: false); + + var folderStream = await _database + .GetFolderStreamAsync( + _stream, + Folder!, + _database.PasswordProvider, + skipSize, + Header.Size, + cancellationToken + ) + .ConfigureAwait(false); + return new ReadOnlySubStream(folderStream, Header.Size, leaveOpen: true); } public CompressionType CompressionType diff --git a/src/SharpCompress/IO/BufferedSubStream.Async.cs b/src/SharpCompress/IO/BufferedSubStream.Async.cs index 04c85b07..8312a7d6 100644 --- a/src/SharpCompress/IO/BufferedSubStream.Async.cs +++ b/src/SharpCompress/IO/BufferedSubStream.Async.cs @@ -36,8 +36,10 @@ internal partial class BufferedSubStream BytesLeftToRead -= _cacheLength; } - [Zomp.SyncMethodGenerator.CreateSyncVersion] - public override async Task ReadAsync( + // Fast, synchronous-completion path used when the requested bytes are already sitting in the + // in-memory cache. Avoids the async state-machine / Task allocation overhead incurred by every + // single-byte read the LZMA range decoder issues, without changing buffering/caching semantics. + public override Task ReadAsync( byte[] buffer, int offset, int count, @@ -49,6 +51,24 @@ internal partial class BufferedSubStream count = (int)Length; } + if (count > 0 && _cacheOffset < _cacheLength) + { + count = Math.Min(count, _cacheLength - _cacheOffset); + Buffer.BlockCopy(_cache!, _cacheOffset, buffer, offset, count); + _cacheOffset += count; + return Task.FromResult(count); + } + + return ReadSlowAsync(buffer, offset, count, cancellationToken); + } + + private async Task ReadSlowAsync( + byte[] buffer, + int offset, + int count, + CancellationToken cancellationToken + ) + { if (count > 0) { if (_cacheOffset == _cacheLength) @@ -65,7 +85,7 @@ internal partial class BufferedSubStream } #if !LEGACY_DOTNET - public override async ValueTask ReadAsync( + public override ValueTask ReadAsync( Memory buffer, CancellationToken cancellationToken = default ) @@ -76,6 +96,28 @@ internal partial class BufferedSubStream count = (int)Length; } + if (count > 0 && _cacheOffset < _cacheLength) + { + count = Math.Min(count, _cacheLength - _cacheOffset); + _cache!.AsSpan(_cacheOffset, count).CopyTo(buffer.Span); + _cacheOffset += count; + return new ValueTask(count); + } + + return ReadSlowAsync(buffer, cancellationToken); + } + + private async ValueTask ReadSlowAsync( + Memory buffer, + CancellationToken cancellationToken + ) + { + var count = buffer.Length; + if (count > Length) + { + count = (int)Length; + } + if (count > 0) { if (_cacheOffset == _cacheLength) diff --git a/src/SharpCompress/IO/BufferedSubStream.cs b/src/SharpCompress/IO/BufferedSubStream.cs index 0de8822c..08e60e15 100755 --- a/src/SharpCompress/IO/BufferedSubStream.cs +++ b/src/SharpCompress/IO/BufferedSubStream.cs @@ -73,6 +73,28 @@ internal partial class BufferedSubStream : Stream, IStreamStack return _cache![_cacheOffset++]; } + public override int Read(byte[] buffer, int offset, int count) + { + if (count > Length) + { + count = (int)Length; + } + + if (count > 0) + { + if (_cacheOffset == _cacheLength) + { + RefillCache(); + } + + count = Math.Min(count, _cacheLength - _cacheOffset); + Buffer.BlockCopy(_cache!, _cacheOffset, buffer, offset, count); + _cacheOffset += count; + } + + return count; + } + public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); public override void SetLength(long value) => throw new NotSupportedException(); From 2f545b553a3b470fe5b7891e044a3e0dc6f5ff95 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 29 Jul 2026 07:51:15 +0000 Subject: [PATCH 04/40] Initial plan From a688c7c6572ded37251f5dbb21fc89230a1c0fe6 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 29 Jul 2026 07:55:48 +0000 Subject: [PATCH 05/40] Fix BufferedSubStream ReadAsync cancellation fast-path --- .../IO/BufferedSubStream.Async.cs | 4 ++ .../Streams/BufferedSubStreamAsyncTests.cs | 53 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 tests/SharpCompress.Test/Streams/BufferedSubStreamAsyncTests.cs diff --git a/src/SharpCompress/IO/BufferedSubStream.Async.cs b/src/SharpCompress/IO/BufferedSubStream.Async.cs index 04c85b07..eb740c21 100644 --- a/src/SharpCompress/IO/BufferedSubStream.Async.cs +++ b/src/SharpCompress/IO/BufferedSubStream.Async.cs @@ -44,6 +44,8 @@ internal partial class BufferedSubStream CancellationToken cancellationToken ) { + cancellationToken.ThrowIfCancellationRequested(); + if (count > Length) { count = (int)Length; @@ -70,6 +72,8 @@ internal partial class BufferedSubStream CancellationToken cancellationToken = default ) { + cancellationToken.ThrowIfCancellationRequested(); + var count = buffer.Length; if (count > Length) { diff --git a/tests/SharpCompress.Test/Streams/BufferedSubStreamAsyncTests.cs b/tests/SharpCompress.Test/Streams/BufferedSubStreamAsyncTests.cs new file mode 100644 index 00000000..8a2125de --- /dev/null +++ b/tests/SharpCompress.Test/Streams/BufferedSubStreamAsyncTests.cs @@ -0,0 +1,53 @@ +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using SharpCompress.IO; +using Xunit; + +namespace SharpCompress.Test.Streams; + +public class BufferedSubStreamAsyncTests +{ + [Fact] + public async ValueTask ReadAsyncByteArray_CanceledTokenWithCachedData_ThrowsOperationCanceledException() + { + using var source = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }); + using var stream = new BufferedSubStream(source, origin: 0, bytesToRead: source.Length); + var buffer = new byte[1]; + + Assert.Equal( + 1, + await stream.ReadAsync(buffer, 0, 1, CancellationToken.None).ConfigureAwait(false) + ); + + using var cts = new CancellationTokenSource(); + cts.Cancel(); + + await Assert.ThrowsAnyAsync(() => + stream.ReadAsync(buffer, 0, 1, cts.Token) + ); + } + +#if !LEGACY_DOTNET + [Fact] + public async ValueTask ReadAsyncMemory_CanceledTokenWithCachedData_ThrowsOperationCanceledException() + { + using var source = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }); + using var stream = new BufferedSubStream(source, origin: 0, bytesToRead: source.Length); + var buffer = new byte[1]; + + Assert.Equal( + 1, + await stream.ReadAsync(buffer, 0, 1, CancellationToken.None).ConfigureAwait(false) + ); + + using var cts = new CancellationTokenSource(); + cts.Cancel(); + + await Assert.ThrowsAnyAsync(async () => + await stream.ReadAsync(buffer.AsMemory(), cts.Token).ConfigureAwait(false) + ); + } +#endif +} From a8f6f188438091800a093866cf318f244c39b985 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Fri, 31 Jul 2026 14:21:24 +0100 Subject: [PATCH 06/40] Fix SeekableSharpCompressStream.StopRecording() to rewind to recorded position When SeekableSharpCompressStream.StopRecording() was called (e.g. after the tar probe in GZipFactory.TryOpenReader), it cleared _recordedPosition without seeking back to it. This left the underlying FileStream at the advanced position reached during the probe, so the subsequent GZipReader would start reading from the wrong offset, producing Key=null and 0 extracted bytes. Fix: seek back to _recordedPosition before clearing it, matching the behavior of the non-seekable SharpCompressStream.StopRecording() which rewinds _logicalPosition. Also enhance the existing GZip_ReaderFactory_FlatGZip test to actually verify the extracted content (not just MoveToNextEntry returns true), and add an async variant. Closes #1391 --- .../IO/SeekableSharpCompressStream.cs | 11 ++++++- .../GZip/GZipReaderAsyncTests.cs | 30 +++++++++++++++++++ .../GZip/GZipReaderTests.cs | 6 ++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/SharpCompress/IO/SeekableSharpCompressStream.cs b/src/SharpCompress/IO/SeekableSharpCompressStream.cs index bad123bb..e3aa9bb5 100644 --- a/src/SharpCompress/IO/SeekableSharpCompressStream.cs +++ b/src/SharpCompress/IO/SeekableSharpCompressStream.cs @@ -83,7 +83,16 @@ internal sealed partial class SeekableSharpCompressStream : SharpCompressStream public override void StartRecording(int? minBufferSize = null) => _recordedPosition = _stream.Position; - public override void StopRecording() => _recordedPosition = null; + public override void StopRecording() + { + if (_recordedPosition.HasValue) + { + // Seek back to the recording anchor position, matching the behavior of the + // non-seekable SharpCompressStream.StopRecording() which rewinds _logicalPosition. + _stream.Seek(_recordedPosition.Value, SeekOrigin.Begin); + } + _recordedPosition = null; + } protected override void Dispose(bool disposing) { diff --git a/tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs index 83d2b352..a3da7e04 100644 --- a/tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs @@ -1,4 +1,5 @@ using System.IO; +using System.IO.Compression; using System.Threading.Tasks; using SharpCompress.Common; using SharpCompress.IO; @@ -37,4 +38,33 @@ public class GZipReaderAsyncTests : ReaderTests } } } + + [Fact] + public async ValueTask GZip_ReaderFactory_FlatGZip_Async() + { + var source = new byte[2048]; + for (var i = 0; i < source.Length; i++) + { + source[i] = 0xFF; + } + + var gzipPath = Path.Combine(SCRATCH_FILES_PATH, "Flat.bin.gz"); + using (var output = File.Create(gzipPath)) + await using (var gzip = new GZipStream(output, CompressionMode.Compress)) + { + await gzip.WriteAsync(source, 0, source.Length); + } + + await using Stream stream = File.OpenRead(gzipPath); + await using var reader = await ReaderFactory.OpenAsyncReader(stream); + Assert.IsType(reader); + Assert.True(await reader.MoveToNextEntryAsync()); + + using var ms = new MemoryStream(); + await reader.WriteEntryToAsync(ms); + Assert.Equal(source.Length, ms.Length); + Assert.Equal(source, ms.ToArray()); + + Assert.False(await reader.MoveToNextEntryAsync()); + } } diff --git a/tests/SharpCompress.Test/GZip/GZipReaderTests.cs b/tests/SharpCompress.Test/GZip/GZipReaderTests.cs index fb3936a8..ab870da2 100644 --- a/tests/SharpCompress.Test/GZip/GZipReaderTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipReaderTests.cs @@ -48,6 +48,12 @@ public class GZipReaderTests : ReaderTests using var reader = ReaderFactory.OpenReader(stream); Assert.IsType(reader); Assert.True(reader.MoveToNextEntry()); + + using var ms = new MemoryStream(); + reader.WriteEntryTo(ms); + Assert.Equal(source.Length, ms.Length); + Assert.Equal(source, ms.ToArray()); + Assert.False(reader.MoveToNextEntry()); } } From 82c44cb5a5f9299418f37d171adb1b83d3add772 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Fri, 31 Jul 2026 14:52:58 +0100 Subject: [PATCH 07/40] remove async dispose --- tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs index a3da7e04..98914780 100644 --- a/tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs @@ -50,12 +50,12 @@ public class GZipReaderAsyncTests : ReaderTests var gzipPath = Path.Combine(SCRATCH_FILES_PATH, "Flat.bin.gz"); using (var output = File.Create(gzipPath)) - await using (var gzip = new GZipStream(output, CompressionMode.Compress)) + using (var gzip = new GZipStream(output, CompressionMode.Compress)) { await gzip.WriteAsync(source, 0, source.Length); } - await using Stream stream = File.OpenRead(gzipPath); + using Stream stream = File.OpenRead(gzipPath); await using var reader = await ReaderFactory.OpenAsyncReader(stream); Assert.IsType(reader); Assert.True(await reader.MoveToNextEntryAsync()); From cfdfad26b614661a1c43497395812a2fab1a5b87 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 1 Aug 2026 03:40:04 +0000 Subject: [PATCH 08/40] Fix RAR5 async decompression corruption in UnpWriteBufAsync Three bugs in the async version of UnpWriteBuf (used only in the async decompression path) were causing data corruption when decompressing RAR5 archives containing executable filters (E8/E8E9/ARM) whose blocks spanned the 4MB write boundary (UNPACK_MAX_WRITE): 1. WrittenFileSize was only incremented inside `if (OutMem != null)`, but should always be incremented after ApplyFilter (matching sync behavior). 2. UnpSomeRead = true was missing from the filter processing path. 3. In the NotAllFiltersProcessed else branch (filter intersects write border): - WrPtr was not updated to WrittenBorder, corrupting the window pointer used to compute WriteBorder on the next flush cycle - Remaining filters had NextWindow set to true (inverted logic), when sync correctly sets them to false so they are processed next time All three changes align the async path with the sync UnpWriteBuf logic. --- global.json | 2 +- .../Rar/UnpackV2017/Unpack.unpack50_async.cs | 22 ++++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/global.json b/global.json index f5d6141c..8a7554cf 100644 --- a/global.json +++ b/global.json @@ -3,4 +3,4 @@ "version": "10.0.301", "rollForward": "disable" } -} +} \ No newline at end of file diff --git a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack50_async.cs b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack50_async.cs index cabce8cf..fbb2d607 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack50_async.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack50_async.cs @@ -394,28 +394,34 @@ internal partial class Unpack { await UnpIO_UnpWriteAsync(OutMem, 0, BlockLength, cancellationToken) .ConfigureAwait(false); - WrittenFileSize += BlockLength; } + UnpSomeRead = true; + WrittenFileSize += BlockLength; WrittenBorder = BlockEnd; WriteSizeLeft = (UnpPtr - WrittenBorder) & MaxWinMask; } } else { - NotAllFiltersProcessed = true; + // Current filter intersects the window write border, so we adjust + // the window border to process this filter next time, not now. + WrPtr = WrittenBorder; + + // Since Filter start position can only increase, we quit processing + // all following filters for this data block and reset 'NextWindow' + // flag for them. for (var J = I; J < Filters.Count; J++) { var fltj = Filters[J]; - if ( - fltj.Type != FILTER_NONE - && fltj.NextWindow == false - && ((fltj.BlockStart - WrPtr) & MaxWinMask) < FullWriteSize - ) + if (fltj.Type != FILTER_NONE) { - fltj.NextWindow = true; + fltj.NextWindow = false; } } + + // Do not write data left after current filter now. + NotAllFiltersProcessed = true; break; } } From bc06a9df8ba3f262a7a9a8231a0630872b22dd29 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Sat, 1 Aug 2026 12:43:44 +0100 Subject: [PATCH 09/40] Some clean up --- global.json | 2 +- .../Compressors/Rar/RarCrcStream.Async.cs | 15 ++++----- .../Compressors/Rar/RarCrcStream.cs | 33 ++++++++----------- src/SharpCompress/packages.lock.json | 12 +++---- .../SharpCompress.AotSmoke/packages.lock.json | 24 +++++++------- 5 files changed, 39 insertions(+), 47 deletions(-) diff --git a/global.json b/global.json index 8a7554cf..e784f38f 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "10.0.301", + "version": "10.0.302", "rollForward": "disable" } } \ No newline at end of file diff --git a/src/SharpCompress/Compressors/Rar/RarCrcStream.Async.cs b/src/SharpCompress/Compressors/Rar/RarCrcStream.Async.cs index 2aedc9a6..a76a9ed6 100644 --- a/src/SharpCompress/Compressors/Rar/RarCrcStream.Async.cs +++ b/src/SharpCompress/Compressors/Rar/RarCrcStream.Async.cs @@ -1,5 +1,4 @@ using System; -using System.IO; using System.Threading; using System.Threading.Tasks; using SharpCompress.Common; @@ -31,11 +30,11 @@ internal partial class RarCrcStream : RarStream .ConfigureAwait(false); if (result != 0) { - currentCrc = RarCRC.CheckCrc(currentCrc, buffer, offset, result); + _currentCrc = RarCRC.CheckCrc(_currentCrc, buffer, offset, result); } else if ( - !disableCRC - && GetCrc() != BitConverter.ToUInt32(readStream.NotNull().CurrentCrc.NotNull(), 0) + !_disableCrc + && GetCrc() != BitConverter.ToUInt32(_readStream.NotNull().CurrentCrc.NotNull(), 0) && count != 0 ) { @@ -56,16 +55,16 @@ internal partial class RarCrcStream : RarStream var result = await base.ReadAsync(buffer, cancellationToken).ConfigureAwait(false); if (result != 0) { - currentCrc = RarCRC.CheckCrc(currentCrc, buffer.Span, 0, result); + _currentCrc = RarCRC.CheckCrc(_currentCrc, buffer.Span, 0, result); } else if ( - !disableCRC - && GetCrc() != BitConverter.ToUInt32(readStream.NotNull().CurrentCrc.NotNull(), 0) + !_disableCrc + && GetCrc() != BitConverter.ToUInt32(_readStream.NotNull().CurrentCrc.NotNull(), 0) && buffer.Length != 0 ) { // NOTE: we use the last FileHeader in a multipart volume to check CRC - throw new InvalidFormatException("file crc mismatch"); + throw new InvalidFormatException("file crc mismatch: " + _key); } return result; diff --git a/src/SharpCompress/Compressors/Rar/RarCrcStream.cs b/src/SharpCompress/Compressors/Rar/RarCrcStream.cs index a9025e19..233b05be 100644 --- a/src/SharpCompress/Compressors/Rar/RarCrcStream.cs +++ b/src/SharpCompress/Compressors/Rar/RarCrcStream.cs @@ -1,7 +1,4 @@ using System; -using System.IO; -using System.Threading; -using System.Threading.Tasks; using SharpCompress.Common; using SharpCompress.Common.Rar.Headers; @@ -9,9 +6,10 @@ namespace SharpCompress.Compressors.Rar; internal partial class RarCrcStream : RarStream { - private readonly MultiVolumeReadOnlyStreamBase readStream; - private uint currentCrc; - private readonly bool disableCRC; + private readonly string? _key; + private readonly MultiVolumeReadOnlyStreamBase _readStream; + private uint _currentCrc; + private readonly bool _disableCrc; private RarCrcStream( IRarUnpack unpack, @@ -20,8 +18,9 @@ internal partial class RarCrcStream : RarStream ) : base(unpack, fileHeader, readStream) { - this.readStream = readStream; - disableCRC = fileHeader.IsEncrypted; + this._readStream = readStream; + _key = fileHeader.FileName; + _disableCrc = fileHeader.IsEncrypted; ResetCrc(); } @@ -36,31 +35,25 @@ internal partial class RarCrcStream : RarStream } // Async methods moved to RarCrcStream.Async.cs + public uint GetCrc() => ~_currentCrc; - protected override void Dispose(bool disposing) - { - base.Dispose(disposing); - } - - public uint GetCrc() => ~currentCrc; - - public void ResetCrc() => currentCrc = 0xffffffff; + public void ResetCrc() => _currentCrc = 0xffffffff; public override int Read(byte[] buffer, int offset, int count) { var result = base.Read(buffer, offset, count); if (result != 0) { - currentCrc = RarCRC.CheckCrc(currentCrc, buffer, offset, result); + _currentCrc = RarCRC.CheckCrc(_currentCrc, buffer, offset, result); } else if ( - !disableCRC - && GetCrc() != BitConverter.ToUInt32(readStream.NotNull().CurrentCrc.NotNull(), 0) + !_disableCrc + && GetCrc() != BitConverter.ToUInt32(_readStream.NotNull().CurrentCrc.NotNull(), 0) && count != 0 ) { // NOTE: we use the last FileHeader in a multipart volume to check CRC - throw new InvalidFormatException("file crc mismatch"); + throw new InvalidFormatException("file crc mismatch: " + _key); } return result; diff --git a/src/SharpCompress/packages.lock.json b/src/SharpCompress/packages.lock.json index 54571844..c49dccb6 100644 --- a/src/SharpCompress/packages.lock.json +++ b/src/SharpCompress/packages.lock.json @@ -321,9 +321,9 @@ "net10.0": { "Microsoft.NET.ILLink.Tasks": { "type": "Direct", - "requested": "[10.0.9, )", - "resolved": "10.0.9", - "contentHash": "4Iw41e2h7I4t70SJcX2GCmbyKJIlA273Cfm9RJMM050/3VBejGAG1KcthP5Z2L6SQcbfbf6BhNWO26+ZG+GzMg==" + "requested": "[10.0.10, )", + "resolved": "10.0.10", + "contentHash": "f5VCIE7AJpd5YvzNTeMGVzQIgyE9tX+AreTYwQF+REbu+DZo/2Ae+jNSwhPEYrVz6RRkd7y8ubXjk6Nn6Ka+Cg==" }, "Microsoft.NETFramework.ReferenceAssemblies": { "type": "Direct", @@ -441,9 +441,9 @@ "net8.0": { "Microsoft.NET.ILLink.Tasks": { "type": "Direct", - "requested": "[8.0.28, )", - "resolved": "8.0.28", - "contentHash": "XMqgVjlLxLqWmEh3c49haXLQwsMNtvo6YscUaqfvEGfg1iA8hnYgkUVq3i9Zu9gKeNKMWiiZKVwZExc/qyEAsQ==" + "requested": "[8.0.29, )", + "resolved": "8.0.29", + "contentHash": "HSBTfrkIZijz8z3ybLRKB7E8rHk4QQufFwpHa9fc5CMIgRhRzdn4mBGmlyXZqaueiMPtuJcnjresGvSTfaW8Mg==" }, "Microsoft.NETFramework.ReferenceAssemblies": { "type": "Direct", diff --git a/tests/SharpCompress.AotSmoke/packages.lock.json b/tests/SharpCompress.AotSmoke/packages.lock.json index 19031145..9e1690d2 100644 --- a/tests/SharpCompress.AotSmoke/packages.lock.json +++ b/tests/SharpCompress.AotSmoke/packages.lock.json @@ -4,15 +4,15 @@ "net10.0": { "Microsoft.DotNet.ILCompiler": { "type": "Direct", - "requested": "[10.0.9, )", - "resolved": "10.0.9", - "contentHash": "4y+VsQOcs4EiTSINdCpCWi/aLRbIbGTxSezQXd8uGVhzbDRm1FNVTZDyCUQixE0+g9UFusvfxVcF68YYz7RzxA==" + "requested": "[10.0.10, )", + "resolved": "10.0.10", + "contentHash": "tnG8ntt/Bk6odvHREnGLMo3PEiihy5iSlIFVp0JbIo00GKtNRt2k73eKZbPqR5yaJNIa3z8R86YLwbxfqpb17g==" }, "Microsoft.NET.ILLink.Tasks": { "type": "Direct", - "requested": "[10.0.9, )", - "resolved": "10.0.9", - "contentHash": "4Iw41e2h7I4t70SJcX2GCmbyKJIlA273Cfm9RJMM050/3VBejGAG1KcthP5Z2L6SQcbfbf6BhNWO26+ZG+GzMg==" + "requested": "[10.0.10, )", + "resolved": "10.0.10", + "contentHash": "f5VCIE7AJpd5YvzNTeMGVzQIgyE9tX+AreTYwQF+REbu+DZo/2Ae+jNSwhPEYrVz6RRkd7y8ubXjk6Nn6Ka+Cg==" }, "Microsoft.NETFramework.ReferenceAssemblies": { "type": "Direct", @@ -76,17 +76,17 @@ "net10.0/linux-x64": { "Microsoft.DotNet.ILCompiler": { "type": "Direct", - "requested": "[10.0.9, )", - "resolved": "10.0.9", - "contentHash": "4y+VsQOcs4EiTSINdCpCWi/aLRbIbGTxSezQXd8uGVhzbDRm1FNVTZDyCUQixE0+g9UFusvfxVcF68YYz7RzxA==", + "requested": "[10.0.10, )", + "resolved": "10.0.10", + "contentHash": "tnG8ntt/Bk6odvHREnGLMo3PEiihy5iSlIFVp0JbIo00GKtNRt2k73eKZbPqR5yaJNIa3z8R86YLwbxfqpb17g==", "dependencies": { - "runtime.linux-x64.Microsoft.DotNet.ILCompiler": "10.0.9" + "runtime.linux-x64.Microsoft.DotNet.ILCompiler": "10.0.10" } }, "runtime.linux-x64.Microsoft.DotNet.ILCompiler": { "type": "Transitive", - "resolved": "10.0.9", - "contentHash": "45CVefG8S0eUKUJ4LBWOi8FOAgMJOP6exW9l5M9OjvQaGR7jvkokBK50XaZCsO66uLxABcuzvncV8A3YiJLUgw==" + "resolved": "10.0.10", + "contentHash": "WRjSRBfv6A6UjgjO8EQuLe9xqdICpkQx1hACUziCw4B2uGL+2jVhkFLq/G7rxRr3MGvqLo9B+nNdfIJ/5CYN7A==" } } } From 2a2218bcf734a0cede2a315246ec42843e65d0a1 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Sat, 1 Aug 2026 12:49:47 +0100 Subject: [PATCH 10/40] small fix --- src/SharpCompress/Compressors/Rar/RarCrcStream.Async.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SharpCompress/Compressors/Rar/RarCrcStream.Async.cs b/src/SharpCompress/Compressors/Rar/RarCrcStream.Async.cs index a76a9ed6..0042af7a 100644 --- a/src/SharpCompress/Compressors/Rar/RarCrcStream.Async.cs +++ b/src/SharpCompress/Compressors/Rar/RarCrcStream.Async.cs @@ -39,7 +39,7 @@ internal partial class RarCrcStream : RarStream ) { // NOTE: we use the last FileHeader in a multipart volume to check CRC - throw new InvalidFormatException("file crc mismatch"); + throw new InvalidFormatException("file crc mismatch: " + _key); } return result; From b91289d82c732e50c8554d051726027c889cd476 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Sat, 1 Aug 2026 13:25:08 +0100 Subject: [PATCH 11/40] add large file tests and GZip/Lzw factory fixes --- src/SharpCompress/Factories/GZipFactory.cs | 12 +- src/SharpCompress/Factories/LzwFactory.cs | 12 +- tests/SharpCompress.Test/LargeArchiveTests.cs | 187 ++++++++++++++++++ tests/TestArchives/Archives/Large/Large.7z | Bin 0 -> 10012 bytes tests/TestArchives/Archives/Large/Large.gz | Bin 0 -> 162850 bytes tests/TestArchives/Archives/Large/Large.rar | Bin 0 -> 3147 bytes .../TestArchives/Archives/Large/Large.tar.gz | Bin 0 -> 163062 bytes tests/TestArchives/Archives/Large/Large.zip | Bin 0 -> 162830 bytes tests/TestArchives/Archives/Large/README.md | 38 ++++ 9 files changed, 241 insertions(+), 8 deletions(-) create mode 100644 tests/SharpCompress.Test/LargeArchiveTests.cs create mode 100644 tests/TestArchives/Archives/Large/Large.7z create mode 100644 tests/TestArchives/Archives/Large/Large.gz create mode 100644 tests/TestArchives/Archives/Large/Large.rar create mode 100644 tests/TestArchives/Archives/Large/Large.tar.gz create mode 100644 tests/TestArchives/Archives/Large/Large.zip create mode 100644 tests/TestArchives/Archives/Large/README.md diff --git a/src/SharpCompress/Factories/GZipFactory.cs b/src/SharpCompress/Factories/GZipFactory.cs index bda6ef1f..4649f0fc 100644 --- a/src/SharpCompress/Factories/GZipFactory.cs +++ b/src/SharpCompress/Factories/GZipFactory.cs @@ -151,13 +151,16 @@ public class GZipFactory CompressionContext.FromStream(sharpCompressStream).WithReaderOptions(options) ) ); - if (TarArchive.IsTarFile(testStream)) + var isTarArchive = TarArchive.IsTarFile(testStream); + + // The TAR probe can consume arbitrary compressed input before it rejects a stream. + sharpCompressStream.Rewind(); + sharpCompressStream.StopRecording(); + if (isTarArchive) { - sharpCompressStream.StopRecording(); reader = new TarReader(sharpCompressStream, options, CompressionType.GZip); return true; } - sharpCompressStream.StopRecording(); reader = OpenReader(sharpCompressStream, options); return true; } @@ -185,12 +188,13 @@ public class GZipFactory var tarReader = await new TarFactory() .TryOpenReaderAsync(sharpCompressStream, options, cancellationToken) .ConfigureAwait(false); + sharpCompressStream.Rewind(); + sharpCompressStream.StopRecording(); if (tarReader is not null) { return tarReader; } - sharpCompressStream.StopRecording(); return await OpenAsyncReader(sharpCompressStream, options, cancellationToken) .ConfigureAwait(false); } diff --git a/src/SharpCompress/Factories/LzwFactory.cs b/src/SharpCompress/Factories/LzwFactory.cs index bd5569d1..da475d8a 100644 --- a/src/SharpCompress/Factories/LzwFactory.cs +++ b/src/SharpCompress/Factories/LzwFactory.cs @@ -65,14 +65,17 @@ public class LzwFactory : Factory, IReaderFactory ) ) { - if (TarArchive.IsTarFile(testStream)) + var isTarArchive = TarArchive.IsTarFile(testStream); + + // The TAR probe can consume arbitrary compressed input before it rejects a stream. + sharpCompressStream.Rewind(); + sharpCompressStream.StopRecording(); + if (isTarArchive) { - sharpCompressStream.StopRecording(); reader = new TarReader(sharpCompressStream, options, CompressionType.Lzw); return true; } } - sharpCompressStream.StopRecording(); reader = OpenReader(sharpCompressStream, options); return true; } @@ -100,12 +103,13 @@ public class LzwFactory : Factory, IReaderFactory var tarReader = await new TarFactory() .TryOpenReaderAsync(sharpCompressStream, options, cancellationToken) .ConfigureAwait(false); + sharpCompressStream.Rewind(); + sharpCompressStream.StopRecording(); if (tarReader is not null) { return tarReader; } - sharpCompressStream.StopRecording(); return await OpenAsyncReader(sharpCompressStream, options, cancellationToken) .ConfigureAwait(false); } diff --git a/tests/SharpCompress.Test/LargeArchiveTests.cs b/tests/SharpCompress.Test/LargeArchiveTests.cs new file mode 100644 index 00000000..fc94c935 --- /dev/null +++ b/tests/SharpCompress.Test/LargeArchiveTests.cs @@ -0,0 +1,187 @@ +using System; +using System.IO; +using System.IO.Compression; +using System.Linq; +using System.Threading.Tasks; +using SharpCompress.Archives; +using SharpCompress.Crypto; +using SharpCompress.Readers; +using SharpCompress.Test.Mocks; +using Xunit; + +namespace SharpCompress.Test; + +[Collection(LargeArchiveCollection.Name)] +public class LargeArchiveTests : TestBase +{ + private const long LargeFileSize = 64L * 1024 * 1024; + private const uint LargeFileCrc = 0xF9081EB0; + private const int BufferSize = 64 * 1024; + + [Theory] + [InlineData("Large/Large.zip")] + [InlineData("Large/Large.tar")] + [InlineData("Large/Large.gz")] + [InlineData("Large/Large.rar")] + [InlineData("Large/Large.7z")] + public void OpenArchive_ShouldStreamLargeEntry(string fixtureName) + { + using var stream = File.OpenRead(GetArchiveFixturePath(fixtureName)); + using var archive = ArchiveFactory.OpenArchive(stream); + + VerifyArchive(archive); + } + + [Theory] + [InlineData("Large/Large.zip")] + [InlineData("Large/Large.tar")] + [InlineData("Large/Large.gz")] + [InlineData("Large/Large.rar")] + [InlineData("Large/Large.7z")] + public async Task OpenAsyncArchive_ShouldStreamLargeEntry(string fixtureName) + { + await using var stream = new AsyncOnlyStream( + File.OpenRead(await GetArchiveFixturePathAsync(fixtureName)) + ); + await using var archive = await ArchiveFactory.OpenAsyncArchive(stream); + + var entry = await GetSingleEntryAsync(archive); + await using var entryStream = await entry.OpenEntryStreamAsync(); + await VerifyContentAsync(entry.Key, entryStream); + } + + [Theory] + [InlineData("Large/Large.zip")] + [InlineData("Large/Large.tar")] + [InlineData("Large/Large.gz")] + [InlineData("Large/Large.rar")] + [InlineData("Large/Large.tar.gz")] + public void OpenReader_ShouldStreamLargeEntry(string fixtureName) + { + using var stream = File.OpenRead(GetFixturePath(fixtureName)); + using var reader = ReaderFactory.OpenReader(stream); + + VerifyReader(reader); + } + + [Theory] + [InlineData("Large/Large.zip")] + [InlineData("Large/Large.tar")] + [InlineData("Large/Large.gz")] + [InlineData("Large/Large.rar")] + [InlineData("Large/Large.tar.gz")] + public async Task OpenAsyncReader_ShouldStreamLargeEntry(string fixtureName) + { + await using var stream = new AsyncOnlyStream(File.OpenRead(GetFixturePath(fixtureName))); + await using var reader = await ReaderFactory.OpenAsyncReader(stream); + + Assert.True(await reader.MoveToNextEntryAsync()); + Assert.False(reader.Entry.IsDirectory); + await using var entryStream = await reader.OpenEntryStreamAsync(); + await VerifyContentAsync(reader.Entry.Key, entryStream); + Assert.False(await reader.MoveToNextEntryAsync()); + } + + private static string GetFixturePath(string fixtureName) => + Path.Combine(TEST_ARCHIVES_PATH, fixtureName); + + private string GetArchiveFixturePath(string fixtureName) => + fixtureName == "Large/Large.tar" ? MaterializeTarFixture() : GetFixturePath(fixtureName); + + private async Task GetArchiveFixturePathAsync(string fixtureName) => + fixtureName == "Large/Large.tar" + ? await MaterializeTarFixtureAsync() + : GetFixturePath(fixtureName); + + private string MaterializeTarFixture() + { + var tarPath = Path.Combine(SCRATCH_FILES_PATH, "Large.tar"); + using var compressedStream = File.OpenRead(GetFixturePath("Large/Large.tar.gz")); + using var gzipStream = new GZipStream(compressedStream, CompressionMode.Decompress); + using var tarStream = File.Create(tarPath); + gzipStream.CopyTo(tarStream); + return tarPath; + } + + private async Task MaterializeTarFixtureAsync() + { + var tarPath = Path.Combine(SCRATCH_FILES_PATH, "Large.tar"); + using var compressedStream = File.OpenRead(GetFixturePath("Large/Large.tar.gz")); + using var gzipStream = new GZipStream(compressedStream, CompressionMode.Decompress); + using var tarStream = File.Create(tarPath); + await gzipStream.CopyToAsync(tarStream); + return tarPath; + } + + private static void VerifyArchive(IArchive archive) + { + var entry = Assert.Single(archive.Entries); + + Assert.False(entry.IsDirectory); + using var entryStream = entry.OpenEntryStream(); + VerifyContent(entry.Key, entryStream); + } + + private static async Task GetSingleEntryAsync(IAsyncArchive archive) + { + IArchiveEntry? entry = null; + await foreach (var candidate in archive.EntriesAsync) + { + Assert.Null(entry); + entry = candidate; + } + + return entry ?? throw new InvalidOperationException("The archive contains no entries."); + } + + private static void VerifyReader(IReader reader) + { + Assert.True(reader.MoveToNextEntry()); + Assert.False(reader.Entry.IsDirectory); + using var entryStream = reader.OpenEntryStream(); + VerifyContent(reader.Entry.Key, entryStream); + Assert.False(reader.MoveToNextEntry()); + } + + private static void VerifyContent(string? key, Stream entryStream) + { + Assert.Equal("large.bin", key); + + using var crcStream = new Crc32Stream(Stream.Null); + var buffer = new byte[BufferSize]; + long length = 0; + int bytesRead; + while ((bytesRead = entryStream.Read(buffer, 0, buffer.Length)) > 0) + { + crcStream.Write(buffer, 0, bytesRead); + length += bytesRead; + } + + Assert.Equal(LargeFileSize, length); + Assert.Equal(LargeFileCrc, crcStream.Crc); + } + + private static async Task VerifyContentAsync(string? key, Stream entryStream) + { + Assert.Equal("large.bin", key); + + using var crcStream = new Crc32Stream(Stream.Null); + var buffer = new byte[BufferSize]; + long length = 0; + int bytesRead; + while ((bytesRead = await entryStream.ReadAsync(buffer, 0, buffer.Length)) > 0) + { + await crcStream.WriteAsync(buffer, 0, bytesRead); + length += bytesRead; + } + + Assert.Equal(LargeFileSize, length); + Assert.Equal(LargeFileCrc, crcStream.Crc); + } +} + +[CollectionDefinition(LargeArchiveCollection.Name, DisableParallelization = true)] +public sealed class LargeArchiveCollection +{ + public const string Name = "Large archive fixtures"; +} diff --git a/tests/TestArchives/Archives/Large/Large.7z b/tests/TestArchives/Archives/Large/Large.7z new file mode 100644 index 0000000000000000000000000000000000000000..719ea18df023e2174e0eab883ff7aaf94708504b GIT binary patch literal 10012 zcmXr7+Ou9=hJhvXKy3CRH3l$<0+S33?Hc>J|G(eH*b~c8|Ni&>e~UN9J^f+7X+;y` zn{5`37g`_kg;XeJ>8|AbsI%?7_m`^7>^r|_HkhPniYpy|8Kc&(%e|8P8J|MgXgaT#T~chpaXM46<@`+eYNS}2euc4lqr+k!7|Gq15|@LBCl zUBlU-_3X;5=PueiW88{n-eRy@t9HPFp?|AnRH)^{y^HL^QtLdvTqp@^p2L!NX79}x zU2CW7{?~gwOLo_}jp`CHk!>&2f+F_4EL#)#P4JMT273Br8;1t$L}tcd)hGh|)5;a&d00$+EYixUM`txcf0)tk9<0_VRMWYlJOQyjef z@4j_4hEnsl_ZsFE%v-J;|3>3<>&2-vy6Utheo`;}E_r}$hVP8OH`ZUBz&~Iv9>Bl? zSrw-3;GKFI3PLVvW-A1(Ap3;oeT zf3(maoyf&C;XYbRj@FW+wd80mIa*7Ouv)T7R*FFq+j6H6ZS80W1_nkJHU>t{Ws4Zt zxfvN46%`p}c%Fb3uyJrPZjj^n$-uzMD9Hg50AWE$Q{SToRe6To~oCWnaA+vcl5n&n94!e) zOTy8TaI_>GEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@z!dl5n&n94!e)OTy8TaI_>G zEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@z!dl5n&n9E>HQe07lr0|-3%pY6WlUiJIE z-$#kjFc?h-quF4zC>SjXM@z!dl5n&n94!e)OTy8TaI_>GEeS_U!qJj&v?LrY2}eu9 z(UNeqBpfXXM@z!dl5n&n94!e)OTy8TaI_>GEeS_U!qJj&v?LrY2}eu9(UNeqBpfXX zM@zz?R1&t^TB?B70y2F1zg0P8D7kzzosGuUXpK8sJNjO>(j+TU@CE;jE zI9d{pmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{pmV~1v z;b=)XS`vJNeC_p56_bk0xt&q`Tv&Ss?nlhv}hPD8b*r- zaM3VIj24HZ#o=giI9eQzmV~1v;b=)XS`vJNjO>(j+TU@ zCE;jEI9d{pmV~1v;b=)XS`vJNjO>(j+TU@CE-vi2^0PK zI6;d68~)V4<+wVOTt1r4Mq_KV#vQG3M{C^Cl5n&n94!e)OTy8TaI_>GEeS_U!qJj& zv?LrY2}eu9(UNeqBpfXXM@z!dl5n&n94!e)OTy8TaI_>GEeS_U!qJj&v?LrY2}eu9 z(UNeqBpfXXM@z!dl5n&n1eb&x)Z1A=ivb<}?JsM2HCi-`77e3C!)Va}E*eIO(c*Bl zI2GEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@z!d zl5n&n94!e)OTy8TaI_>GEeS_U!qJj&v?LrY2}eu9(UNeqBpga5;f=}8OrXVp3jgD+ zoxTnwmyf2i(byWTaYt+1(HeKOBpfXXM@z!dl5n&n94!e)OTy8TaI_>GEeS_U!qJj& zG#iYTgrgJNjO>(j+TU@CE;jEI9d{pmV~1v;b=)XS`v0mS+jHZLp8gaBn z94!e)OTy8TaI_>GEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@z!dl5n&n94!e)OTy8T zaI_>GEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@zz?R1(U4dolsE7Leh@|Lc7LL&@c% z>1;H%Mr+*B8h5nD9W4n*OTy8TaI_>GEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@z!d zl5n&n94!e)OTy8TaI_>GEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@z!dl5n&n94!e) zOG0o-$X{LL0bUIFJNjO>(j+TU@CE;jEI9d{pmV~3(V6-G0EeS_U!qJj& zv?LrY2}eu9(UNeqBpfXXM@z!dl5i-MgzdJLD&WO{pZ;%E4jD==A5CYYu{B!bj@G!N zHSTChI9d{pmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{p zmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{dOTxqRWQ4$r z0e}9#CAey|Xc#RTMvI2gq5)hqj1r^8;b?IJNjO>( zj+TU@CE;jEI9d{pmV~1v;b=)XS`vJNjO>(j+TU@CE;jE zI9d{pmV~1v;b=)XluE)xe?CsoV!(z!^=~<@4kee5rnAx58m)0hYuwQqceErNEeS_U z!qJj&v?LrY2}eu9(UNeqBpfXXM@z!dl5n&n94!e)OTy8TaI_>GEeS_U!qJj&v?LrY z2}eu9(UNeqBpfXXM@z!dl5jK|jFyC>CE;jEI9d{pmW1GvaD#d~3urN*!@vDyEw4t4 zhS8#7v}hPD8o))vC^1?bjuwZb#o=giI9d{pmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{pmV~1v;b=)XS`vJNjO>( zj+TT&sU*BH*_jEn7*OGVytUKUq2%(>bT%4Wqc!emjXPT7j+TU@CE;jEI9d{pmV~1v z;b=)XS`vJNjO>(j+TU@CE;jEI9d{pmV~1v;b=)XS`vJNjO>(j+TU@B_XIJw0(Qhke7jhp<%(lwQnCjIBNNPPVu=> zVl)g!)4^yq7%d7$OTy8TaI_>GEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@z!dl5n&n z94!e)OTy8TaI_>GEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@z!tToTR`-yp~FlYxPO F1ps&6m3ROE literal 0 HcmV?d00001 diff --git a/tests/TestArchives/Archives/Large/Large.rar b/tests/TestArchives/Archives/Large/Large.rar new file mode 100644 index 0000000000000000000000000000000000000000..6f0a65d484263ea26b0589e8bb2b542e390d6d8b GIT binary patch literal 3147 zcmWGaEK-zWXJlYm*eb}&$jZsU!N}ME0t}%urxvO+G5ww1(9poZ*3i(fpn+jYGZVu@ z?zjzd96uXe898$ji_%l|k}~r;S?_WiyM9#u8eqIH(M{jLv(bz5!;~%#g^7DRe>F>M zyjwr{daT~OfAx8B`|t1B`SIoD_UHTU=h@iV{jUG|@!!1raaR8&7#J9&kJk4vFfg$G zGhkp~5dU$2fq^0ZF#`hwMua<%=x literal 0 HcmV?d00001 diff --git a/tests/TestArchives/Archives/Large/Large.tar.gz b/tests/TestArchives/Archives/Large/Large.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..007fef083fe9e4d65cac12d5c3639a5659ff8303 GIT binary patch literal 163062 zcmb2|=3oE;CgwNS4*Cf@${cujbn?sKGY3B#t-T#_?5&Yot6K2jLWRJhkH2HpX6LKjwEScK+e-J_>yo(bv%^>Zjd;F%`s6qM;>kMQ zN`InOZ`vP~{Qg^I==S^Ecc-Vm2~E{W;@KEI<*yQV_Z!Y#^UenEu9;dg@7U{ay4!o* zUhD7FxwHPi$6qZOzH+ww^6%~MzkPC@b^i9B9IJD|mt(G0UiP!UKH0=_bv*Aq9`@t& zFC9GiAZc~=r=`I;?Teo-JpBDNU#`t*yPDs>9{oJ~eYW4S{Tq(Y{&j!m|3~kHKP3FC zP7O^}ob})ReY=Y1ulHGH%O5XWo4$F&FJYd=r;q;Gwbt5Qd}V5n;W3HiQDQUGEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@z!d zl5n&n94!e)OTy8TaI_>GEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@z!dl91Sv@Y`ft zOBK+fM21iQwJNjO>(j+TU@ zCE;jEI9d{pmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{p zmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{p zmV~1v;b=)XS`vJNjO>(j+TU@CE;jE2rdZ|{rNaSivb({ z)W7ApI$AW077e3C!)Va}E*eIO(c*BlI2GEeS_U z!qJj&v?LrY2}eu9(UNeqBpfXXM@z!dl5n&n94!e)OTy8TaI_>GEeS_U!qJj&v?LrY z2}eu9(UNeqBpga5;Rf|~7SLiqhkyIaT3!t$myf2i(byWTaYt+1(HeKOBpfXXM@z!d zl5n&n94!e)OTy8TaI_>GEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@z!dl5n&n94!e) zOTy8TaI_>GEeS_U!qJj&v?LrY2}eu9(UNeqBpfXX!6o61$<9om#efR`(jus80 zMZ;*(Fj_Q#i-u8Rv^X3s4o8c_(c*BlBpfXXM@z!dl5n&n94!e)OTy8TaI_>GEeS_U z!qJj&v?LrY2}eu9(UNeqBpfXXM@z!dl5n&n94!e)OTy8TaI_>GEeS_U!qJj&v?LrY z35QTgX#3`*A|q%q;DrC{WfxWrA(xL9HKRq%Xi+noVMiNXqb1>JNjO>(j+TU@CE;jE zI9d{pmV~1v;b=)XS`vGEeS_U!qJj&v?LrY2}eu9(UNeqBm|d)a^Idz0IdaNIPw2_U%+V5Fj_Q>77e3C z1Gs1yB}R+G(c*BlI2GEeS_U!qJj&v?LrY2}eu9 z(UNeqBpfXXM@z!dl5n&n94!e)OTy8TaI_>GEeS_U!qJj&v?LrY2}eu9(UNc|m4y7& zMIPYAfKUEsyRR5ZE+0*2qp>ww(j+TU@CE;jEI9d{pmV~1v;b=)X zS`vJNjO>(j+TU@CE;jEI9d{pmV~1v;b=)XS`vJNjO>(f=j}7TT2!2V!%)Tw77e3C!)VbkS~P%*hEZa)I2GEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@z!dl5jK| zjFyC>CE;jEI9d{pmV~1v;b=)XS`vJNjO>( zj+TU@CE;jEI9d{pmV~1v;b=)XS`vJNjO>(j+TU@CE;jE zI9d{pmW1GvFwvin6SNqx;ZOZrj;o_Z!)VbkS~QFn4d9|-lo%}zM~lPJ;&8M$94!e) zOTy8TaI_>GEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@z!dl5n&n94!e)OTy8TaI_>G zEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@zz?R1$7bZ)X8526XthzpUleP;&WbIvb6x z(HeKO#vQG3M@z!dl5n&n94!e)OTy8TaI_>GEeS_U!qJj&v?LrY2}eu9(UNeqBpfXX zM@z!dl5n&n94!e)OTy8TaI_>GEeS_U!qJj&v?LtO2BRh6Xh}F)5{{OHqb1>JNeC_p zZ%lS(0xbqq_#bcW^mVjo7%dt`i-ysn0bDeU5~Ic8XmL1N9F7);qb1>JNjO>(j+TU@ zCE;jEI9d{pmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{p zmV~1v;b=)XS`vGEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@z!d zl5n&n94!e)OTy8TaI_>GEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@vF*NjUG@lL?@; zfQ%FVhyQk7_-Ib?Im_px#Aq0dri0OJFj^FhmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{pmV~1v;b=)XS`vJNjO>( bj+TU@B_UNx!tIred7_sW+AA_}F|YstBaSne literal 0 HcmV?d00001 diff --git a/tests/TestArchives/Archives/Large/Large.zip b/tests/TestArchives/Archives/Large/Large.zip new file mode 100644 index 0000000000000000000000000000000000000000..5b07336fde030aff7517df489d4596cddafca01f GIT binary patch literal 162830 zcmWIWW@Zs#U}E54U|>)T*dWL8b5=DI0|NsC3nv2uLr!8*da7PhX5O2Vr+FI`L>M;g z4&KnMY{AI>M@8x+v+(xgjydaPnHhe}wz?nQdnwAQI7fBu@!Rt?E^m5XaL)4ioZ?Yp zGz>=5!Du!ZEeb|U!qJj&v?LrY2}eu9(UNeqBpfXXM@z!dl5n&n94!e)OTy8TaI_>G zEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@z!dl5n&n94!e)OTy8TaI_>GEeS_U!qJj& zv?LrY2}eu9(UNeqBpfXXM@z!dl5n&n94!e)OTy8TaI_>GEeS_U!qJj&v?LrY2}eu9 z(UNeqBpfXXM@z!dl5n&n94!e)OTy8TaI_>GEeS_U!qJj&v?LrY2}eu9(UNeqBpfXX zM@z!dl8_oDp?r0b2k5{+hA02C-B;YJe!utoC@~raqv>EY8;lkOqb1>JNjO>(j+TU@ zCE;jEI9d{pmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{p zmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{pmV~1v;b=)X zS`vJNjO>(j+TU@CE;jEI9d{pmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{pmV~1v;b=)n%aX9&)=~w07~rS>Ta`m- zY13#tj)v}NjW$}Njn-(RCE;jEI9d{pmV~1v;b=)XS`vJ zNjO>(j+TU@CE;jEI9d{pmV~1v;b=)XS`vJNjO>(j+TU@ zCE;jEI9d{pmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{p zmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{pmV~1v;b=)X zS`vGEeS_U!qJj&v?LrY2}iTRXh}F)5{{OHqb1>JNjO>(j+TU@CE;jE zI9d{pmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{pmV~1v z;b=)XS`vJNjO>(j+TU@CE;jEI9d{pmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{pmV~1vAuUV7M1MX`&|!cLf9l_I zT&1N=qwzQzx}!DPXpJ^nqm7n?qb1>JNjO>(j+TU@CE;jEI9d{pmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{pmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{pmV~1v;b=)XS`vJ zNjO>(j+TU@CE;jEI9d{pmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{pmV~1v z;b=)XS`vJNjO>(j+TU@CE;jEI9d{pmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{pmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{pmV~1v;b=)XS`vGEeS_U!qJj&v?LrY2}eu9(UNeq zBpfXXM@z!dl5n&n94!e)OTy8TaI_>GEeS_U!qJj&v?LrY2}eu9(UNeqBpl5Kqb1>J zNjO>(j+TU@CE;jEI9d{pmV~1v;b=)XS`vJNjO>(j+TU@ zCE;jEI9d{pmV~1v;b=)XS`vJNjO>(j+TU@CE;jEI9d{p zmV~1v;b=)XS`vGEeS_U!qJj&v?LrY2}eu9(UNeq zBpfXXM@z!dl5n&n94!e)OTy8TaI_>GEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@z!d zl5n&n94!e)OTy8TaI_>GEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@z!dl5n&n94!e) zOTy8TaI_>GEeS_U!qJj&v?LrY2}eu9(UNeqBpfXXM@zy%QWDM+5AbGWl4BNOU}E54 zU|>)T*dWL8b5=DI0|NsC3nz$VU|?WmU|?9%2;$@<7Nw`^C1vIXc(byB1Q;0@8O#|N J7!HE9002S{jOhRX literal 0 HcmV?d00001 diff --git a/tests/TestArchives/Archives/Large/README.md b/tests/TestArchives/Archives/Large/README.md new file mode 100644 index 00000000..4992f31b --- /dev/null +++ b/tests/TestArchives/Archives/Large/README.md @@ -0,0 +1,38 @@ +# Large Test Archives + +Each fixture contains one `large.bin` entry with a 67,108,864-byte (64 MiB) +deterministic, repeated text pattern with a 2 KiB `0xFF` prefix. The entry CRC-32 is +`f9081eb0`. + +| Fixture | Archive API | Reader API | +| --- | --- | --- | +| `Large.zip` | Yes | Yes | +| Generated `Large.tar` | Yes | N/A | +| `Large.gz` | Yes | Yes | +| `Large.rar` | Yes | Yes | +| `Large.7z` | Yes | No | +| `Large.tar.gz` | No | Yes | + +The compressible payload keeps the compressed fixtures small while requiring a full +64 MiB decompression to validate each API. The Archive API test expands `Large.tar.gz` +to a scratch `Large.tar` before exercising raw TAR support, so the 64 MiB TAR file is +not committed. + +## Regenerating + +The fixtures were created with `zip`, `tar`, `gzip`, RAR 7.22, and 7-Zip. Run these +commands from a temporary directory after replacing `` with the repository root: + +```sh +yes "SharpCompress large fixture" | head -c 67108864 > large.bin +printf '\377%.0s' {1..2048} > prefix.bin +dd if=prefix.bin of=large.bin bs=2048 count=1 conv=notrunc +touch -t 202001010000 large.bin +mkdir -p /tests/TestArchives/Archives/Large +zip -X -9 -j /tests/TestArchives/Archives/Large/Large.zip large.bin +COPYFILE_DISABLE=1 tar -cf large.tar large.bin +gzip -9 -c large.bin > /tests/TestArchives/Archives/Large/Large.gz +gzip -n -9 -c large.tar > /tests/TestArchives/Archives/Large/Large.tar.gz +rar a -ma5 -m5 -ep /tests/TestArchives/Archives/Large/Large.rar large.bin +7z a -t7z -mx=9 /tests/TestArchives/Archives/Large/Large.7z large.bin +``` From d391cf21e15938731bbc2f68e225e1b9821bee59 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Sat, 1 Aug 2026 13:26:48 +0100 Subject: [PATCH 12/40] finalize check in --- tests/SharpCompress.Test/LargeArchiveTests.cs | 14 ++++++++------ tests/TestArchives/Archives/Large/README.md | 7 +++---- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/tests/SharpCompress.Test/LargeArchiveTests.cs b/tests/SharpCompress.Test/LargeArchiveTests.cs index fc94c935..761abda6 100644 --- a/tests/SharpCompress.Test/LargeArchiveTests.cs +++ b/tests/SharpCompress.Test/LargeArchiveTests.cs @@ -26,7 +26,7 @@ public class LargeArchiveTests : TestBase [InlineData("Large/Large.7z")] public void OpenArchive_ShouldStreamLargeEntry(string fixtureName) { - using var stream = File.OpenRead(GetArchiveFixturePath(fixtureName)); + using var stream = File.OpenRead(GetMaterializedFixturePath(fixtureName)); using var archive = ArchiveFactory.OpenArchive(stream); VerifyArchive(archive); @@ -41,7 +41,7 @@ public class LargeArchiveTests : TestBase public async Task OpenAsyncArchive_ShouldStreamLargeEntry(string fixtureName) { await using var stream = new AsyncOnlyStream( - File.OpenRead(await GetArchiveFixturePathAsync(fixtureName)) + File.OpenRead(await GetMaterializedFixturePathAsync(fixtureName)) ); await using var archive = await ArchiveFactory.OpenAsyncArchive(stream); @@ -58,7 +58,7 @@ public class LargeArchiveTests : TestBase [InlineData("Large/Large.tar.gz")] public void OpenReader_ShouldStreamLargeEntry(string fixtureName) { - using var stream = File.OpenRead(GetFixturePath(fixtureName)); + using var stream = File.OpenRead(GetMaterializedFixturePath(fixtureName)); using var reader = ReaderFactory.OpenReader(stream); VerifyReader(reader); @@ -72,7 +72,9 @@ public class LargeArchiveTests : TestBase [InlineData("Large/Large.tar.gz")] public async Task OpenAsyncReader_ShouldStreamLargeEntry(string fixtureName) { - await using var stream = new AsyncOnlyStream(File.OpenRead(GetFixturePath(fixtureName))); + await using var stream = new AsyncOnlyStream( + File.OpenRead(await GetMaterializedFixturePathAsync(fixtureName)) + ); await using var reader = await ReaderFactory.OpenAsyncReader(stream); Assert.True(await reader.MoveToNextEntryAsync()); @@ -85,10 +87,10 @@ public class LargeArchiveTests : TestBase private static string GetFixturePath(string fixtureName) => Path.Combine(TEST_ARCHIVES_PATH, fixtureName); - private string GetArchiveFixturePath(string fixtureName) => + private string GetMaterializedFixturePath(string fixtureName) => fixtureName == "Large/Large.tar" ? MaterializeTarFixture() : GetFixturePath(fixtureName); - private async Task GetArchiveFixturePathAsync(string fixtureName) => + private async Task GetMaterializedFixturePathAsync(string fixtureName) => fixtureName == "Large/Large.tar" ? await MaterializeTarFixtureAsync() : GetFixturePath(fixtureName); diff --git a/tests/TestArchives/Archives/Large/README.md b/tests/TestArchives/Archives/Large/README.md index 4992f31b..0797db5d 100644 --- a/tests/TestArchives/Archives/Large/README.md +++ b/tests/TestArchives/Archives/Large/README.md @@ -7,16 +7,15 @@ deterministic, repeated text pattern with a 2 KiB `0xFF` prefix. The entry CRC-3 | Fixture | Archive API | Reader API | | --- | --- | --- | | `Large.zip` | Yes | Yes | -| Generated `Large.tar` | Yes | N/A | +| Generated `Large.tar` | Yes | Yes | | `Large.gz` | Yes | Yes | | `Large.rar` | Yes | Yes | | `Large.7z` | Yes | No | | `Large.tar.gz` | No | Yes | The compressible payload keeps the compressed fixtures small while requiring a full -64 MiB decompression to validate each API. The Archive API test expands `Large.tar.gz` -to a scratch `Large.tar` before exercising raw TAR support, so the 64 MiB TAR file is -not committed. +64 MiB decompression to validate each API. The tests expand `Large.tar.gz` to a scratch +`Large.tar` before exercising raw TAR support, so the 64 MiB TAR file is not committed. ## Regenerating From 5f340db48db07f138fd67580269afdfeb5c46e1a Mon Sep 17 00:00:00 2001 From: Julian Xhokaxhiu Date: Sun, 2 Aug 2026 23:11:05 +0200 Subject: [PATCH 13/40] Improve CRC32 by using the slice-by-16 optimization --- src/SharpCompress/Crypto/Crc32Stream.cs | 77 ++++++++++++++++++++----- 1 file changed, 64 insertions(+), 13 deletions(-) diff --git a/src/SharpCompress/Crypto/Crc32Stream.cs b/src/SharpCompress/Crypto/Crc32Stream.cs index 67dcb913..8d72f23f 100644 --- a/src/SharpCompress/Crypto/Crc32Stream.cs +++ b/src/SharpCompress/Crypto/Crc32Stream.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers.Binary; using System.IO; using System.Threading; using System.Threading.Tasks; @@ -119,6 +120,14 @@ public sealed class Crc32Stream : Stream public static uint Compute(uint polynomial, uint seed, ReadOnlySpan buffer) => ~CalculateCrc(InitializeTable(polynomial), seed, buffer); + // Number of "slice-by-N" tables generated per polynomial (see InitializeTable). 16 gives a + // good throughput/table-size tradeoff (16 * 256 * 4 bytes = 16KB per polynomial, generated + // once and cached for the default polynomial) - a well-known technique (zlib-ng, Chromium, + // etc.) for speeding up table-driven CRC32 by processing 16 bytes/iteration through 16 + // independent (parallelizable) table lookups instead of 1 byte/iteration through a single + // lookup on the CPU's dependency chain. + private const int SliceTableCount = 16; + internal static uint[] InitializeTable(uint polynomial) { if (polynomial == DEFAULT_POLYNOMIAL && _defaultTable != null) @@ -126,38 +135,80 @@ public sealed class Crc32Stream : Stream return _defaultTable; } - var createTable = new uint[256]; + // table[0..255] is the ordinary byte-wise CRC table (unchanged layout, so existing + // single-byte callers - CalculateCrc(table, crc, byte) - keep working unmodified). + // table[256*k .. 256*k+255] for k=1..SliceTableCount-1 are the derived "slice" tables: + // table[k][i] = (table[k-1][i] >> 8) ^ table[0][table[k-1][i] & 0xFF], i.e. each table + // folds in the effect of a byte that is k positions further back in the stream than + // the byte table[0] would have processed - letting CalculateCrc(span) below combine 4 + // bytes' worth of independent lookups per 32-bit word with no cross-lookup dependency. + var table = new uint[256 * SliceTableCount]; for (var i = 0; i < 256; i++) { var entry = (uint)i; for (var j = 0; j < 8; j++) { - if ((entry & 1) == 1) - { - entry = (entry >> 1) ^ polynomial; - } - else - { - entry >>= 1; - } + entry = (entry & 1) == 1 ? (entry >> 1) ^ polynomial : entry >> 1; } + table[i] = entry; + } - createTable[i] = entry; + for (var k = 1; k < SliceTableCount; k++) + { + var prevBase = (k - 1) * 256; + var curBase = k * 256; + for (var i = 0; i < 256; i++) + { + var prev = table[prevBase + i]; + table[curBase + i] = (prev >> 8) ^ table[prev & 0xFF]; + } } if (polynomial == DEFAULT_POLYNOMIAL) { - _defaultTable = createTable; + _defaultTable = table; } - return createTable; + return table; } internal static uint CalculateCrc(uint[] table, uint crc, ReadOnlySpan buffer) { unchecked { - for (var i = 0; i < buffer.Length; i++) + var i = 0; + if (table.Length >= 256 * SliceTableCount) + { + while (buffer.Length - i >= 16) + { + var word0 = BinaryPrimitives.ReadUInt32LittleEndian(buffer.Slice(i)) ^ crc; + var word1 = BinaryPrimitives.ReadUInt32LittleEndian(buffer.Slice(i + 4)); + var word2 = BinaryPrimitives.ReadUInt32LittleEndian(buffer.Slice(i + 8)); + var word3 = BinaryPrimitives.ReadUInt32LittleEndian(buffer.Slice(i + 12)); + + crc = + table[15 * 256 + (byte)word0] + ^ table[14 * 256 + (byte)(word0 >> 8)] + ^ table[13 * 256 + (byte)(word0 >> 16)] + ^ table[12 * 256 + (word0 >> 24)] + ^ table[11 * 256 + (byte)word1] + ^ table[10 * 256 + (byte)(word1 >> 8)] + ^ table[9 * 256 + (byte)(word1 >> 16)] + ^ table[8 * 256 + (word1 >> 24)] + ^ table[7 * 256 + (byte)word2] + ^ table[6 * 256 + (byte)(word2 >> 8)] + ^ table[5 * 256 + (byte)(word2 >> 16)] + ^ table[4 * 256 + (word2 >> 24)] + ^ table[3 * 256 + (byte)word3] + ^ table[2 * 256 + (byte)(word3 >> 8)] + ^ table[1 * 256 + (byte)(word3 >> 16)] + ^ table[word3 >> 24]; + + i += 16; + } + } + + for (; i < buffer.Length; i++) { crc = CalculateCrc(table, crc, buffer[i]); } From 55db16ecb3502a16bd82ba4606c7da9f8d0b71c8 Mon Sep 17 00:00:00 2001 From: Julian Xhokaxhiu Date: Sun, 2 Aug 2026 18:08:51 +0200 Subject: [PATCH 14/40] Improve LZMA decoder using 7zip logics for modern dotnet - Add opt-in Parallel decoding - Add a new ASM-like decoder - Add Unit tests for Parallel decoding --- docs/API.md | 7 + .../SevenZip/SevenZipArchive.Async.cs | 9 +- .../Archives/SevenZip/SevenZipArchive.cs | 6 +- .../Common/Options/IParallelismOptions.cs | 14 + .../Common/Options/IReaderOptions.cs | 6 +- .../Common/Options/IWriterOptions.cs | 6 +- .../Common/SevenZip/ArchiveDatabase.cs | 125 ++- .../Common/SevenZip/SevenZipFilePart.cs | 8 +- .../Compressors/LZMA/LZ/LzOutWindow.cs | 29 + .../Compressors/LZMA/Lzma2ParallelDecoder.cs | 293 +++++++ .../Compressors/LZMA/LzmaDecoder.Fast.cs | 794 ++++++++++++++++++ .../Compressors/LZMA/LzmaDecoder.cs | 11 + .../Compressors/LZMA/LzmaStream.cs | 14 + .../Compressors/LZMA/RangeCoder/RangeCoder.cs | 127 ++- src/SharpCompress/Readers/ReaderOptions.cs | 7 + .../Readers/ReaderOptionsExtensions.cs | 8 + .../Writers/GZip/GZipWriterOptions.cs | 6 + .../Writers/SevenZip/SevenZipWriterOptions.cs | 6 + .../Writers/Tar/TarWriterOptions.cs | 6 + src/SharpCompress/Writers/WriterOptions.cs | 6 + .../Writers/WriterOptionsExtensions.cs | 8 + .../Writers/Zip/ZipWriterOptions.cs | 6 + .../SevenZip/SevenZipArchiveParallelTests.cs | 310 +++++++ .../SevenZip/SevenZipArchiveTests.cs | 77 ++ .../Streams/LzmaStreamTests.cs | 302 +++++++ 25 files changed, 2180 insertions(+), 11 deletions(-) create mode 100644 src/SharpCompress/Common/Options/IParallelismOptions.cs create mode 100644 src/SharpCompress/Compressors/LZMA/Lzma2ParallelDecoder.cs create mode 100644 src/SharpCompress/Compressors/LZMA/LzmaDecoder.Fast.cs create mode 100644 tests/SharpCompress.Test/SevenZip/SevenZipArchiveParallelTests.cs diff --git a/docs/API.md b/docs/API.md index 80143e83..1174d0c6 100644 --- a/docs/API.md +++ b/docs/API.md @@ -332,6 +332,10 @@ var hinted = ReaderOptions.ForExternalStream.WithExtensionHint("tar.gz"); // Increase for non-seekable streams with large detection probes, such as SFX RAR var buffered = ReaderOptions.ForExternalStream.WithRewindableBufferSize(1_048_576); +// Opt in to formats' optional parallel decode (e.g. 7-Zip's automatic parallel LZMA2 +// solid-folder decode); default is sequential decoding +var parallel = ReaderOptions.ForExternalStream.WithEnableParallelism(true); + // Extraction presets var safeOptions = ExtractionOptions.SafeExtract; // No overwrite var flatOptions = ExtractionOptions.FlatExtract; // No directory structure @@ -362,6 +366,7 @@ var options = new ReaderOptions DisableCheckIncomplete = false, BufferSize = 81920, RewindableBufferSize = 1_048_576, + EnableParallelism = false, }; var extractionOptions = new ExtractionOptions @@ -444,6 +449,8 @@ var options = new WriterOptions(CompressionType.Deflate) archive.SaveTo("output.zip", options); ``` +`WriterOptions.EnableParallelism` (also available on `ZipWriterOptions`, `TarWriterOptions`, `GZipWriterOptions`, and `SevenZipWriterOptions`) opts in to a format's optional parallel encode. No writer currently implements parallel encoding, so this is a no-op today; it is reserved for future use and mirrors `ReaderOptions.EnableParallelism`. + ### Extraction behavior ```csharp diff --git a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.Async.cs b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.Async.cs index ce00f20d..8cacfa43 100644 --- a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.Async.cs +++ b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.Async.cs @@ -46,7 +46,14 @@ public partial class SevenZipArchive var file = _database._files[i]; entries[i] = new SevenZipArchiveEntry( this, - new SevenZipFilePart(stream, _database, i, file, ReaderOptions.ArchiveEncoding), + new SevenZipFilePart( + stream, + _database, + i, + file, + ReaderOptions.ArchiveEncoding, + ReaderOptions.EnableParallelism + ), ReaderOptions ); } diff --git a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs index 53e697a0..c414065e 100644 --- a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs +++ b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs @@ -54,7 +54,8 @@ public partial class SevenZipArchive : AbstractArchive +/// Options for controlling whether SharpCompress may use multi-threaded processing. +/// +public interface IParallelismOptions +{ + /// + /// When true, opts in to any optional parallel (multi-threaded) processing a compressor may + /// offer. Compressors that do not implement parallel processing ignore this setting. Default + /// is false (single-threaded processing), so behavior is unchanged unless explicitly enabled. + /// + bool EnableParallelism { get; set; } +} diff --git a/src/SharpCompress/Common/Options/IReaderOptions.cs b/src/SharpCompress/Common/Options/IReaderOptions.cs index c25f4825..71561bfe 100644 --- a/src/SharpCompress/Common/Options/IReaderOptions.cs +++ b/src/SharpCompress/Common/Options/IReaderOptions.cs @@ -3,7 +3,11 @@ using SharpCompress.Providers; namespace SharpCompress.Common.Options; -public interface IReaderOptions : IStreamOptions, IEncodingOptions, IProgressOptions +public interface IReaderOptions + : IStreamOptions, + IEncodingOptions, + IProgressOptions, + IParallelismOptions { /// /// Look for RarArchive (Check for self-extracting archives or cases where RarArchive isn't at the start of the file) diff --git a/src/SharpCompress/Common/Options/IWriterOptions.cs b/src/SharpCompress/Common/Options/IWriterOptions.cs index 37fbb21e..90852c53 100644 --- a/src/SharpCompress/Common/Options/IWriterOptions.cs +++ b/src/SharpCompress/Common/Options/IWriterOptions.cs @@ -7,7 +7,11 @@ namespace SharpCompress.Common.Options; /// /// Options for configuring writer behavior when creating archives. /// -public interface IWriterOptions : IStreamOptions, IEncodingOptions, IProgressOptions +public interface IWriterOptions + : IStreamOptions, + IEncodingOptions, + IProgressOptions, + IParallelismOptions { /// /// The compression type to use for the archive. diff --git a/src/SharpCompress/Common/SevenZip/ArchiveDatabase.cs b/src/SharpCompress/Common/SevenZip/ArchiveDatabase.cs index cdee70b1..f616a77f 100644 --- a/src/SharpCompress/Common/SevenZip/ArchiveDatabase.cs +++ b/src/SharpCompress/Common/SevenZip/ArchiveDatabase.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using SharpCompress.Compressors.LZMA; using SharpCompress.Compressors.LZMA.Utilities; +using SharpCompress.IO; namespace SharpCompress.Common.SevenZip; @@ -144,8 +145,30 @@ internal partial class ArchiveDatabase return size; } - internal Stream GetFolderStream(Stream stream, CFolder folder, IPasswordProvider pw) + internal Stream GetFolderStream( + Stream stream, + CFolder folder, + IPasswordProvider pw, + bool enableParallelism = false + ) { +#if !LEGACY_DOTNET + // Opportunistically decode the whole folder concurrently when it's a single, unchained, + // unencrypted LZMA2 stream backed by a real (seekable, positional-I/O-capable) file -- the + // only shape where independent per-block decoding is possible and worth the temp-file + // round-trip. Opt-in only (via ReaderOptions.EnableParallelism): any other shape + // (multi-coder chains, BCJ/delta filters, encryption, a non-file stream, a single-core + // machine, or too few independent restart points to make parallelizing worthwhile) safely + // falls back to the existing sequential decode below. + if ( + enableParallelism + && TryGetParallelDecodedFolderStream(stream, folder, out var parallelStream) + ) + { + return parallelStream!; + } +#endif + var packStreamIndex = folder._firstPackStreamId; var folderStartPackPos = GetFolderStreamPos(folder, 0); var count = folder._packStreams.Count; @@ -164,6 +187,101 @@ internal partial class ArchiveDatabase ); } +#if !LEGACY_DOTNET + /// + /// Attempts to decode entirely up front using + /// , writing the result to a temp file and returning a + /// stream over it. Returns false (and leaves null) whenever + /// the folder's shape, the host stream, or the current hardware make parallel decoding + /// inapplicable or not worthwhile -- callers must fall back to sequential decoding. + /// + private bool TryGetParallelDecodedFolderStream( + Stream stream, + CFolder folder, + out Stream? decodedStream + ) + { + decodedStream = null; + + // Matches 7-Zip's own behavior: with a single available core there is nothing to gain + // from splitting work across threads, so skip straight to the sequential path. + if (Environment.ProcessorCount <= 1) + { + return false; + } + + // Only a lone, unchained LZMA2 coder can be split into independently-decodable blocks. + // Any bind pairs (BCJ/delta filters, etc.) or additional coders (e.g. AES encryption) + // mean the folder's bytes depend on more than just this one LZMA2 stream. + if ( + folder._coders.Count != 1 + || folder._coders[0]._methodId != CMethodId.K_LZMA2 + || folder._packStreams.Count != 1 + || folder._bindPairs.Count != 0 + ) + { + return false; + } + + var props = folder._coders[0]._props; + if (props is null || props.Length == 0) + { + return false; + } + + // Parallel decoding needs positional (random-access) reads from the source file and + // positional writes to the temp output file across multiple threads; a real, seekable + // file handle is required for that. Streams the archive was opened from may be wrapped + // (buffering, source-stream indirection, etc.), so unwrap looking for the underlying file. + var inputFile = stream as FileStream ?? (stream as IStreamStack)?.GetStream(); + if (inputFile is null) + { + return false; + } + + var folderIndex = _folders.IndexOf(folder); + var packStart = GetFolderStreamPos(folder, 0); + var packSize = GetFolderFullPackSize(folderIndex); + var unpackSize = folder.GetUnpackSize(); + + var blocks = Lzma2ParallelDecoder.TryScanBlocks(stream, packStart, packSize, unpackSize); + if (blocks is null || blocks.Count <= 1) + { + return false; + } + + var tempFile = new FileStream( + Path.GetTempFileName(), + FileMode.Create, + FileAccess.ReadWrite, + FileShare.None, + 4096, + FileOptions.DeleteOnClose + ); + + try + { + Lzma2ParallelDecoder.DecodeBlocksParallel( + inputFile.SafeFileHandle, + props, + packStart, + blocks, + tempFile.SafeFileHandle, + Math.Min(Environment.ProcessorCount, Lzma2ParallelDecoder.MaxThreads) + ); + } + catch + { + tempFile.Dispose(); + throw; + } + + tempFile.Position = 0; + decodedStream = tempFile; + return true; + } +#endif + // Cache used to avoid re-decoding a solid folder from scratch for every file it contains. // Without this, extracting N files from the same solid folder decodes O(N^2) bytes since each // file's stream was previously created fresh from the folder start and skipped forward. @@ -181,7 +299,8 @@ internal partial class ArchiveDatabase CFolder folder, IPasswordProvider pw, long skipSize, - long entrySize + long entrySize, + bool enableParallelism = false ) { if (_cachedFolder == folder && _cachedFolderStream != null) @@ -211,7 +330,7 @@ internal partial class ArchiveDatabase _cachedFolder = null; } - var newStream = GetFolderStream(stream, folder, pw); + var newStream = GetFolderStream(stream, folder, pw, enableParallelism); if (skipSize > 0) { newStream.Skip(skipSize); diff --git a/src/SharpCompress/Common/SevenZip/SevenZipFilePart.cs b/src/SharpCompress/Common/SevenZip/SevenZipFilePart.cs index cfedbe07..007b3508 100644 --- a/src/SharpCompress/Common/SevenZip/SevenZipFilePart.cs +++ b/src/SharpCompress/Common/SevenZip/SevenZipFilePart.cs @@ -11,18 +11,21 @@ internal class SevenZipFilePart : FilePart private CompressionType? _type; private readonly Stream _stream; private readonly ArchiveDatabase _database; + private readonly bool _enableParallelism; internal SevenZipFilePart( Stream stream, ArchiveDatabase database, int index, CFileItem fileEntry, - IArchiveEncoding archiveEncoding + IArchiveEncoding archiveEncoding, + bool enableParallelism = false ) : base(archiveEncoding) { _stream = stream; _database = database; + _enableParallelism = enableParallelism; Index = index; Header = fileEntry; if (Header.HasStream) @@ -58,7 +61,8 @@ internal class SevenZipFilePart : FilePart Folder!, _database.PasswordProvider, skipSize, - Header.Size + Header.Size, + _enableParallelism ); return new ReadOnlySubStream(folderStream, Header.Size, leaveOpen: true); } diff --git a/src/SharpCompress/Compressors/LZMA/LZ/LzOutWindow.cs b/src/SharpCompress/Compressors/LZMA/LZ/LzOutWindow.cs index 78300446..2b38fac7 100644 --- a/src/SharpCompress/Compressors/LZMA/LZ/LzOutWindow.cs +++ b/src/SharpCompress/Compressors/LZMA/LZ/LzOutWindow.cs @@ -24,6 +24,35 @@ internal partial class OutWindow : IDisposable public long Total => _total; +#if !LEGACY_DOTNET + // Fast-path accessors used by the local-variable LZMA decode loop (see + // LzmaDecoder.Fast.cs). CodeFast snapshots pos/total/buffer into locals for the whole + // decode call instead of going through PutByte/GetByte/CopyBlock (and re-reading the + // fields of this object) for every single output byte, mirroring how the reference + // 7-Zip C decoder caches dicPos/dic as locals for the duration of one decode call. + internal byte[] FastBuffer => _buffer; + internal int FastPos + { + get => _pos; + set => _pos = value; + } + internal long FastTotal + { + get => _total; + set => _total = value; + } + internal int FastWindowSize => _windowSize; + internal long FastLimit => _limit; + + internal void FastFlush() => Flush(); + + internal void SetPendingFast(int distance, int len) + { + _pendingDist = distance; + _pendingLen = len; + } +#endif + public void Create(int windowSize) { if (windowSize <= 0) diff --git a/src/SharpCompress/Compressors/LZMA/Lzma2ParallelDecoder.cs b/src/SharpCompress/Compressors/LZMA/Lzma2ParallelDecoder.cs new file mode 100644 index 00000000..0539c195 --- /dev/null +++ b/src/SharpCompress/Compressors/LZMA/Lzma2ParallelDecoder.cs @@ -0,0 +1,293 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading.Tasks; +#if !LEGACY_DOTNET +using Microsoft.Win32.SafeHandles; +#endif + +namespace SharpCompress.Compressors.LZMA; + +/// +/// A contiguous run of one or more independent LZMA2 chunks that can be decoded without any +/// state (dictionary / range-coder) carried over from a preceding chunk. +/// +internal readonly record struct Lzma2Block( + long PackOffset, + long PackLen, + long UnpackOffset, + long UnpackLen +); + +/// +/// Splits a single LZMA2-coded stream into independently-decodable blocks and decodes them +/// concurrently. The block-splitting strategy mirrors 7-Zip's own multi-threaded LZMA2 decoder +/// (see the reference 7-Zip C source, Lzma2DecMt.c / MtDec.c): independent restart points +/// (LZMA2 control byte 0x01, or 0xE0-0xFF) are candidate block boundaries, but consecutive +/// restart segments are merged together until the accumulated block reaches at least +/// decoded bytes -- 7-Zip's own comment for this is "we decode small +/// blocks in one thread" -- to avoid parallelization overhead for tiny fragments. Merging also +/// stops once a block would exceed , matching 7-Zip's outBlockMax +/// default (Lzma2DecMtProps_Init: 1<<28 / 256MB). +/// +internal static class Lzma2ParallelDecoder +{ + // 7-Zip Lzma2DecMt.c (block-parse loop): "we decode small blocks in one thread" -- + // if (t->dec.decoder.dicPos >= (1 << 14)) break; + internal const int MinBlockSize = 1 << 14; + + // 7-Zip Lzma2DecMt.c: Lzma2DecMtProps_Init -> p->outBlockMax = 1 << 28 + internal const long MaxBlockSize = 1 << 28; + + // 7-Zip MtDec.h: #define MTDEC_THREADS_MAX 32 + internal const int MaxThreads = 32; + + /// + /// Header-only walk of the LZMA2 chunk stream (no LZMA decoding) that locates every + /// independent restart point and merges adjacent segments into blocks using the same size + /// thresholds 7-Zip's own MT decoder uses. Returns null if the stream doesn't parse as + /// well-formed LZMA2 or the parsed total doesn't match ; callers + /// should fall back to ordinary sequential decoding in that case. + /// + internal static List? TryScanBlocks( + Stream packStream, + long packStart, + long packSize, + long unpackSize + ) + { + try + { + packStream.Position = packStart; + + var blocks = new List(); + long packPos = 0; + long unpackPos = 0; + + var blockPackStart = 0L; + var blockUnpackStart = 0L; + var blockUnpackSize = 0L; + var blockHasContent = false; + + Span sizeBuf = stackalloc byte[2]; + Span header = stackalloc byte[4]; + + while (packPos < packSize) + { + var chunkPackStart = packPos; + var control = packStream.ReadByte(); + if (control < 0) + { + return null; // truncated stream + } + packPos++; + + if (control == 0) + { + break; // end-of-stream marker + } + + long chunkUnpackSize; + long chunkPackPayload; + bool isRestart; + + if (control < 0x80) + { + // 0x01 = uncompressed chunk + dictionary reset, 0x02 = uncompressed, no reset + if (control > 2) + { + return null; // not a recognized LZMA2 control byte + } + if (!ReadFully(packStream, sizeBuf)) + { + return null; + } + packPos += 2; + chunkUnpackSize = ((sizeBuf[0] << 8) | sizeBuf[1]) + 1; + chunkPackPayload = chunkUnpackSize; + isRestart = control == 1; + } + else + { + // 0x80-0xFF = compressed chunk; bit layout: 1uuuuu, u = high bits of unpack size + if (!ReadFully(packStream, header)) + { + return null; + } + packPos += 4; + chunkUnpackSize = + (long)(((control & 0x1F) << 16) | (header[0] << 8) | header[1]) + 1; + chunkPackPayload = ((header[2] << 8) | header[3]) + 1; + // 0xA0-0xBF state reset, 0xC0-0xDF state reset + new props, 0xE0-0xFF also + // resets the dictionary -- only a dictionary reset is an independent restart. + isRestart = control >= 0xE0; + + // 0xC0-0xFF (new-props flag, bits 6-5 = 1x) carries one extra properties byte + // right after the 4 size-field bytes, before the compressed payload begins. + if (control >= 0xC0) + { + if (packStream.ReadByte() < 0) + { + return null; + } + packPos++; + } + } + + if ( + isRestart + && blockHasContent + && ( + blockUnpackSize >= MinBlockSize + || blockUnpackSize + chunkUnpackSize > MaxBlockSize + ) + ) + { + blocks.Add( + new Lzma2Block( + blockPackStart, + chunkPackStart - blockPackStart, + blockUnpackStart, + blockUnpackSize + ) + ); + blockPackStart = chunkPackStart; + blockUnpackStart = unpackPos; + blockUnpackSize = 0; + blockHasContent = false; + } + + packPos += chunkPackPayload; + if (packPos > packSize) + { + return null; // chunk claims more pack bytes than the folder actually has + } + packStream.Seek(chunkPackPayload, SeekOrigin.Current); + unpackPos += chunkUnpackSize; + blockUnpackSize += chunkUnpackSize; + blockHasContent = true; + } + + if (unpackPos != unpackSize || packPos != packSize) + { + return null; // scan didn't land on the expected totals -- don't trust it + } + + if (blockHasContent) + { + blocks.Add( + new Lzma2Block( + blockPackStart, + packPos - blockPackStart, + blockUnpackStart, + blockUnpackSize + ) + ); + } + + // Guards against a single unmerged segment large enough to overflow a byte[] buffer + // (only possible with an extreme, effectively-pathological run with zero restarts). + foreach (var block in blocks) + { + if (block.PackLen > int.MaxValue - 16 || block.UnpackLen > int.MaxValue - 16) + { + return null; + } + } + + return blocks; + } + catch (Exception) + { + return null; + } + } + + private static bool ReadFully(Stream stream, Span buffer) + { + var total = 0; + while (total < buffer.Length) + { + var read = stream.Read(buffer.Slice(total)); + if (read <= 0) + { + return false; + } + total += read; + } + return true; + } + +#if !LEGACY_DOTNET + /// + /// Decodes every block concurrently, reading packed bytes positionally from + /// and writing decoded bytes positionally to + /// . Both handles must support positional (random) access + /// since blocks complete out of order across threads. + /// + internal static void DecodeBlocksParallel( + SafeFileHandle inputHandle, + byte[] lzma2Props, + long packStart, + IReadOnlyList blocks, + SafeFileHandle outputHandle, + int maxDegreeOfParallelism + ) + { + Parallel.ForEach( + blocks, + new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism }, + block => + { + var packBuffer = new byte[block.PackLen]; + ReadFullyAt(inputHandle, packBuffer, packStart + block.PackOffset); + + using var packStream = new MemoryStream(packBuffer, writable: false); + using var lzma = LzmaStream.Create( + lzma2Props, + packStream, + -1, + block.UnpackLen, + leaveOpen: true + ); + + var buffer = new byte[Math.Min(block.UnpackLen, 1 << 20)]; + long total = 0; + while (total < block.UnpackLen) + { + var toRead = (int)Math.Min(buffer.Length, block.UnpackLen - total); + var read = lzma.Read(buffer, 0, toRead); + if (read <= 0) + { + throw new EndOfStreamException( + $"LZMA2 block at unpack offset {block.UnpackOffset:N0} ended early after {total:N0}/{block.UnpackLen:N0} bytes." + ); + } + RandomAccess.Write( + outputHandle, + buffer.AsSpan(0, read), + block.UnpackOffset + total + ); + total += read; + } + } + ); + } + + private static void ReadFullyAt(SafeFileHandle handle, byte[] buffer, long fileOffset) + { + var total = 0; + while (total < buffer.Length) + { + var read = RandomAccess.Read(handle, buffer.AsSpan(total), fileOffset + total); + if (read <= 0) + { + throw new EndOfStreamException( + "Unexpected end of pack stream while reading an LZMA2 block." + ); + } + total += read; + } + } +#endif +} diff --git a/src/SharpCompress/Compressors/LZMA/LzmaDecoder.Fast.cs b/src/SharpCompress/Compressors/LZMA/LzmaDecoder.Fast.cs new file mode 100644 index 00000000..81ad80d8 --- /dev/null +++ b/src/SharpCompress/Compressors/LZMA/LzmaDecoder.Fast.cs @@ -0,0 +1,794 @@ +#nullable disable + +#if !LEGACY_DOTNET +using System; +using System.Runtime.CompilerServices; +using SharpCompress.Compressors.LZMA.LZ; + +namespace SharpCompress.Compressors.LZMA; + +// Fast LZMA decode path used on .NET targets that support it (everything except +// net48/netstandard2.0/netstandard2.1, see LEGACY_DOTNET). This mirrors the design of the +// reference LZMA SDK / 7-Zip C decoder (LzmaDec.c): probabilities are stored in flat ushort +// arrays (instead of arrays of BitDecoder structs reached through nested decoder objects), +// range/code/dictionary-position/input-buffer-position are kept in local variables (pinned +// with `fixed` and accessed through raw pointers, exactly like the C reference's `probs`/ +// `dic`/`buf` locals) for the duration of the decode loop, instead of being re-read from +// object fields - and re-bounds-checked - on every bit. Compressed input is consumed from a +// buffered reader (RangeCoder.Decoder's fast buffer) instead of one virtual Stream.ReadByte() +// call per byte. +// +// These tables are separate from (and not kept in sync with) the BitDecoder-based tables used +// by the async decode path (LzmaDecoder.Async.cs), which remains unchanged on every target. +// Both sets are kept up to date by SetDecoderProperties/Init, so either decode path can be used +// on a given Decoder instance, as long as sync and async decoding are not interleaved on the +// same instance. +public partial class Decoder +{ + private const int KNumMoveBitsFast = 5; + + // Bias used by the branchless probability-update formula in DecodeBitFast (see there for + // the derivation): (1 << KNumMoveBitsFast) - 1. + private const int KBitModelOffsetFast = (1 << KNumMoveBitsFast) - 1; + + // Choice/Choice2 flags followed by Low (16 posStates * 8 symbols), Mid (16 posStates * 8 + // symbols) and High (256 symbols) trees, laid out contiguously rather than reusing 7-Zip's + // overlapping ASM-oriented addressing, to keep the arithmetic straightforward and safe. + private const int LenChoiceIndex = 0; + private const int LenChoice2Index = 1; + private const int LenLowBase = 2; + private const int LenLowStride = 1 << Base.K_NUM_LOW_LEN_BITS; // 8 + private const int LenLowSize = (int)Base.K_NUM_POS_STATES_MAX * LenLowStride; // 128 + private const int LenMidBase = LenLowBase + LenLowSize; + private const int LenMidStride = 1 << Base.K_NUM_MID_LEN_BITS; // 8 + private const int LenMidSize = (int)Base.K_NUM_POS_STATES_MAX * LenMidStride; // 128 + private const int LenHighBase = LenMidBase + LenMidSize; + private const int LenHighSize = 1 << Base.K_NUM_HIGH_LEN_BITS; // 256 + private const int LenProbsSize = LenHighBase + LenHighSize; + + private ushort[] _fIsMatch; + private ushort[] _fIsRep; + private ushort[] _fIsRepG0; + private ushort[] _fIsRepG1; + private ushort[] _fIsRepG2; + private ushort[] _fIsRep0Long; + private ushort[] _fPosSlot; + private ushort[] _fPosDecoders; + private ushort[] _fPosAlign; + private ushort[] _fLenProbs; + private ushort[] _fRepLenProbs; + private ushort[] _fLiteral; + + private int _fLiteralNumPrevBits = -1; + private int _fLiteralNumPosBits = -1; + private uint _fLiteralPosMask; + + private void CreateFastModel(int lp, int lc) + { + _fIsMatch ??= new ushort[Base.K_NUM_STATES << Base.K_NUM_POS_STATES_BITS_MAX]; + _fIsRep ??= new ushort[Base.K_NUM_STATES]; + _fIsRepG0 ??= new ushort[Base.K_NUM_STATES]; + _fIsRepG1 ??= new ushort[Base.K_NUM_STATES]; + _fIsRepG2 ??= new ushort[Base.K_NUM_STATES]; + _fIsRep0Long ??= new ushort[Base.K_NUM_STATES << Base.K_NUM_POS_STATES_BITS_MAX]; + _fPosSlot ??= new ushort[Base.K_NUM_LEN_TO_POS_STATES << Base.K_NUM_POS_SLOT_BITS]; + _fPosDecoders ??= new ushort[Base.K_NUM_FULL_DISTANCES - Base.K_END_POS_MODEL_INDEX]; + _fPosAlign ??= new ushort[1 << Base.K_NUM_ALIGN_BITS]; + _fLenProbs ??= new ushort[LenProbsSize]; + _fRepLenProbs ??= new ushort[LenProbsSize]; + + if (_fLiteralNumPrevBits != lc || _fLiteralNumPosBits != lp) + { + _fLiteralNumPrevBits = lc; + _fLiteralNumPosBits = lp; + _fLiteralPosMask = ((uint)1 << lp) - 1; + var numStates = (uint)1 << (lc + lp); + _fLiteral = new ushort[checked((int)(numStates * 0x300))]; + } + } + + private void InitFastModel() + { + const ushort probInit = (ushort)(RangeCoder.BitDecoder.K_BIT_MODEL_TOTAL >> 1); + Array.Fill(_fIsMatch, probInit); + Array.Fill(_fIsRep, probInit); + Array.Fill(_fIsRepG0, probInit); + Array.Fill(_fIsRepG1, probInit); + Array.Fill(_fIsRepG2, probInit); + Array.Fill(_fIsRep0Long, probInit); + Array.Fill(_fPosSlot, probInit); + Array.Fill(_fPosDecoders, probInit); + Array.Fill(_fPosAlign, probInit); + Array.Fill(_fLenProbs, probInit); + Array.Fill(_fRepLenProbs, probInit); + Array.Fill(_fLiteral, probInit); + } + + // Mutable range-coder state threaded through the decode helpers below as a single `ref` + // parameter (instead of one `ref` per field), pinned to the RangeCoder.Decoder's fast + // input buffer for the duration of one CodeFast call. Consumed byte count is batched into + // RangeCoder.Decoder._total only when the local buffer is refilled/at call exit, instead + // of touching that field on every single byte. + private unsafe struct FastRangeState + { + public uint Range; + public uint Code; + public byte* InBuf; + public int InPos; + public int InLen; + public long Consumed; + public RangeCoder.Decoder RangeDecoder; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public byte ReadByte() + { + if (InPos >= InLen) + { + RangeDecoder.FastBufferPos = InPos; + RangeDecoder.AddTotal(Consumed); + Consumed = 0; + RangeDecoder.RefillFast(); + InPos = RangeDecoder.FastBufferPos; + InLen = RangeDecoder.FastBufferLen; + } + Consumed++; + return InBuf[InPos++]; + } + } + + // Mutable dictionary/output-window state, pinned to the OutWindow's circular buffer for + // the duration of one CodeFast call. Mirrors dicPos/dic being plain locals in the + // reference 7-Zip C decoder instead of being re-read from the OutWindow object. + private unsafe struct FastOutState + { + public byte* Dic; + public int Pos; + public long Total; + public int WindowSize; + public long Limit; + public OutWindow OutWindow; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly byte GetByte(int distance) + { + var p = Pos - distance - 1; + if (p < 0) + { + p += WindowSize; + } + return Dic[p]; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void PutByte(byte value) + { + Dic[Pos++] = value; + Total++; + if (Pos >= WindowSize) + { + // Rare (once per dictionary-size worth of output): sync locals back, let + // OutWindow flush the pending bytes out to the underlying stream and wrap + // its position, then reload the (now wrapped) position. + OutWindow.FastPos = Pos; + OutWindow.FastTotal = Total; + OutWindow.FastFlush(); + Pos = OutWindow.FastPos; + } + } + + public void CopyBlock(int distance, int len) + { + var rem = len; + var p = (distance < Pos ? Pos : Pos + WindowSize) - distance - 1; + var targetSize = + (Pos < WindowSize && Total < Limit) ? (int)Math.Min(rem, Limit - Total) : 0; + var sizeUntilWindowEnd = Math.Min(WindowSize - Pos, WindowSize - p); + var sizeUntilOverlap = Math.Abs(p - Pos); + var fastSize = Math.Min(Math.Min(sizeUntilWindowEnd, sizeUntilOverlap), targetSize); + if (fastSize >= 2) + { + Buffer.MemoryCopy(Dic + p, Dic + Pos, fastSize, fastSize); + Pos += fastSize; + p += fastSize; + Total += fastSize; + if (Pos >= WindowSize) + { + OutWindow.FastPos = Pos; + OutWindow.FastTotal = Total; + OutWindow.FastFlush(); + Pos = OutWindow.FastPos; + } + rem -= fastSize; + } + while (rem > 0 && Pos < WindowSize && Total < Limit) + { + if (p >= WindowSize) + { + p = 0; + } + PutByte(Dic[p++]); + rem--; + } + OutWindow.SetPendingFast(distance, rem); + } + } + + internal unsafe bool CodeFast( + int dictionarySize, + OutWindow outWindow, + RangeCoder.Decoder rangeDecoder + ) + { + var dictionarySizeCheck = Math.Max(dictionarySize, 1); + + outWindow.CopyPending(); + + var dic = outWindow.FastBuffer; + var inBuf = rangeDecoder.FastBufferArray; + bool result; + + fixed (byte* pDic = dic) + fixed (byte* pIn = inBuf) + fixed ( + ushort* pIsMatch = _fIsMatch, + pIsRep = _fIsRep, + pIsRepG0 = _fIsRepG0, + pIsRepG1 = _fIsRepG1, + pIsRepG2 = _fIsRepG2, + pIsRep0Long = _fIsRep0Long, + pPosSlot = _fPosSlot, + pPosDecoders = _fPosDecoders, + pPosAlign = _fPosAlign, + pLenProbs = _fLenProbs, + pRepLenProbs = _fRepLenProbs, + pLiteral = _fLiteral + ) + { + var rs = new FastRangeState + { + Range = rangeDecoder._range, + Code = rangeDecoder._code, + InBuf = pIn, + InPos = rangeDecoder.FastBufferPos, + InLen = rangeDecoder.FastBufferLen, + Consumed = 0, + RangeDecoder = rangeDecoder, + }; + var os = new FastOutState + { + Dic = pDic, + Pos = outWindow.FastPos, + Total = outWindow.FastTotal, + WindowSize = outWindow.FastWindowSize, + Limit = outWindow.FastLimit, + OutWindow = outWindow, + }; + + result = false; + while (os.Pos < os.WindowSize && os.Total < os.Limit) + { + var posState = (uint)os.Total & _posStateMask; + var stateIndex = (int)_state._index; + // (stateIndex << K_NUM_POS_STATES_BITS_MAX) + posState is used both as the + // IsMatch index and (numerically identical) the IsRep0Long/"short rep" index. + var matchIndex = (int)((stateIndex << Base.K_NUM_POS_STATES_BITS_MAX) + posState); + + // prevByte/matchByte only depend on os.Pos/_rep0, both already fixed from the + // previous iteration - independent of the upcoming (unavoidably branchy, since + // it changes the whole loop-body shape) IsMatch dispatch below. Issuing these + // dictionary reads before that branch resolves lets their load latency overlap + // with the IsMatch bit-decode arithmetic instead of stalling right after the + // branch, where they'd otherwise sit on the critical path. (Tried hoisting the + // literal-tree base-index/root-probability computation the same way too, but it + // regressed ~1s - that computation is heavy enough that doing it unconditionally + // on every iteration, including non-literal ones, outweighs the latency-hiding + // benefit; kept lazy, computed only once IsMatch resolves to 0.) + var isCharState = _state.IsCharState(); + var prevByte = os.GetByte(0); + var matchByte = isCharState ? (byte)0 : os.GetByte((int)_rep0); + + // Split compare/update (mirrors ASM's IF_BIT_x_NOUP + deferred UPDATE_0/ + // UPDATE_1): the probability write only happens once we know which arm we're + // in, interleaved with that arm's own independent setup work below, instead of + // being on the decode's own critical path. + var probIsMatch = pIsMatch[matchIndex]; + var isMatchSymbol = DecodeBitFastNoUpdate(ref rs, probIsMatch, out var isMatchMask); + + if (isMatchSymbol == 0) + { + UpdateProbFast(pIsMatch + matchIndex, probIsMatch, isMatchMask); + var literalP = + pLiteral + (int)GetFastLiteralBaseIndex((uint)os.Total, prevByte); + var firstIndex = isCharState + ? 1u + : (uint)((((matchByte >> 7) & 1) + 1) << 8) + 1; + var firstProb = (uint)literalP[firstIndex]; + var b = isCharState + ? LiteralDecodeNormalFast(ref rs, literalP, firstProb) + : LiteralDecodeWithMatchByteFast( + ref rs, + literalP, + matchByte, + firstIndex, + firstProb + ); + os.PutByte(b); + _state.UpdateChar(); + continue; + } + + UpdateProbFast(pIsMatch + matchIndex, probIsMatch, isMatchMask); + + uint len; + if (DecodeBitFast(ref rs, pIsRep, stateIndex) == 1) + { + if (DecodeBitFast(ref rs, pIsRepG0, stateIndex) == 0) + { + if (DecodeBitFast(ref rs, pIsRep0Long, matchIndex) == 0) + { + _state.UpdateShortRep(); + os.PutByte(os.GetByte((int)_rep0)); + continue; + } + } + else + { + uint distance; + if (DecodeBitFast(ref rs, pIsRepG1, stateIndex) == 0) + { + distance = _rep1; + } + else + { + if (DecodeBitFast(ref rs, pIsRepG2, stateIndex) == 0) + { + distance = _rep2; + } + else + { + distance = _rep3; + _rep3 = _rep2; + } + _rep2 = _rep1; + } + _rep1 = _rep0; + _rep0 = distance; + } + len = LenDecodeFast(ref rs, pRepLenProbs, posState) + Base.K_MATCH_MIN_LEN; + _state.UpdateRep(); + } + else + { + _rep3 = _rep2; + _rep2 = _rep1; + _rep1 = _rep0; + len = Base.K_MATCH_MIN_LEN + LenDecodeFast(ref rs, pLenProbs, posState); + _state.UpdateMatch(); + var posSlot = BitTreeDecodeFast( + ref rs, + pPosSlot, + (int)(Base.GetLenToPosState(len) << Base.K_NUM_POS_SLOT_BITS), + Base.K_NUM_POS_SLOT_BITS + ); + if (posSlot >= Base.K_START_POS_MODEL_INDEX) + { + var numDirectBits = (int)((posSlot >> 1) - 1); + _rep0 = (2 | (posSlot & 1)) << numDirectBits; + if (posSlot < Base.K_END_POS_MODEL_INDEX) + { + _rep0 += BitTreeReverseDecodeFast( + ref rs, + pPosDecoders, + (int)(_rep0 - posSlot - 1), + numDirectBits + ); + } + else + { + _rep0 += + DecodeDirectBitsFast(ref rs, numDirectBits - Base.K_NUM_ALIGN_BITS) + << Base.K_NUM_ALIGN_BITS; + _rep0 += BitTreeReverseDecodeFast( + ref rs, + pPosAlign, + 0, + Base.K_NUM_ALIGN_BITS + ); + } + } + else + { + _rep0 = posSlot; + } + } + + if (_rep0 >= os.Total || _rep0 >= dictionarySizeCheck) + { + if (_rep0 == 0xFFFFFFFF) + { + result = true; + break; + } + rangeDecoder._range = rs.Range; + rangeDecoder._code = rs.Code; + rangeDecoder.FastBufferPos = rs.InPos; + rangeDecoder.AddTotal(rs.Consumed); + outWindow.FastPos = os.Pos; + outWindow.FastTotal = os.Total; + throw new DataErrorException(); + } + os.CopyBlock((int)_rep0, (int)len); + } + + rangeDecoder._range = rs.Range; + rangeDecoder._code = rs.Code; + rangeDecoder.FastBufferPos = rs.InPos; + rangeDecoder.AddTotal(rs.Consumed); + outWindow.FastPos = os.Pos; + outWindow.FastTotal = os.Total; + } + + return result; + } + + // Core of DecodeBitFast, factored out so the bit-tree/literal decoders below can pass in a + // probability that was already loaded (prefetched) by the *previous* tree level instead of + // loading it here, and can read back `mask` to select their own prefetched next-level + // probability without an extra branch. `probSlot` must point at the array slot the caller + // read `prob` from, for the write-back. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static unsafe uint DecodeBitFastCore( + ref FastRangeState rs, + ushort* probSlot, + uint prob, + out uint mask + ) + { + var range = rs.Range; + var bound = (range >> RangeCoder.BitDecoder.K_NUM_BIT_MODEL_TOTAL_BITS) * prob; + + // Branchless bit decode + probability update, mirroring 7-Zip's ASM decoder + // (Asm/x86/LzmaDecOpt.asm) rather than the reference C decoder's data-dependent + // `if`: both possible next-states are computed unconditionally and combined via an + // all-ones/all-zeros mask (the software equivalent of the ASM's cmovae/cmovb), + // instead of branching on a comparison whose outcome is close to 50/50 for + // well-compressed data and is therefore poorly predicted by the CPU. + var symbol = rs.Code < bound ? 0u : 1u; + mask = (uint)-(int)symbol; // 0 for symbol 0, 0xFFFFFFFF for symbol 1 + + rs.Range = (bound & ~mask) | ((range - bound) & mask); + rs.Code -= bound & mask; + + // UPDATE_0 (symbol 0) is `prob + ((K_BIT_MODEL_TOTAL - prob) >> 5)`; UPDATE_1 + // (symbol 1) is `prob - (prob >> 5)`. KBitModelOffsetFast (31) is the standard LZMA + // bias that makes a single *arithmetic* (signed) shift reproduce UPDATE_1's + // logical-shift result from a target of 0, so both formulas collapse into one + // branchless expression selected by the same mask used above. + int target = (int)( + (RangeCoder.BitDecoder.K_BIT_MODEL_TOTAL & ~mask) | (uint)(KBitModelOffsetFast & mask) + ); + *probSlot = (ushort)((int)prob + ((target - (int)prob) >> KNumMoveBitsFast)); + + if (rs.Range < RangeCoder.Decoder.K_TOP_VALUE) + { + rs.Range <<= 8; + rs.Code = (rs.Code << 8) | rs.ReadByte(); + } + return symbol; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static unsafe uint DecodeBitFast(ref FastRangeState rs, ushort* probs, int index) => + DecodeBitFastCore(ref rs, probs + index, probs[index], out _); + + // Split form of DecodeBitFastCore used only for the outer dispatch bit (IsMatch): mirrors + // the ASM's IF_BIT_x_NOUP / UPDATE_0 / UPDATE_1 split, where the probability-model update is + // deferred past the (unavoidable, since literal vs. match are wholly different code) branch + // so it can be interleaved with independent work in each arm instead of sitting on the + // decode's own critical path. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint DecodeBitFastNoUpdate(ref FastRangeState rs, uint prob, out uint mask) + { + var range = rs.Range; + var bound = (range >> RangeCoder.BitDecoder.K_NUM_BIT_MODEL_TOTAL_BITS) * prob; + var symbol = rs.Code < bound ? 0u : 1u; + mask = (uint)-(int)symbol; + rs.Range = (bound & ~mask) | ((range - bound) & mask); + rs.Code -= bound & mask; + if (rs.Range < RangeCoder.Decoder.K_TOP_VALUE) + { + rs.Range <<= 8; + rs.Code = (rs.Code << 8) | rs.ReadByte(); + } + return symbol; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static unsafe void UpdateProbFast(ushort* probSlot, uint prob, uint mask) + { + int target = (int)( + (RangeCoder.BitDecoder.K_BIT_MODEL_TOTAL & ~mask) | (uint)(KBitModelOffsetFast & mask) + ); + *probSlot = (ushort)((int)prob + ((target - (int)prob) >> KNumMoveBitsFast)); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static unsafe uint BitTreeDecodeFast( + ref FastRangeState rs, + ushort* probs, + int baseIndex, + int numBits + ) + { + var p = probs + baseIndex; + uint m = 1; + // The very first probability must still be loaded fresh; from then on `prob` always + // arrives pre-loaded from the previous level's prefetch below. + var prob = (uint)p[1]; + for (var i = 0; i < numBits; i++) + { + var child0 = m << 1; + // Prefetch BOTH possible next-level probabilities now: which child we'll actually + // need depends on the bit we're about to decode, but the child *indices* only + // depend on `m`, which is already known - so these loads can proceed in parallel + // with (instead of only after) the branchless decode below, hiding their latency. + var hasNext = i + 1 < numBits; + uint probChild0 = 0, + probChild1 = 0; + if (hasNext) + { + probChild0 = p[child0]; + probChild1 = p[child0 + 1]; + } + + var bit = DecodeBitFastCore(ref rs, p + m, prob, out var mask); + + m = child0 + bit; + if (hasNext) + { + prob = (probChild0 & ~mask) | (probChild1 & mask); + } + } + return m - ((uint)1 << numBits); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static unsafe uint BitTreeReverseDecodeFast( + ref FastRangeState rs, + ushort* probs, + int baseIndex, + int numBits + ) + { + var p = probs + baseIndex; + uint m = 1; + var prob = (uint)p[1]; + uint symbol = 0; + for (var i = 0; i < numBits; i++) + { + var child0 = m << 1; + var hasNext = i + 1 < numBits; + uint probChild0 = 0, + probChild1 = 0; + if (hasNext) + { + probChild0 = p[child0]; + probChild1 = p[child0 + 1]; + } + + var bit = DecodeBitFastCore(ref rs, p + m, prob, out var mask); + symbol |= bit << i; + + m = child0 + bit; + if (hasNext) + { + prob = (probChild0 & ~mask) | (probChild1 & mask); + } + } + return symbol; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint DecodeDirectBitsFast(ref FastRangeState rs, int numTotalBits) + { + uint result = 0; + for (var i = numTotalBits; i > 0; i--) + { + rs.Range >>= 1; + var t = (rs.Code - rs.Range) >> 31; + rs.Code -= rs.Range & (t - 1); + result = (result << 1) | (1 - t); + if (rs.Range < RangeCoder.Decoder.K_TOP_VALUE) + { + rs.Code = (rs.Code << 8) | rs.ReadByte(); + rs.Range <<= 8; + } + } + return result; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static unsafe uint LenDecodeFast(ref FastRangeState rs, ushort* probs, uint posState) + { + if (DecodeBitFast(ref rs, probs, LenChoiceIndex) == 0) + { + return BitTreeDecodeFast( + ref rs, + probs, + LenLowBase + (int)posState * LenLowStride, + Base.K_NUM_LOW_LEN_BITS + ); + } + var symbol = Base.K_NUM_LOW_LEN_SYMBOLS; + if (DecodeBitFast(ref rs, probs, LenChoice2Index) == 0) + { + symbol += BitTreeDecodeFast( + ref rs, + probs, + LenMidBase + (int)posState * LenMidStride, + Base.K_NUM_MID_LEN_BITS + ); + } + else + { + symbol += Base.K_NUM_MID_LEN_SYMBOLS; + symbol += BitTreeDecodeFast(ref rs, probs, LenHighBase, Base.K_NUM_HIGH_LEN_BITS); + } + return symbol; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private uint GetFastLiteralBaseIndex(uint pos, byte prevByte) => + ( + ((pos & _fLiteralPosMask) << _fLiteralNumPrevBits) + + (uint)(prevByte >> (8 - _fLiteralNumPrevBits)) + ) * 0x300; + + // baseP/firstProb (root of the literal tree, index 1) are precomputed by the caller - + // pos/prevByte are already known before the IsMatch decode resolves, so hoisting this load + // out lets it overlap with the IsMatch bit-decode instead of stalling right after it. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static unsafe byte LiteralDecodeNormalFast( + ref FastRangeState rs, + ushort* baseP, + uint firstProb + ) + { + var p = baseP; + // Same one-level-ahead child-probability prefetch as BitTreeDecodeFast (see there), + // applied to the hottest call site in the decoder: one 8-bit literal-tree decode per + // literal byte. Manually unrolled (mirroring the ASM's explicit BIT_0/BIT_1x7 + // sequence, as opposed to a loop) because RyuJIT does not inline methods containing + // a loop - leaving this as a `for` loop, even with AggressiveInlining, forces this + // call to remain a real call boundary, which makes `rs` address-exposed and prevents + // the JIT from keeping Range/Code in registers for the rest of the CodeFast loop. + uint m = 1; + var prob = firstProb; + + var child0 = m << 1; + var probChild0 = p[child0]; + var probChild1 = p[child0 + 1]; + var bit = DecodeBitFastCore(ref rs, p + m, prob, out var mask); + m = child0 + bit; + prob = (probChild0 & ~mask) | (probChild1 & mask); + + child0 = m << 1; + probChild0 = p[child0]; + probChild1 = p[child0 + 1]; + bit = DecodeBitFastCore(ref rs, p + m, prob, out mask); + m = child0 + bit; + prob = (probChild0 & ~mask) | (probChild1 & mask); + + child0 = m << 1; + probChild0 = p[child0]; + probChild1 = p[child0 + 1]; + bit = DecodeBitFastCore(ref rs, p + m, prob, out mask); + m = child0 + bit; + prob = (probChild0 & ~mask) | (probChild1 & mask); + + child0 = m << 1; + probChild0 = p[child0]; + probChild1 = p[child0 + 1]; + bit = DecodeBitFastCore(ref rs, p + m, prob, out mask); + m = child0 + bit; + prob = (probChild0 & ~mask) | (probChild1 & mask); + + child0 = m << 1; + probChild0 = p[child0]; + probChild1 = p[child0 + 1]; + bit = DecodeBitFastCore(ref rs, p + m, prob, out mask); + m = child0 + bit; + prob = (probChild0 & ~mask) | (probChild1 & mask); + + child0 = m << 1; + probChild0 = p[child0]; + probChild1 = p[child0 + 1]; + bit = DecodeBitFastCore(ref rs, p + m, prob, out mask); + m = child0 + bit; + prob = (probChild0 & ~mask) | (probChild1 & mask); + + child0 = m << 1; + probChild0 = p[child0]; + probChild1 = p[child0 + 1]; + bit = DecodeBitFastCore(ref rs, p + m, prob, out mask); + m = child0 + bit; + prob = (probChild0 & ~mask) | (probChild1 & mask); + + child0 = m << 1; + bit = DecodeBitFastCore(ref rs, p + m, prob, out mask); + m = child0 + bit; + + return (byte)m; + } + + // Shared plain-literal-tree tail used once a matched-literal decode diverges from + // matchByte: from that point on there's no more matched-zone offset, so this is the same + // decode as LiteralDecodeNormalFast's inner step, just resuming mid-tree from `symbol`. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static unsafe byte LiteralDecodeTailFast(ref FastRangeState rs, ushort* p, uint symbol) + { + while (symbol < 0x100) + { + var bit = DecodeBitFastCore(ref rs, p + symbol, p[symbol], out _); + symbol = (symbol << 1) | bit; + } + return (byte)symbol; + } + + // baseP/firstIndex/firstProb are precomputed by the caller for the same reason as in + // LiteralDecodeNormalFast above (pos/prevByte/matchByte are all already known before the + // IsMatch decode resolves). + private static unsafe byte LiteralDecodeWithMatchByteFast( + ref FastRangeState rs, + ushort* baseP, + byte matchByte, + uint firstIndex, + uint firstProb + ) + { + var p = baseP; + + // Same one-level-ahead prefetch as BitTreeDecodeFast, adapted for the matched-literal + // zone: matchByte's bits are all known upfront (it comes from the dictionary, not from + // decoding), so the *next* iteration's matched-zone index can be computed from it + // immediately, independent of this iteration's decoded bit. + uint symbol = 1; + var matchBit = (uint)(matchByte >> 7) & 1; + matchByte <<= 1; + var index = (int)firstIndex; + var prob = firstProb; + + for (var i = 0; i < 8; i++) + { + var nextMatchBit = (uint)(matchByte >> 7) & 1; + matchByte <<= 1; + var hasNext = i < 7; + + var child0 = (uint)((1 + nextMatchBit) << 8) + (symbol << 1); + uint probChild0 = 0, + probChild1 = 0; + if (hasNext) + { + probChild0 = p[child0]; + probChild1 = p[child0 + 1]; + } + + var bit = DecodeBitFastCore(ref rs, p + index, prob, out var mask); + symbol = (symbol << 1) | bit; + + if (matchBit != bit) + { + return LiteralDecodeTailFast(ref rs, p, symbol); + } + + matchBit = nextMatchBit; + if (hasNext) + { + index = (int)(child0 + bit); + prob = (probChild0 & ~mask) | (probChild1 & mask); + } + } + return (byte)symbol; + } +} +#endif diff --git a/src/SharpCompress/Compressors/LZMA/LzmaDecoder.cs b/src/SharpCompress/Compressors/LZMA/LzmaDecoder.cs index 02fd16d7..60ac6a00 100644 --- a/src/SharpCompress/Compressors/LZMA/LzmaDecoder.cs +++ b/src/SharpCompress/Compressors/LZMA/LzmaDecoder.cs @@ -331,6 +331,12 @@ public partial class Decoder : ICoder, ISetDecoderProperties, IDisposable _outWindow = null; } +#if !LEGACY_DOTNET + // On modern .NET targets, the hot decode loop uses the unsafe flat-array fast path + // (see LzmaDecoder.Fast.cs) instead of the per-array, method-call based loop below. + internal bool Code(int dictionarySize, OutWindow outWindow, RangeCoder.Decoder rangeDecoder) => + CodeFast(dictionarySize, outWindow, rangeDecoder); +#else internal bool Code(int dictionarySize, OutWindow outWindow, RangeCoder.Decoder rangeDecoder) { var dictionarySizeCheck = Math.Max(dictionarySize, 1); @@ -456,6 +462,7 @@ public partial class Decoder : ICoder, ISetDecoderProperties, IDisposable } return false; } +#endif public void SetDecoderProperties(byte[] properties) => SetDecoderProperties(properties.AsSpan()); @@ -477,6 +484,10 @@ public partial class Decoder : ICoder, ISetDecoderProperties, IDisposable SetLiteralProperties(lp, lc); SetPosBitsProperties(pb); Init(); +#if !LEGACY_DOTNET + CreateFastModel(lp, lc); + InitFastModel(); +#endif if (properties.Length >= 5) { _dictionarySize = 0; diff --git a/src/SharpCompress/Compressors/LZMA/LzmaStream.cs b/src/SharpCompress/Compressors/LZMA/LzmaStream.cs index f45b33a3..7f296a15 100644 --- a/src/SharpCompress/Compressors/LZMA/LzmaStream.cs +++ b/src/SharpCompress/Compressors/LZMA/LzmaStream.cs @@ -131,6 +131,13 @@ public partial class LzmaStream : Stream, IStreamStack, IAsyncDisposable } lzma._rangeDecoder.Init(inputStream); +#if !LEGACY_DOTNET + // Bound the fast buffered reader to the known compressed size (when available) so + // it never reads past this entry's data even on unbounded/shared streams. When the + // size is unknown (e.g. Zip data-descriptor entries), RangeCoder.Decoder falls back + // to conservative per-byte reads on streams that cannot report a safe Length. + lzma._rangeDecoder.SetFastLimit(lzma._rangeDecoderLimit); +#endif } else { @@ -474,6 +481,13 @@ public partial class LzmaStream : Stream, IStreamStack, IAsyncDisposable } _rangeDecoder.Init(_inputStream); +#if !LEGACY_DOTNET + // LZMA2 chunks share one underlying stream with the raw chunk-header bytes read + // above/below, so the buffered fast-read path (see RangeCoder.Decoder's fast + // buffer) must never physically read past this chunk's compressed size, or it + // would desynchronize the stream position for the next chunk header. + _rangeDecoder.SetFastLimit(_rangeDecoderLimit); +#endif } else if (control > 0x02) { diff --git a/src/SharpCompress/Compressors/LZMA/RangeCoder/RangeCoder.cs b/src/SharpCompress/Compressors/LZMA/RangeCoder/RangeCoder.cs index 147146c4..141b1dbd 100644 --- a/src/SharpCompress/Compressors/LZMA/RangeCoder/RangeCoder.cs +++ b/src/SharpCompress/Compressors/LZMA/RangeCoder/RangeCoder.cs @@ -1,7 +1,11 @@ #nullable disable +using System; using System.IO; using System.Runtime.CompilerServices; +#if !LEGACY_DOTNET +using System.Buffers; +#endif namespace SharpCompress.Compressors.LZMA.RangeCoder; @@ -89,6 +93,33 @@ internal partial class Decoder public long _total; private byte[] _singleByteBuffer; +#if !LEGACY_DOTNET + // Upper bound (in terms of _total) that the fast buffered reader is allowed to physically + // read up to. -1 means unbounded. This matters for formats like LZMA2 where multiple + // independent chunks share one underlying stream: chunk headers are read directly from that + // stream between decode sessions, so the fast path must not read past the end of the + // current chunk's compressed data, or it would desynchronize the stream position for the + // next chunk header read. Set via SetFastLimit once the caller knows the chunk/stream size. + private long _fastLimit = -1; + + // Whether it is safe to bulk-read ahead of the decoder even without a known _fastLimit. + // This is only true for streams that are guaranteed to self-clamp Read() to their own + // logical end and never return bytes that belong to something else - e.g. 7Zip's + // per-folder BufferedSubStream, which limits every Read() to its own remaining pack size. + // It is false for everything else, notably a shared streaming Zip reader stream with an + // unknown compressed size (data-descriptor entries): that stream keeps handing out bytes + // past the logical end of this LZMA stream (the next entry's header, etc.) with no self + // clamping and no way to give unread bytes back, so bulk-buffering there would + // desynchronize the stream. Note a stream reporting a queryable Length is NOT a reliable + // signal here: some wrapper streams (e.g. SharpCompressStream's ring-buffer mode used for + // over-read recording on non-seekable Zip streams) expose a Length without actually + // bounding Read() to the current logical stream's end. In the unsafe case we fall back to + // reading exactly one byte at a time, matching the legacy per-byte ReadByte() behavior. + private bool _fastBufferSafeUnbounded; + + public void SetFastLimit(long limit) => _fastLimit = limit; +#endif + public void Init(Stream stream) { _stream = stream; @@ -100,11 +131,105 @@ internal partial class Decoder _code = (_code << 8) | (byte)_stream.ReadByte(); } _total = 5; +#if !LEGACY_DOTNET + _fastLimit = -1; + _fastBufferPos = 0; + _fastBufferLen = 0; + _fastEndOfStream = false; + _fastBufferSafeUnbounded = stream is SharpCompress.IO.BufferedSubStream; +#endif } - public void ReleaseStream() => + public void ReleaseStream() + { +#if !LEGACY_DOTNET + ReleaseFastBuffer(); +#endif // Stream.ReleaseStream(); _stream = null; + } + +#if !LEGACY_DOTNET + // Buffered input used only by the unsafe fast LZMA decode path (see LzmaDecoder.Fast.cs). + // Avoids issuing a virtual Stream.ReadByte() call per consumed byte, which otherwise + // dominates decode time (millions of calls for a typical archive). + private const int FastBufferSize = 1 << 16; + private byte[] _fastBuffer; + private int _fastBufferPos; + private int _fastBufferLen; + private bool _fastEndOfStream; + + // The fast decode loop (LzmaDecoder.Fast.cs) pins this array and keeps its own local + // copies of position/length/consumed-byte-count for the duration of a decode call, only + // syncing back through these accessors when the local buffer is exhausted (rare) or when + // the decode call ends. This avoids reloading these fields from this object on every bit. + internal byte[] FastBufferArray => _fastBuffer ??= ArrayPool.Shared.Rent(FastBufferSize); + + internal int FastBufferPos + { + get => _fastBufferPos; + set => _fastBufferPos = value; + } + + internal int FastBufferLen => _fastBufferLen; + + internal void AddTotal(long consumed) => _total += consumed; + + internal void RefillFast() => FillFastBuffer(); + + private void FillFastBuffer() + { + _fastBuffer ??= ArrayPool.Shared.Rent(FastBufferSize); + if (_fastEndOfStream) + { + // Match legacy (byte)_stream.ReadByte() behavior: once the stream is + // exhausted, keep yielding 0xFF instead of throwing or re-reading. + _fastBufferPos = 0; + _fastBufferLen = 1; + _fastBuffer[0] = 0xFF; + return; + } + var requestSize = _fastBuffer.Length; + if (_fastLimit >= 0) + { + var remaining = _fastLimit - _total; + requestSize = remaining <= 0 ? 1 : (int)Math.Min(requestSize, remaining); + } + else if (!_fastBufferSafeUnbounded) + { + // No known limit and the underlying stream cannot report a bounded Length (e.g. a + // shared, forward-only stream such as a streaming Zip reader). Reading ahead here + // could consume bytes that belong to whatever comes after this logical LZMA stream + // (the next Zip entry's header, etc.) with no way to give them back. Fall back to + // requesting exactly one byte at a time so we never consume more than the decoder + // actually needs, matching the legacy per-byte ReadByte() behavior. + requestSize = 1; + } + var read = _stream.Read(_fastBuffer, 0, requestSize); + if (read <= 0) + { + _fastEndOfStream = true; + _fastBufferPos = 0; + _fastBufferLen = 1; + _fastBuffer[0] = 0xFF; + return; + } + _fastBufferPos = 0; + _fastBufferLen = read; + } + + private void ReleaseFastBuffer() + { + if (_fastBuffer is not null) + { + ArrayPool.Shared.Return(_fastBuffer); + _fastBuffer = null; + } + _fastBufferPos = 0; + _fastBufferLen = 0; + _fastEndOfStream = false; + } +#endif public void Normalize() { diff --git a/src/SharpCompress/Readers/ReaderOptions.cs b/src/SharpCompress/Readers/ReaderOptions.cs index aced7169..3d9028b2 100644 --- a/src/SharpCompress/Readers/ReaderOptions.cs +++ b/src/SharpCompress/Readers/ReaderOptions.cs @@ -143,6 +143,13 @@ public sealed record ReaderOptions : IReaderOptions public CompressionProviderRegistry Providers { get; set; } = CompressionProviderRegistry.Default; + /// + /// When true, opts in to a format's optional parallel decode (e.g. 7-Zip's automatic + /// parallel LZMA2 solid-folder decode). Formats that do not implement parallel decoding + /// ignore this setting. Default is false (sequential decode). + /// + public bool EnableParallelism { get; set; } + /// /// Creates a new ReaderOptions instance with default values. /// diff --git a/src/SharpCompress/Readers/ReaderOptionsExtensions.cs b/src/SharpCompress/Readers/ReaderOptionsExtensions.cs index 2415b544..0e3628ab 100644 --- a/src/SharpCompress/Readers/ReaderOptionsExtensions.cs +++ b/src/SharpCompress/Readers/ReaderOptionsExtensions.cs @@ -86,6 +86,14 @@ public static class ReaderOptionsExtensions int? rewindableBufferSize ) => options with { RewindableBufferSize = rewindableBufferSize }; + /// + /// Creates a copy with the specified EnableParallelism value. + /// + public static ReaderOptions WithEnableParallelism( + this ReaderOptions options, + bool enableParallelism + ) => options with { EnableParallelism = enableParallelism }; + /// /// Creates a copy with the specified compression provider registry. /// diff --git a/src/SharpCompress/Writers/GZip/GZipWriterOptions.cs b/src/SharpCompress/Writers/GZip/GZipWriterOptions.cs index ca1b6a9d..e6c4b3bb 100644 --- a/src/SharpCompress/Writers/GZip/GZipWriterOptions.cs +++ b/src/SharpCompress/Writers/GZip/GZipWriterOptions.cs @@ -81,6 +81,12 @@ public sealed record GZipWriterOptions : IWriterOptions public CompressionProviderRegistry Providers { get; set; } = CompressionProviderRegistry.Default; + /// + /// When true, opts in to a format's optional parallel encode. No writer currently implements + /// parallel encoding; reserved for future use. Default is false. + /// + public bool EnableParallelism { get; set; } + /// /// Creates a new GZipWriterOptions instance with default values. /// diff --git a/src/SharpCompress/Writers/SevenZip/SevenZipWriterOptions.cs b/src/SharpCompress/Writers/SevenZip/SevenZipWriterOptions.cs index 6ed26a42..2a286434 100644 --- a/src/SharpCompress/Writers/SevenZip/SevenZipWriterOptions.cs +++ b/src/SharpCompress/Writers/SevenZip/SevenZipWriterOptions.cs @@ -69,6 +69,12 @@ public sealed record SevenZipWriterOptions : IWriterOptions public CompressionProviderRegistry Providers { get; set; } = CompressionProviderRegistry.Default; + /// + /// When true, opts in to a format's optional parallel encode. No writer currently implements + /// parallel encoding; reserved for future use. Default is false. + /// + public bool EnableParallelism { get; set; } + /// /// Whether to compress the archive header itself using LZMA. /// Default is true, matching standard 7-Zip behavior. diff --git a/src/SharpCompress/Writers/Tar/TarWriterOptions.cs b/src/SharpCompress/Writers/Tar/TarWriterOptions.cs index d7aaf22b..369df364 100755 --- a/src/SharpCompress/Writers/Tar/TarWriterOptions.cs +++ b/src/SharpCompress/Writers/Tar/TarWriterOptions.cs @@ -56,6 +56,12 @@ public sealed record TarWriterOptions : IWriterOptions public CompressionProviderRegistry Providers { get; set; } = CompressionProviderRegistry.Default; + /// + /// When true, opts in to a format's optional parallel encode. No writer currently implements + /// parallel encoding; reserved for future use. Default is false. + /// + public bool EnableParallelism { get; set; } + /// /// Indicates if archive should be finalized (by 2 empty blocks) on close. /// diff --git a/src/SharpCompress/Writers/WriterOptions.cs b/src/SharpCompress/Writers/WriterOptions.cs index d70ea6aa..0b70df88 100644 --- a/src/SharpCompress/Writers/WriterOptions.cs +++ b/src/SharpCompress/Writers/WriterOptions.cs @@ -69,6 +69,12 @@ public sealed record WriterOptions : IWriterOptions public CompressionProviderRegistry Providers { get; set; } = CompressionProviderRegistry.Default; + /// + /// When true, opts in to a format's optional parallel encode. No writer currently implements + /// parallel encoding; reserved for future use. Default is false. + /// + public bool EnableParallelism { get; set; } + /// /// Creates a new WriterOptions instance with the specified compression type. /// Compression level is automatically set based on the compression type. diff --git a/src/SharpCompress/Writers/WriterOptionsExtensions.cs b/src/SharpCompress/Writers/WriterOptionsExtensions.cs index 2aca567a..c7689c00 100644 --- a/src/SharpCompress/Writers/WriterOptionsExtensions.cs +++ b/src/SharpCompress/Writers/WriterOptionsExtensions.cs @@ -73,6 +73,14 @@ public static class WriterOptionsExtensions int compressionLevel ) => options with { CompressionLevel = compressionLevel }; + /// + /// Creates a copy with the specified EnableParallelism value. + /// + public static WriterOptions WithEnableParallelism( + this WriterOptions options, + bool enableParallelism + ) => options with { EnableParallelism = enableParallelism }; + /// /// Creates a copy with the specified archive encoding. /// diff --git a/src/SharpCompress/Writers/Zip/ZipWriterOptions.cs b/src/SharpCompress/Writers/Zip/ZipWriterOptions.cs index 73c92ee6..fc7d613b 100644 --- a/src/SharpCompress/Writers/Zip/ZipWriterOptions.cs +++ b/src/SharpCompress/Writers/Zip/ZipWriterOptions.cs @@ -73,6 +73,12 @@ public sealed record ZipWriterOptions : IWriterOptions public CompressionProviderRegistry Providers { get; set; } = CompressionProviderRegistry.Default; + /// + /// When true, opts in to a format's optional parallel encode. No writer currently implements + /// parallel encoding; reserved for future use. Default is false. + /// + public bool EnableParallelism { get; set; } + /// /// Optional comment for the archive. /// diff --git a/tests/SharpCompress.Test/SevenZip/SevenZipArchiveParallelTests.cs b/tests/SharpCompress.Test/SevenZip/SevenZipArchiveParallelTests.cs new file mode 100644 index 00000000..bdfb5b67 --- /dev/null +++ b/tests/SharpCompress.Test/SevenZip/SevenZipArchiveParallelTests.cs @@ -0,0 +1,310 @@ +using System.IO; +using System.Linq; +using SharpCompress.Archives; +using SharpCompress.Archives.SevenZip; +using SharpCompress.Common; +using SharpCompress.Common.SevenZip; +using SharpCompress.Factories; +using SharpCompress.Readers; +using Xunit; + +namespace SharpCompress.Test.SevenZip; + +// Parallel counterpart of SevenZipArchiveTests: every test here opts in via +// ReaderOptions.EnableParallelism (default is sequential decode), covering the same archives to +// verify decoded content is identical regardless of which decode path 7-Zip's automatic parallel +// LZMA2 solid-folder decode takes. +public class SevenZipArchiveParallelTests : ArchiveTests +{ + private static ReaderOptions Parallel(ReaderOptions options) => + options.WithEnableParallelism(true); + + [Fact] + public void SevenZipArchive_Solid_StreamRead_Parallel() => + ArchiveStreamRead("7Zip.solid.7z", Parallel(ReaderOptions.ForExternalStream)); + + [Fact] + public void SevenZipArchive_NonSolid_StreamRead_Parallel() => + ArchiveStreamRead("7Zip.nonsolid.7z", Parallel(ReaderOptions.ForExternalStream)); + + [Fact] + public void SevenZipArchive_LZMA_StreamRead_Parallel() => + ArchiveStreamRead("7Zip.LZMA.7z", Parallel(ReaderOptions.ForExternalStream)); + + [Fact] + public void SevenZipArchive_LZMA_PathRead_Parallel() => + ArchiveFileRead("7Zip.LZMA.7z", Parallel(ReaderOptions.ForFilePath)); + + [Fact] + public void SevenZipArchive_LZMAAES_StreamRead_Parallel() => + ArchiveStreamRead( + "7Zip.LZMA.Aes.7z", + Parallel(ReaderOptions.ForExternalStream) with + { + Password = "testpassword", + } + ); + + [Fact] + public void SevenZipArchive_LZMAAES_PathRead_Parallel() => + ArchiveFileRead( + "7Zip.LZMA.Aes.7z", + Parallel(ReaderOptions.ForFilePath) with + { + Password = "testpassword", + } + ); + + [Fact] + public void SevenZipArchive_PPMd_StreamRead_Parallel() => + ArchiveStreamRead("7Zip.PPMd.7z", Parallel(ReaderOptions.ForExternalStream)); + + [Fact] + public void SevenZipArchive_PPMd_PathRead_Parallel() => + ArchiveFileRead("7Zip.PPMd.7z", Parallel(ReaderOptions.ForFilePath)); + + [Fact] + public void SevenZipArchive_LZMA2_StreamRead_Parallel() => + ArchiveStreamRead("7Zip.LZMA2.7z", Parallel(ReaderOptions.ForExternalStream)); + + [Fact] + public void SevenZipArchive_LZMA2_PathRead_Parallel() => + ArchiveFileRead("7Zip.LZMA2.7z", Parallel(ReaderOptions.ForFilePath)); + + [Fact] + public void SevenZipArchive_LZMA2_EXE_StreamRead_Parallel() => + ArchiveStreamRead( + new SevenZipFactory(), + "7Zip.LZMA2.exe", + Parallel(ReaderOptions.ForExternalStream).WithLookForHeader(true) + ); + + [Fact] + public void SevenZipArchive_LZMA2_EXE_PathRead_Parallel() => + ArchiveFileRead( + "7Zip.LZMA2.exe", + Parallel(ReaderOptions.ForFilePath).WithLookForHeader(true), + new SevenZipFactory() + ); + + [Fact] + public void SevenZipArchive_LZMA2AES_StreamRead_Parallel() => + ArchiveStreamRead( + "7Zip.LZMA2.Aes.7z", + Parallel(ReaderOptions.ForExternalStream) with + { + Password = "testpassword", + } + ); + + [Fact] + public void SevenZipArchive_LZMA2AES_PathRead_Parallel() => + ArchiveFileRead( + "7Zip.LZMA2.Aes.7z", + Parallel(ReaderOptions.ForFilePath) with + { + Password = "testpassword", + } + ); + + [Fact] + public void SevenZipArchive_BZip2_StreamRead_Parallel() => + ArchiveStreamRead("7Zip.BZip2.7z", Parallel(ReaderOptions.ForExternalStream)); + + [Fact] + public void SevenZipArchive_BZip2_PathRead_Parallel() => + ArchiveFileRead("7Zip.BZip2.7z", Parallel(ReaderOptions.ForFilePath)); + + [Fact] + public void SevenZipArchive_Copy_StreamRead_Parallel() => + ArchiveStreamRead("7Zip.Copy.7z", Parallel(ReaderOptions.ForExternalStream)); + + [Fact] + public void SevenZipArchive_Copy_PathRead_Parallel() => + ArchiveFileRead("7Zip.Copy.7z", Parallel(ReaderOptions.ForFilePath)); + + [Fact] + public void SevenZipArchive_Copy_CompressionType_Parallel() + { + using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "7Zip.Copy.7z"))) + using ( + var archive = SevenZipArchive.OpenArchive( + stream, + Parallel(ReaderOptions.ForExternalStream) + ) + ) + { + foreach (var entry in archive.Entries.Where(e => !e.IsDirectory)) + { + Assert.Equal(CompressionType.None, entry.CompressionType); + } + } + } + + [Fact] + public void SevenZipArchive_ZSTD_StreamRead_Parallel() => + ArchiveStreamRead("7Zip.ZSTD.7z", Parallel(ReaderOptions.ForExternalStream)); + + [Fact] + public void SevenZipArchive_ZSTD_PathRead_Parallel() => + ArchiveFileRead("7Zip.ZSTD.7z", Parallel(ReaderOptions.ForFilePath)); + + [Fact] + public void SevenZipArchive_ZSTD_Split_Parallel() => + ArchiveStreamMultiRead( + Parallel(ReaderOptions.ForFilePath), + "7Zip.ZSTD.Split.7z.001", + "7Zip.ZSTD.Split.7z.002", + "7Zip.ZSTD.Split.7z.003", + "7Zip.ZSTD.Split.7z.004", + "7Zip.ZSTD.Split.7z.005", + "7Zip.ZSTD.Split.7z.006" + ); + + [Fact] + public void SevenZipArchive_EOS_FileRead_Parallel() => + ArchiveFileRead("7Zip.eos.7z", Parallel(ReaderOptions.ForFilePath)); + + [Fact] + public void SevenZipArchive_Delta_FileRead_Parallel() => + ArchiveFileRead("7Zip.delta.7z", Parallel(ReaderOptions.ForFilePath)); + + [Fact] + public void SevenZipArchive_ARM_FileRead_Parallel() => + ArchiveFileRead("7Zip.ARM.7z", Parallel(ReaderOptions.ForFilePath)); + + [Fact] + public void SevenZipArchive_ARMT_FileRead_Parallel() => + ArchiveFileRead("7Zip.ARMT.7z", Parallel(ReaderOptions.ForFilePath)); + + [Fact] + public void SevenZipArchive_BCJ_FileRead_Parallel() => + ArchiveFileRead("7Zip.BCJ.7z", Parallel(ReaderOptions.ForFilePath)); + + [Fact] + public void SevenZipArchive_BCJ2_FileRead_Parallel() => + ArchiveFileRead("7Zip.BCJ2.7z", Parallel(ReaderOptions.ForFilePath)); + + [Fact] + public void SevenZipArchive_IA64_FileRead_Parallel() => + ArchiveFileRead("7Zip.IA64.7z", Parallel(ReaderOptions.ForFilePath)); + + [Fact] + public void SevenZipArchive_PPC_FileRead_Parallel() => + ArchiveFileRead("7Zip.PPC.7z", Parallel(ReaderOptions.ForFilePath)); + + [Fact] + public void SevenZipArchive_SPARC_FileRead_Parallel() => + ArchiveFileRead("7Zip.SPARC.7z", Parallel(ReaderOptions.ForFilePath)); + + [Fact] + public void SevenZipArchive_ARM64_FileRead_Parallel() => + ArchiveFileRead("7Zip.ARM64.7z", Parallel(ReaderOptions.ForFilePath)); + + [Fact] + public void SevenZipArchive_RISCV_FileRead_Parallel() => + ArchiveFileRead("7Zip.RISCV.7z", Parallel(ReaderOptions.ForFilePath)); + + [Fact] + public void SevenZipArchive_Filters_FileRead_Parallel() => + ArchiveFileRead("7Zip.Filters.7z", Parallel(ReaderOptions.ForFilePath)); + + [Fact] + public void SevenZipArchive_Tar_PathRead_Parallel() + { + using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "7Zip.Tar.tar.7z"))) + using ( + var archive = SevenZipArchive.OpenArchive( + stream, + Parallel(ReaderOptions.ForExternalStream) + ) + ) + { + var entry = archive.Entries.First(); + entry.WriteToFile(Path.Combine(SCRATCH_FILES_PATH, entry.Key.NotNull())); + + var size = entry.Size; + var scratch = new FileInfo(Path.Combine(SCRATCH_FILES_PATH, "7Zip.Tar.tar")); + var test = new FileInfo(Path.Combine(TEST_ARCHIVES_PATH, "7Zip.Tar.tar")); + + Assert.Equal(size, scratch.Length); + Assert.Equal(size, test.Length); + } + + CompareArchivesByPath( + Path.Combine(SCRATCH_FILES_PATH, "7Zip.Tar.tar"), + Path.Combine(TEST_ARCHIVES_PATH, "7Zip.Tar.tar") + ); + } + + [Fact] + public void SevenZipArchive_Solid_ExtractAllEntries_Contiguous_Parallel() + { + // This test verifies that solid archives iterate entries as contiguous streams + // rather than recreating the decompression stream for each entry, with the parallel + // decode path opted in. + var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "7Zip.solid.7z"); + using var archive = SevenZipArchive.OpenArchive( + testArchive, + Parallel(ReaderOptions.ForFilePath) + ); + Assert.True(archive.IsSolid); + + using var reader = archive.ExtractAllEntries(); + while (reader.MoveToNextEntry()) + { + if (!reader.Entry.IsDirectory) + { + reader.WriteEntryToDirectory(SCRATCH_FILES_PATH); + } + } + + VerifyFiles(); + } + + [Fact] + public void SevenZipArchive_EmptyStream_WriteToDirectory_Parallel() + { + // This test specifically verifies that archives with empty-stream entries + // (files with size 0 and no compressed data) can be extracted without throwing + // NullReferenceException, with the parallel decode path opted in. + var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "7Zip.EmptyStream.7z"); + using var archive = SevenZipArchive.OpenArchive( + testArchive, + Parallel(ReaderOptions.ForFilePath) + ); + + var emptyStreamFileCount = 0; + foreach (var entry in archive.Entries) + { + if (!entry.IsDirectory) + { + var sevenZipEntry = entry as SevenZipEntry; + if (sevenZipEntry?.FilePart.Header.HasStream == false) + { + emptyStreamFileCount++; + } + + entry.WriteToDirectory(SCRATCH_FILES_PATH); + } + } + + Assert.True( + emptyStreamFileCount > 0, + "Test archive should contain at least one empty-stream entry" + ); + + var extractedFiles = Directory.GetFiles( + SCRATCH_FILES_PATH, + "*", + SearchOption.AllDirectories + ); + Assert.NotEmpty(extractedFiles); + + foreach (var file in extractedFiles) + { + var fileInfo = new FileInfo(file); + Assert.Equal(0, fileInfo.Length); + } + } +} diff --git a/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs b/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs index 0fc86c3e..d1a07532 100644 --- a/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs +++ b/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs @@ -5,9 +5,11 @@ using SharpCompress.Archives; using SharpCompress.Archives.SevenZip; using SharpCompress.Common; using SharpCompress.Common.SevenZip; +using SharpCompress.Compressors.LZMA; using SharpCompress.Factories; using SharpCompress.Readers; using SharpCompress.Test.Mocks; +using SharpCompress.Writers.SevenZip; using Xunit; namespace SharpCompress.Test.SevenZip; @@ -445,4 +447,79 @@ public class SevenZipArchiveTests : ArchiveTests Assert.Equal(0, fileInfo.Length); } } + +#if !LEGACY_DOTNET + [Fact] + public void SevenZipArchive_EnableParallelism_OptInEngagesParallelDecodePath() + { + // Highly repetitive so it compresses well, and large enough that -- combined with the + // small dictionary below -- the encoder is forced to periodically reset the dictionary, + // producing multiple independently-decodable LZMA2 restart blocks (the only shape + // Lzma2ParallelDecoder can split across threads). + var payload = new byte[20_000_000]; + for (var i = 0; i < payload.Length; i++) + { + payload[i] = (byte)(i % 7); + } + + // A real (file-backed, seekable) archive is required: the parallel decode path only + // engages for a genuine FileStream, never for archives opened from in-memory streams. + var archivePath = Path.Combine(SCRATCH_FILES_PATH, "parallel-decode-optin.7z"); + var writerOptions = new SevenZipWriterOptions(CompressionType.LZMA2) + { + LzmaProperties = new LzmaEncoderProperties( + eos: false, + dictionary: 1 << 20, + numFastBytes: 32 + ), + }; + using (var archiveStream = File.Create(archivePath)) + using (var writer = new SevenZipWriter(archiveStream, writerOptions)) + using (var source = new MemoryStream(payload)) + { + writer.Write("payload.bin", source, DateTime.UtcNow); + } + + Assert.Equal( + payload, + ReadSinglePayloadEntry( + archivePath, + ReaderOptions.ForFilePath, + out var sequentialFolderStreamType + ) + ); + Assert.NotEqual(typeof(FileStream), sequentialFolderStreamType); + + Assert.Equal( + payload, + ReadSinglePayloadEntry( + archivePath, + ReaderOptions.ForFilePath.WithEnableParallelism(true), + out var parallelFolderStreamType + ) + ); + Assert.Equal(typeof(FileStream), parallelFolderStreamType); + } + + private static byte[] ReadSinglePayloadEntry( + string archivePath, + ReaderOptions readerOptions, + out Type folderStreamType + ) + { + using var archive = (SevenZipArchive) + SevenZipArchive.OpenArchive(archivePath, readerOptions); + using var reader = archive.ExtractAllEntries(); + var sevenZipReader = Assert.IsType(reader); + sevenZipReader.DiagnosticsEnabled = true; + + Assert.True(reader.MoveToNextEntry()); + using var entryStream = reader.OpenEntryStream(); + using var output = new MemoryStream(); + entryStream.CopyTo(output); + + folderStreamType = sevenZipReader.DiagnosticsCurrentFolderStream!.GetType(); + return output.ToArray(); + } +#endif } diff --git a/tests/SharpCompress.Test/Streams/LzmaStreamTests.cs b/tests/SharpCompress.Test/Streams/LzmaStreamTests.cs index 8b7ebea0..5a0e77cf 100644 --- a/tests/SharpCompress.Test/Streams/LzmaStreamTests.cs +++ b/tests/SharpCompress.Test/Streams/LzmaStreamTests.cs @@ -1,6 +1,8 @@ using System; using System.Buffers; +using System.Collections.Generic; using System.IO; +using System.Linq; using SharpCompress.Compressors.LZMA; using Xunit; @@ -606,4 +608,304 @@ public class LzmaStreamTests } } } + + // Tests Lzma2ParallelDecoder directly using hand-built LZMA2 streams made only of + // "uncompressed" chunks (control bytes 0x01/0x02). Those chunks carry their payload verbatim + // (no real LZMA compression involved), which makes it possible to build fully valid, precisely + // controlled multi-block LZMA2 streams -- including ones that exercise the small-block merge + // threshold and cross-segment block cuts -- without needing a real (and large) 7z test archive. +#if !LEGACY_DOTNET + // Props byte encoding a ~2MB dictionary (see LzmaStream's dict-size formula), comfortably + // larger than any single chunk used by these tests. + private static readonly byte[] Lzma2Props = { 18 }; +#endif + + private static byte[] UncompressedChunk(bool dictReset, byte[] payload) + { + Assert.InRange(payload.Length, 1, 0x10000); + var sizeMinusOne = payload.Length - 1; + var chunk = new byte[3 + payload.Length]; + chunk[0] = dictReset ? (byte)0x01 : (byte)0x02; + chunk[1] = (byte)(sizeMinusOne >> 8); + chunk[2] = (byte)(sizeMinusOne & 0xFF); + Buffer.BlockCopy(payload, 0, chunk, 3, payload.Length); + return chunk; + } + + private static byte[] RepeatingPayload(int size, byte seed) + { + var payload = new byte[size]; + for (var i = 0; i < size; i++) + { + payload[i] = (byte)(seed + i); + } + return payload; + } + + [Fact] + public void Lzma2ParallelDecoder_TryScanBlocks_MergesSmallSegmentsAndCutsOnceThresholdReached() + { + // Seg1+Seg2 are both tiny -> merge. Seg3 is >= MinBlockSize on its own, so it merges into + // the same block (the cut only happens once the *already accumulated* size reaches the + // threshold), pushing that block's total past MinBlockSize. Seg4 then starts a new block, + // and Seg5 (tiny) merges into it. The trailing block is flushed as-is even though small. + var seg1 = UncompressedChunk(dictReset: true, RepeatingPayload(100, 1)); + var seg2 = UncompressedChunk(dictReset: true, RepeatingPayload(100, 2)); + var seg3 = UncompressedChunk(dictReset: true, RepeatingPayload(20_000, 3)); + var seg4 = UncompressedChunk(dictReset: true, RepeatingPayload(50, 4)); + var seg5 = UncompressedChunk(dictReset: true, RepeatingPayload(50, 5)); + + using var ms = new MemoryStream(); + ms.Write(seg1, 0, seg1.Length); + ms.Write(seg2, 0, seg2.Length); + ms.Write(seg3, 0, seg3.Length); + ms.Write(seg4, 0, seg4.Length); + ms.Write(seg5, 0, seg5.Length); + ms.WriteByte(0); // end marker + + var packSize = ms.Length; + var unpackSize = 100 + 100 + 20_000 + 50 + 50; + + var blocks = Lzma2ParallelDecoder.TryScanBlocks(ms, 0, packSize, unpackSize); + + Assert.NotNull(blocks); + Assert.Equal(2, blocks!.Count); + + Assert.Equal(0, blocks[0].UnpackOffset); + Assert.Equal(100 + 100 + 20_000, blocks[0].UnpackLen); + + Assert.Equal(100 + 100 + 20_000, blocks[1].UnpackOffset); + Assert.Equal(50 + 50, blocks[1].UnpackLen); + } + + [Fact] + public void Lzma2ParallelDecoder_TryScanBlocks_SingleSmallSegment_ReturnsOneBlock() + { + var seg = UncompressedChunk(dictReset: true, RepeatingPayload(64, 7)); + using var ms = new MemoryStream(); + ms.Write(seg, 0, seg.Length); + ms.WriteByte(0); + + var blocks = Lzma2ParallelDecoder.TryScanBlocks(ms, 0, ms.Length, 64); + + Assert.NotNull(blocks); + Assert.Single(blocks!); + Assert.Equal(0, blocks![0].UnpackOffset); + Assert.Equal(64, blocks[0].UnpackLen); + } + + [Fact] + public void Lzma2ParallelDecoder_TryScanBlocks_NonRestartChunksNeverCutABlock() + { + // A dict-reset chunk followed by several *non*-restart continuation chunks must all stay + // in the same block, regardless of accumulated size, since none of them are independently + // decodable restart points. + var seg1 = UncompressedChunk(dictReset: true, RepeatingPayload(20_000, 1)); + var seg2 = UncompressedChunk(dictReset: false, RepeatingPayload(20_000, 2)); + var seg3 = UncompressedChunk(dictReset: false, RepeatingPayload(20_000, 3)); + + using var ms = new MemoryStream(); + ms.Write(seg1, 0, seg1.Length); + ms.Write(seg2, 0, seg2.Length); + ms.Write(seg3, 0, seg3.Length); + ms.WriteByte(0); + + var blocks = Lzma2ParallelDecoder.TryScanBlocks(ms, 0, ms.Length, 60_000); + + Assert.NotNull(blocks); + Assert.Single(blocks!); + Assert.Equal(60_000, blocks![0].UnpackLen); + } + + [Fact] + public void Lzma2ParallelDecoder_TryScanBlocks_MismatchedUnpackSize_ReturnsNull() + { + var seg = UncompressedChunk(dictReset: true, RepeatingPayload(64, 7)); + using var ms = new MemoryStream(); + ms.Write(seg, 0, seg.Length); + ms.WriteByte(0); + + var blocks = Lzma2ParallelDecoder.TryScanBlocks( + ms, + 0, + ms.Length, + 65 /* wrong */ + ); + + Assert.Null(blocks); + } + + [Fact] + public void Lzma2ParallelDecoder_TryScanBlocks_InvalidControlByte_ReturnsNull() + { + using var ms = new MemoryStream(new byte[] { 0x03, 0x00, 0x00, 0x00, 0x00 }); + + var blocks = Lzma2ParallelDecoder.TryScanBlocks(ms, 0, ms.Length, 1); + + Assert.Null(blocks); + } + + [Fact] + public void Lzma2ParallelDecoder_TryScanBlocks_TruncatedStream_ReturnsNull() + { + // Claims a 100-byte chunk but only provides a couple of bytes of payload. + using var ms = new MemoryStream(new byte[] { 0x01, 0x00, 0x63, 0xAA, 0xBB }); + + var blocks = Lzma2ParallelDecoder.TryScanBlocks(ms, 0, ms.Length, 100); + + Assert.Null(blocks); + } + + [Fact] + public void Lzma2ParallelDecoder_TryScanBlocks_EmptyStream_ReturnsEmptyList() + { + using var ms = new MemoryStream(new byte[] { 0 }); // just the end marker + + var blocks = Lzma2ParallelDecoder.TryScanBlocks(ms, 0, ms.Length, 0); + + Assert.NotNull(blocks); + Assert.Empty(blocks!); + } + + [Fact] + public void Lzma2ParallelDecoder_TryScanBlocks_RealCompressedChunksWithNewProps_ParsesAndDecodesCorrectly() + { + // Real LZMA2 compressed chunks (as produced by the actual encoder used by SevenZipWriter) + // always set the new-props flag (control 0xE0-0xFF), which carries an extra properties + // byte in the chunk header, in addition to the 4 size-field bytes. This regression test + // guards that exact byte -- the hand-built "uncompressed chunk" tests above (control + // 0x01/0x02) never exercise this header shape, which is exactly how this bug slipped + // through: a real multi-GB archive was needed to surface it, header-only synthetic tests + // using only uncompressed chunks were not enough. + var payload = new byte[5_000_000]; + for (var i = 0; i < payload.Length; i++) + { + payload[i] = (byte)(i % 7); // highly repetitive -> compresses well, forces real chunks + } + + byte[] lzma2Props; + using var packMs = new MemoryStream(); + using ( + var encoder = new Lzma2EncoderStream(packMs, dictionarySize: 1 << 20, numFastBytes: 32) + ) + { + encoder.Write(payload, 0, payload.Length); + lzma2Props = encoder.Properties; + } + var packBytes = packMs.ToArray(); + + var blocks = Lzma2ParallelDecoder.TryScanBlocks( + packMs, + 0, + packBytes.Length, + payload.Length + ); + + Assert.NotNull(blocks); + Assert.True(blocks!.Count >= 1); + Assert.Equal(payload.Length, blocks.Sum(b => b.UnpackLen)); + + // Decode sequentially (production LzmaStream decode) to confirm the scanner's reported + // pack/unpack accounting is actually consistent with what a real decode produces. + using var decodeMs = new MemoryStream(packBytes); + using var lzma = LzmaStream.Create( + lzma2Props, + decodeMs, + -1, + payload.Length, + leaveOpen: true + ); + var decoded = new byte[payload.Length]; + var totalRead = 0; + while (totalRead < decoded.Length) + { + var read = lzma.Read(decoded, totalRead, decoded.Length - totalRead); + Assert.True(read > 0); + totalRead += read; + } + + Assert.Equal(payload, decoded); + } + +#if !LEGACY_DOTNET + [Fact] + public void Lzma2ParallelDecoder_DecodeBlocksParallel_ProducesByteIdenticalOutputAcrossMultipleBlocks() + { + // Enough independent, large-enough segments to guarantee more than one merged block. + var payloads = new List(); + for (var i = 0; i < 6; i++) + { + payloads.Add(RepeatingPayload(20_000 + i, (byte)(i * 17))); + } + + using var packMs = new MemoryStream(); + foreach (var payload in payloads) + { + var chunk = UncompressedChunk(dictReset: true, payload); + packMs.Write(chunk, 0, chunk.Length); + } + packMs.WriteByte(0); + var packBytes = packMs.ToArray(); + + var expected = new byte[payloads.Count == 0 ? 0 : payloads.Sum(p => p.Length)]; + var offset = 0; + foreach (var payload in payloads) + { + Buffer.BlockCopy(payload, 0, expected, offset, payload.Length); + offset += payload.Length; + } + + var blocks = Lzma2ParallelDecoder.TryScanBlocks( + packMs, + 0, + packBytes.Length, + expected.Length + ); + Assert.NotNull(blocks); + Assert.True(blocks!.Count > 1, "Test setup should produce more than one block."); + + var inputPath = Path.GetTempFileName(); + var outputPath = Path.GetTempFileName(); + try + { + File.WriteAllBytes(inputPath, packBytes); + + using ( + var inputFile = new FileStream( + inputPath, + FileMode.Open, + FileAccess.Read, + FileShare.Read + ) + ) + using ( + var outputFile = new FileStream( + outputPath, + FileMode.Open, + FileAccess.ReadWrite, + FileShare.None + ) + ) + { + outputFile.SetLength(expected.Length); + Lzma2ParallelDecoder.DecodeBlocksParallel( + inputFile.SafeFileHandle, + Lzma2Props, + 0, + blocks, + outputFile.SafeFileHandle, + Environment.ProcessorCount + ); + } + + var actual = File.ReadAllBytes(outputPath); + Assert.Equal(expected, actual); + } + finally + { + File.Delete(inputPath); + File.Delete(outputPath); + } + } +#endif } From c4fa31d615c2ca336878f63fba7768d4e47f6a5a Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Mon, 3 Aug 2026 08:15:18 +0100 Subject: [PATCH 15/40] gzip using sync generator --- .../Archives/GZip/GZipArchive.Async.cs | 11 +- .../Archives/GZip/GZipArchive.Factory.cs | 7 +- .../Archives/GZip/GZipArchive.cs | 33 +---- .../Archives/GZip/GZipArchiveEntry.cs | 15 +-- .../GZip/GZipChecksumValidationStream.cs | 55 ++++---- .../Common/GZip/GZipEntry.Async.cs | 1 + src/SharpCompress/Common/GZip/GZipEntry.cs | 10 -- .../Common/GZip/GZipFilePart.Async.cs | 1 + src/SharpCompress/Common/GZip/GZipFilePart.cs | 29 +---- .../Compressors/Deflate/GZipStream.Async.cs | 63 ++++++++++ .../Compressors/Deflate/GZipStream.cs | 117 +----------------- .../Default/GZipCompressionProvider.cs | 25 ++-- .../Readers/GZip/GZipReader.Async.cs | 1 + src/SharpCompress/Readers/GZip/GZipReader.cs | 5 - .../Writers/GZip/GZipWriter.Async.cs | 22 +++- src/SharpCompress/Writers/GZip/GZipWriter.cs | 22 ---- .../GZip/GZipArchiveAsyncTests.cs | 31 +++++ .../GZip/GZipWriterAsyncTests.cs | 31 +++++ .../GZip/GZipWriterDirectoryTests.cs | 12 ++ .../OptionsUsabilityTests.cs | 15 +++ 20 files changed, 229 insertions(+), 277 deletions(-) diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.Async.cs b/src/SharpCompress/Archives/GZip/GZipArchive.Async.cs index 49f03937..7b2cda51 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.Async.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.Async.cs @@ -15,9 +15,11 @@ namespace SharpCompress.Archives.GZip; public partial class GZipArchive { + [Zomp.SyncMethodGenerator.CreateSyncVersion] public ValueTask SaveToAsync(string filePath, CancellationToken cancellationToken = default) => SaveToAsync(new FileInfo(filePath), cancellationToken); + [Zomp.SyncMethodGenerator.CreateSyncVersion] public async ValueTask SaveToAsync( FileInfo fileInfo, CancellationToken cancellationToken = default @@ -28,6 +30,7 @@ public partial class GZipArchive .ConfigureAwait(false); } + [Zomp.SyncMethodGenerator.CreateSyncVersion] protected override async ValueTask SaveToAsync( Stream stream, GZipWriterOptions options, @@ -54,6 +57,7 @@ public partial class GZipArchive .WriteAsync( entry.Key.NotNull("Entry Key is null"), entryStream, + entry.LastModifiedTime, cancellationToken ) .ConfigureAwait(false); @@ -65,7 +69,12 @@ public partial class GZipArchive .OpenEntryStreamAsync(cancellationToken) .ConfigureAwait(false); await writer - .WriteAsync(entry.Key.NotNull("Entry Key is null"), entryStream, cancellationToken) + .WriteAsync( + entry.Key.NotNull("Entry Key is null"), + entryStream, + entry.LastModifiedTime, + cancellationToken + ) .ConfigureAwait(false); } } diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs b/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs index 2f5e5e33..5f4e7726 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs @@ -182,7 +182,12 @@ public partial class GZipArchive var header = ArrayPool.Shared.Rent(10); try { - await stream.ReadFullyAsync(header, 0, 10, cancellationToken).ConfigureAwait(false); + if ( + !await stream.ReadFullyAsync(header, 0, 10, cancellationToken).ConfigureAwait(false) + ) + { + return false; + } if (header[0] != 0x1F || header[1] != 0x8B || header[2] != 8) { diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.cs b/src/SharpCompress/Archives/GZip/GZipArchive.cs index e47bf7c6..ab3a009d 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.cs @@ -8,7 +8,6 @@ using SharpCompress.Common.Options; using SharpCompress.IO; using SharpCompress.Readers; using SharpCompress.Readers.GZip; -using SharpCompress.Writers; using SharpCompress.Writers.GZip; namespace SharpCompress.Archives.GZip; @@ -28,14 +27,6 @@ public partial class GZipArchive return sourceStream.Streams.Select(a => new GZipVolume(a, ReaderOptions, 0)); } - public void SaveTo(string filePath) => SaveTo(new FileInfo(filePath)); - - public void SaveTo(FileInfo fileInfo) - { - using var stream = fileInfo.Open(FileMode.Create, FileAccess.Write); - SaveTo(stream, new GZipWriterOptions(CompressionType.GZip)); - } - protected override GZipArchiveEntry CreateEntryInternal( string key, Stream source, @@ -54,31 +45,9 @@ public partial class GZipArchive protected override GZipArchiveEntry CreateDirectoryEntry(string key, DateTime? modified) => throw new NotSupportedException("GZip archives do not support directory entries."); - protected override void SaveTo( - Stream stream, - GZipWriterOptions options, - IEnumerable oldEntries, - IEnumerable newEntries - ) - { - if (Entries.Count > 1) - { - throw new InvalidFormatException("Only one entry is allowed in a GZip Archive"); - } - using var writer = new GZipWriter(stream, options); - foreach (var entry in oldEntries.Concat(newEntries).Where(x => !x.IsDirectory)) - { - using var entryStream = entry.OpenEntryStream(); - writer.Write( - entry.Key.NotNull("Entry Key is null"), - entryStream, - entry.LastModifiedTime - ); - } - } - protected override IEnumerable LoadEntries(IEnumerable volumes) { + // Kept hand-written: the generator cannot map IAsyncEnumerable.SingleAsync to LINQ Single. var stream = volumes.Single().Stream; yield return new GZipArchiveEntry( this, diff --git a/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs b/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs index 9d975788..4c3bed6c 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs @@ -7,23 +7,12 @@ using SharpCompress.Common.Options; namespace SharpCompress.Archives.GZip; -public class GZipArchiveEntry : GZipEntry, IArchiveEntry +public partial class GZipArchiveEntry : GZipEntry, IArchiveEntry { internal GZipArchiveEntry(GZipArchive archive, GZipFilePart? part, IReaderOptions readerOptions) : base(part, readerOptions) => Archive = archive; - public virtual Stream OpenEntryStream() - { - //this is to reset the stream to be read multiple times - var part = (GZipFilePart)Parts.Single(); - var rawStream = part.GetRawStream(); - if (rawStream.CanSeek && rawStream.Position != part.EntryStartPosition) - { - rawStream.Position = part.EntryStartPosition; - } - return Parts.Single().GetCompressedStream().NotNull(); - } - + [Zomp.SyncMethodGenerator.CreateSyncVersion] public virtual async ValueTask OpenEntryStreamAsync( CancellationToken cancellationToken = default ) diff --git a/src/SharpCompress/Common/GZip/GZipChecksumValidationStream.cs b/src/SharpCompress/Common/GZip/GZipChecksumValidationStream.cs index 512d7cc2..accf1db4 100644 --- a/src/SharpCompress/Common/GZip/GZipChecksumValidationStream.cs +++ b/src/SharpCompress/Common/GZip/GZipChecksumValidationStream.cs @@ -7,7 +7,7 @@ using SharpCompress.Crypto; namespace SharpCompress.Common.GZip; -internal sealed class GZipChecksumValidationStream : Stream +internal sealed partial class GZipChecksumValidationStream : Stream { private readonly Stream _source; private readonly Stream _rawStream; @@ -46,23 +46,38 @@ internal sealed class GZipChecksumValidationStream : Stream set => throw new NotSupportedException(); } - public override void Flush() => _source.Flush(); - + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override Task FlushAsync(CancellationToken cancellationToken) => _source.FlushAsync(cancellationToken); - public override int Read(byte[] buffer, int offset, int count) + [Zomp.SyncMethodGenerator.CreateSyncVersion] + public override async Task ReadAsync( + byte[] buffer, + int offset, + int count, + CancellationToken cancellationToken + ) { - var read = _source.Read(buffer, offset, count); + var read = await _source + .ReadAsync(buffer, offset, count, cancellationToken) + .ConfigureAwait(false); UpdateAndValidateAtEof(buffer.AsSpan(offset, read), read); return read; } #if !LEGACY_DOTNET - public override int Read(Span buffer) + [Zomp.SyncMethodGenerator.CreateSyncVersion] + public override async ValueTask ReadAsync( + Memory buffer, + CancellationToken cancellationToken = default + ) { - var read = _source.Read(buffer); + var read = await _source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false); +#if SYNC_ONLY UpdateAndValidateAtEof(buffer[..read], read); +#else + UpdateAndValidateAtEof(buffer.Span[..read], read); +#endif return read; } #endif @@ -83,32 +98,6 @@ internal sealed class GZipChecksumValidationStream : Stream return value; } - public override async Task ReadAsync( - byte[] buffer, - int offset, - int count, - CancellationToken cancellationToken - ) - { - var read = await _source - .ReadAsync(buffer, offset, count, cancellationToken) - .ConfigureAwait(false); - UpdateAndValidateAtEof(buffer.AsSpan(offset, read), read); - return read; - } - -#if !LEGACY_DOTNET - public override async ValueTask ReadAsync( - Memory buffer, - CancellationToken cancellationToken = default - ) - { - var read = await _source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false); - UpdateAndValidateAtEof(buffer.Span[..read], read); - return read; - } -#endif - public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); public override void SetLength(long value) => throw new NotSupportedException(); diff --git a/src/SharpCompress/Common/GZip/GZipEntry.Async.cs b/src/SharpCompress/Common/GZip/GZipEntry.Async.cs index 7bb5f983..a19caa71 100644 --- a/src/SharpCompress/Common/GZip/GZipEntry.Async.cs +++ b/src/SharpCompress/Common/GZip/GZipEntry.Async.cs @@ -6,6 +6,7 @@ namespace SharpCompress.Common.GZip; public partial class GZipEntry { + [Zomp.SyncMethodGenerator.CreateSyncVersion] internal static async IAsyncEnumerable GetEntriesAsync( Stream stream, ReaderOptions options diff --git a/src/SharpCompress/Common/GZip/GZipEntry.cs b/src/SharpCompress/Common/GZip/GZipEntry.cs index 48f08d46..e359198e 100644 --- a/src/SharpCompress/Common/GZip/GZipEntry.cs +++ b/src/SharpCompress/Common/GZip/GZipEntry.cs @@ -46,14 +46,4 @@ public partial class GZipEntry : Entry public override bool IsSplitAfter => false; internal override IEnumerable Parts => _filePart.Empty(); - - internal static IEnumerable GetEntries(Stream stream, ReaderOptions options) - { - yield return new GZipEntry( - GZipFilePart.Create(stream, options.ArchiveEncoding, options.Providers), - options - ); - } - - // Async methods moved to GZipEntry.Async.cs } diff --git a/src/SharpCompress/Common/GZip/GZipFilePart.Async.cs b/src/SharpCompress/Common/GZip/GZipFilePart.Async.cs index ae3e118a..e4bd555d 100644 --- a/src/SharpCompress/Common/GZip/GZipFilePart.Async.cs +++ b/src/SharpCompress/Common/GZip/GZipFilePart.Async.cs @@ -13,6 +13,7 @@ namespace SharpCompress.Common.GZip; internal sealed partial class GZipFilePart { + [Zomp.SyncMethodGenerator.CreateSyncVersion] internal static async ValueTask CreateAsync( Stream stream, IArchiveEncoding archiveEncoding, diff --git a/src/SharpCompress/Common/GZip/GZipFilePart.cs b/src/SharpCompress/Common/GZip/GZipFilePart.cs index 13a1c968..3ce51163 100644 --- a/src/SharpCompress/Common/GZip/GZipFilePart.cs +++ b/src/SharpCompress/Common/GZip/GZipFilePart.cs @@ -16,32 +16,6 @@ internal sealed partial class GZipFilePart : FilePart private readonly Stream _stream; private readonly CompressionProviderRegistry _compressionProviders; - internal static GZipFilePart Create( - Stream stream, - IArchiveEncoding archiveEncoding, - CompressionProviderRegistry compressionProviders - ) - { - var part = new GZipFilePart(stream, archiveEncoding, compressionProviders); - - part.ReadAndValidateGzipHeader(); - if (stream.CanSeek) - { - var position = stream.Position; - stream.Position = stream.Length - 8; - part.ReadTrailer(); - stream.Position = position; - part.EntryStartPosition = position; - } - else - { - // For non-seekable streams, we can't read the trailer or track position. - // Set to 0 since the stream will be read sequentially from its current position. - part.EntryStartPosition = 0; - } - return part; - } - private GZipFilePart( Stream stream, IArchiveEncoding archiveEncoding, @@ -74,6 +48,7 @@ internal sealed partial class GZipFilePart : FilePart private void ReadTrailer() { + // Keep this sync implementation for its stack allocation on the parser hot path. // Read and potentially verify the GZIP trailer: CRC32 and size mod 2^32 Span trailer = stackalloc byte[8]; _stream.ReadFully(trailer); @@ -84,6 +59,7 @@ internal sealed partial class GZipFilePart : FilePart private void ReadAndValidateGzipHeader() { + // Keep this sync implementation for stackalloc and ReadByte-based header parsing. // read the header on the first read Span header = stackalloc byte[10]; var n = _stream.Read(header); @@ -136,6 +112,7 @@ internal sealed partial class GZipFilePart : FilePart private string ReadZeroTerminatedString(Stream stream) { + // Keep this sync implementation for its one-byte stack allocation. Span buf1 = stackalloc byte[1]; var list = new List(); var done = false; diff --git a/src/SharpCompress/Compressors/Deflate/GZipStream.Async.cs b/src/SharpCompress/Compressors/Deflate/GZipStream.Async.cs index dc2f823c..4a70c519 100644 --- a/src/SharpCompress/Compressors/Deflate/GZipStream.Async.cs +++ b/src/SharpCompress/Compressors/Deflate/GZipStream.Async.cs @@ -9,6 +9,10 @@ namespace SharpCompress.Compressors.Deflate; public partial class GZipStream { + /// + /// Flush the stream. + /// + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override async Task FlushAsync(CancellationToken cancellationToken) { if (_disposed) @@ -18,6 +22,38 @@ public partial class GZipStream await BaseStream.FlushAsync(cancellationToken).ConfigureAwait(false); } + /// + /// Read and decompress data from the source stream. + /// + /// + /// + /// With a GZipStream, decompression is done through reading. + /// + /// + /// + /// + /// byte[] working = new byte[WORKING_BUFFER_SIZE]; + /// using (System.IO.Stream input = System.IO.File.OpenRead(_CompressedFile)) + /// { + /// using (Stream decompressor= new Ionic.Zlib.GZipStream(input, CompressionMode.Decompress, true)) + /// { + /// using (var output = System.IO.File.Create(_DecompressedFile)) + /// { + /// int n; + /// while ((n= decompressor.Read(working, 0, working.Length)) !=0) + /// { + /// output.Write(working, 0, n); + /// } + /// } + /// } + /// } + /// + /// + /// The buffer into which the decompressed data should be placed. + /// the offset within that data array to put the first byte read. + /// the number of bytes to read. + /// the number of bytes actually read + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override async Task ReadAsync( byte[] buffer, int offset, @@ -33,6 +69,9 @@ public partial class GZipStream .ReadAsync(buffer, offset, count, cancellationToken) .ConfigureAwait(false); + // Console.WriteLine("GZipStream::Read(buffer, off({0}), c({1}) = {2}", offset, count, n); + // Console.WriteLine( Util.FormatByteArray(buffer, offset, n) ); + if (!_firstReadDone) { _firstReadDone = true; @@ -66,6 +105,29 @@ public partial class GZipStream } #endif + /// + /// Write data to the stream. + /// + /// + /// + /// + /// If you wish to use the GZipStream to compress data while writing, + /// you can create a GZipStream with CompressionMode.Compress, and a + /// writable output stream. Then call Write() on that GZipStream, + /// providing uncompressed data as input. The data sent to the output stream + /// will be the compressed form of the data written. + /// + /// + /// + /// A GZipStream can be used for Read() or Write(), but not + /// both. Writing implies compression. Reading implies decompression. + /// + /// + /// + /// The buffer holding data to write to the stream. + /// the offset within that data array to find the first byte to write. + /// the number of bytes to write. + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override async Task WriteAsync( byte[] buffer, int offset, @@ -79,6 +141,7 @@ public partial class GZipStream } if (BaseStream._streamMode == ZlibBaseStream.StreamMode.Undefined) { + //Console.WriteLine("GZipStream: First write"); if (BaseStream._wantCompress) { // first write in compression, therefore, emit the GZIP header diff --git a/src/SharpCompress/Compressors/Deflate/GZipStream.cs b/src/SharpCompress/Compressors/Deflate/GZipStream.cs index b0a690d8..0009118d 100644 --- a/src/SharpCompress/Compressors/Deflate/GZipStream.cs +++ b/src/SharpCompress/Compressors/Deflate/GZipStream.cs @@ -250,70 +250,6 @@ public partial class GZipStream : Stream } } - /// - /// Flush the stream. - /// - public override void Flush() - { - if (_disposed) - { - throw new ObjectDisposedException("GZipStream"); - } - BaseStream.Flush(); - } - - /// - /// Read and decompress data from the source stream. - /// - /// - /// - /// With a GZipStream, decompression is done through reading. - /// - /// - /// - /// - /// byte[] working = new byte[WORKING_BUFFER_SIZE]; - /// using (System.IO.Stream input = System.IO.File.OpenRead(_CompressedFile)) - /// { - /// using (Stream decompressor= new Ionic.Zlib.GZipStream(input, CompressionMode.Decompress, true)) - /// { - /// using (var output = System.IO.File.Create(_DecompressedFile)) - /// { - /// int n; - /// while ((n= decompressor.Read(working, 0, working.Length)) !=0) - /// { - /// output.Write(working, 0, n); - /// } - /// } - /// } - /// } - /// - /// - /// The buffer into which the decompressed data should be placed. - /// the offset within that data array to put the first byte read. - /// the number of bytes to read. - /// the number of bytes actually read - public override int Read(byte[] buffer, int offset, int count) - { - if (_disposed) - { - throw new ObjectDisposedException("GZipStream"); - } - var n = BaseStream.Read(buffer, offset, count); - - // Console.WriteLine("GZipStream::Read(buffer, off({0}), c({1}) = {2}", offset, count, n); - // Console.WriteLine( Util.FormatByteArray(buffer, offset, n) ); - - if (!_firstReadDone) - { - _firstReadDone = true; - FileName = BaseStream._GzipFileName; - Comment = BaseStream._GzipComment; - LastModified = BaseStream._GzipMtime; - } - return n; - } - /// /// Calling this method always throws a . /// @@ -328,51 +264,6 @@ public partial class GZipStream : Stream /// irrelevant; this method will always throw! public override void SetLength(long value) => throw new NotSupportedException(); - /// - /// Write data to the stream. - /// - /// - /// - /// - /// If you wish to use the GZipStream to compress data while writing, - /// you can create a GZipStream with CompressionMode.Compress, and a - /// writable output stream. Then call Write() on that GZipStream, - /// providing uncompressed data as input. The data sent to the output stream - /// will be the compressed form of the data written. - /// - /// - /// - /// A GZipStream can be used for Read() or Write(), but not - /// both. Writing implies compression. Reading implies decompression. - /// - /// - /// - /// The buffer holding data to write to the stream. - /// the offset within that data array to find the first byte to write. - /// the number of bytes to write. - public override void Write(byte[] buffer, int offset, int count) - { - if (_disposed) - { - throw new ObjectDisposedException("GZipStream"); - } - if (BaseStream._streamMode == ZlibBaseStream.StreamMode.Undefined) - { - //Console.WriteLine("GZipStream: First write"); - if (BaseStream._wantCompress) - { - // first write in compression, therefore, emit the GZIP header - _headerByteCount = EmitHeader(); - } - else - { - throw new ArchiveOperationException(); - } - } - - BaseStream.Write(buffer, offset, count); - } - #endregion Stream methods public string? Comment @@ -509,13 +400,7 @@ public partial class GZipStream : Stream return header; } - private int EmitHeader() - { - var header = BuildHeader(); - BaseStream._stream.Write(header, 0, header.Length); - return header.Length; // bytes written - } - + [Zomp.SyncMethodGenerator.CreateSyncVersion] private async ValueTask EmitHeaderAsync(CancellationToken cancellationToken) { var header = BuildHeader(); diff --git a/src/SharpCompress/Providers/Default/GZipCompressionProvider.cs b/src/SharpCompress/Providers/Default/GZipCompressionProvider.cs index 1365e0a7..be88c40c 100644 --- a/src/SharpCompress/Providers/Default/GZipCompressionProvider.cs +++ b/src/SharpCompress/Providers/Default/GZipCompressionProvider.cs @@ -11,7 +11,7 @@ namespace SharpCompress.Providers.Default; /// /// Provides GZip compression using SharpCompress's internal implementation. /// -public sealed class GZipCompressionProvider : CompressionProviderBase +public sealed partial class GZipCompressionProvider : CompressionProviderBase { public override CompressionType CompressionType => CompressionType.GZip; public override bool SupportsCompression => true; @@ -28,23 +28,22 @@ public sealed class GZipCompressionProvider : CompressionProviderBase return new GZipStream(source, CompressionMode.Decompress); } - public override Stream CreateDecompressStream(Stream source, CompressionContext context) - { - return new GZipStream( - source, - CompressionMode.Decompress, - CompressionLevel.Default, - context.ResolveArchiveEncoding() - ); - } - - public override ValueTask CreateDecompressStreamAsync( + [Zomp.SyncMethodGenerator.CreateSyncVersion] + public override async ValueTask CreateDecompressStreamAsync( Stream source, CompressionContext context, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new ValueTask(CreateDecompressStream(source, context)); + return await Task.FromResult( + new GZipStream( + source, + CompressionMode.Decompress, + CompressionLevel.Default, + context.ResolveArchiveEncoding() + ) + ) + .ConfigureAwait(false); } } diff --git a/src/SharpCompress/Readers/GZip/GZipReader.Async.cs b/src/SharpCompress/Readers/GZip/GZipReader.Async.cs index 720cb792..28b10b81 100644 --- a/src/SharpCompress/Readers/GZip/GZipReader.Async.cs +++ b/src/SharpCompress/Readers/GZip/GZipReader.Async.cs @@ -10,6 +10,7 @@ public partial class GZipReader /// /// Returns entries asynchronously for streams that only support async reads. /// + [Zomp.SyncMethodGenerator.CreateSyncVersion] protected override IAsyncEnumerable GetEntriesAsync(Stream stream) => GZipEntry.GetEntriesAsync(stream, Options); } diff --git a/src/SharpCompress/Readers/GZip/GZipReader.cs b/src/SharpCompress/Readers/GZip/GZipReader.cs index eb09ab6c..aea23e73 100644 --- a/src/SharpCompress/Readers/GZip/GZipReader.cs +++ b/src/SharpCompress/Readers/GZip/GZipReader.cs @@ -11,9 +11,4 @@ public partial class GZipReader : AbstractReader : base(options, ArchiveType.GZip) => Volume = new GZipVolume(stream, options, 0); public override GZipVolume Volume { get; } - - protected override IEnumerable GetEntries(Stream stream) => - GZipEntry.GetEntries(stream, Options); - - // GetEntriesAsync moved to GZipReader.Async.cs } diff --git a/src/SharpCompress/Writers/GZip/GZipWriter.Async.cs b/src/SharpCompress/Writers/GZip/GZipWriter.Async.cs index a1aa613a..de72c0d4 100644 --- a/src/SharpCompress/Writers/GZip/GZipWriter.Async.cs +++ b/src/SharpCompress/Writers/GZip/GZipWriter.Async.cs @@ -3,11 +3,13 @@ using System.IO; using System.Threading; using System.Threading.Tasks; using SharpCompress.Compressors.Deflate; +using SharpCompress.IO; namespace SharpCompress.Writers.GZip; public partial class GZipWriter { + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override async ValueTask WriteAsync( string filename, Stream source, @@ -19,18 +21,28 @@ public partial class GZipWriter { throw new ArgumentException("Can only write a single stream to a GZip file."); } - var stream = (GZipStream)OutputStream.NotNull(); - stream.FileName = filename; - stream.LastModified = modificationTime; + + // Custom providers need not expose SharpCompress's internal GZip stream. + if (OutputStream is GZipStream gzipStream) + { + gzipStream.FileName = filename; + gzipStream.LastModified = modificationTime; + } + var progressStream = WrapWithProgress(source, filename); #if LEGACY_DOTNET - await progressStream.CopyToAsync(stream).ConfigureAwait(false); + await progressStream + .CopyToAsync(OutputStream.NotNull(), WriterOptions.BufferSize) + .ConfigureAwait(false); #else - await progressStream.CopyToAsync(stream, cancellationToken).ConfigureAwait(false); + await progressStream + .CopyToAsync(OutputStream.NotNull(), WriterOptions.BufferSize, cancellationToken) + .ConfigureAwait(false); #endif _wroteToStream = true; } + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override ValueTask WriteDirectoryAsync( string directoryName, DateTime? modificationTime, diff --git a/src/SharpCompress/Writers/GZip/GZipWriter.cs b/src/SharpCompress/Writers/GZip/GZipWriter.cs index 3c62ff2d..604e7b02 100644 --- a/src/SharpCompress/Writers/GZip/GZipWriter.cs +++ b/src/SharpCompress/Writers/GZip/GZipWriter.cs @@ -70,26 +70,4 @@ public sealed partial class GZipWriter : AbstractWriter } } #pragma warning restore CA2215 - - public override void Write(string filename, Stream source, DateTime? modificationTime) - { - if (_wroteToStream) - { - throw new ArgumentException("Can only write a single stream to a GZip file."); - } - - // Set metadata on the stream if it's the internal GZipStream - if (OutputStream is GZipStream gzipStream) - { - gzipStream.FileName = filename; - gzipStream.LastModified = modificationTime; - } - - var progressStream = WrapWithProgress(source, filename); - progressStream.CopyTo(OutputStream.NotNull(), WriterOptions.BufferSize); - _wroteToStream = true; - } - - public override void WriteDirectory(string directoryName, DateTime? modificationTime) => - throw new NotSupportedException("GZip archives do not support directory entries."); } diff --git a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs index d7812b57..ba5a7782 100644 --- a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs @@ -240,4 +240,35 @@ public class GZipArchiveAsyncTests : ArchiveTests Assert.True(entryStream.IsDisposed); } + + [Fact] + public async ValueTask GZip_Archive_SaveTo_Async_Preserves_Modification_Time() + { + var modificationTime = new DateTime(2024, 1, 2, 3, 4, 5); + using var destination = new MemoryStream(); + await using (var archive = (GZipArchive)await GZipArchive.CreateAsyncArchive()) + { + await archive.AddEntryAsync( + "contents.bin", + new MemoryStream(new byte[] { 1, 2, 3, 4 }), + size: 4, + modified: modificationTime + ); + await archive.SaveToAsync(destination, new GZipWriterOptions()); + } + + destination.Position = 0; + await using var result = await GZipArchive.OpenAsyncArchive(destination); + var entry = await result.EntriesAsync.FirstAsync(); + + Assert.Equal(modificationTime, entry.LastModifiedTime); + } + + [Fact] + public async ValueTask GZip_Archive_IsGZipFileAsync_ReturnsFalse_For_Truncated_Header() + { + using var stream = new MemoryStream(new byte[] { 0x1F, 0x8B, 8 }); + + Assert.False(await GZipArchive.IsGZipFileAsync(stream)); + } } diff --git a/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs index ba9ecd61..56136e8e 100644 --- a/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs @@ -1,6 +1,8 @@ using System.IO; using System.Threading.Tasks; using SharpCompress.Common; +using SharpCompress.Providers; +using SharpCompress.Providers.System; using SharpCompress.Test.Mocks; using SharpCompress.Writers; using SharpCompress.Writers.GZip; @@ -91,4 +93,33 @@ public class GZipWriterAsyncTests : WriterTests Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz") ); } + + [Fact] + public async ValueTask GZip_Writer_Async_With_System_Provider() + { + var contents = new byte[] { 1, 2, 3, 4 }; + var providers = CompressionProviderRegistry.Default.With( + new SystemGZipCompressionProvider() + ); + using var source = new MemoryStream(contents); + using var destination = new MemoryStream(); + await using ( + var writer = new GZipWriter( + destination, + new GZipWriterOptions { LeaveStreamOpen = true, Providers = providers } + ) + ) + { + await writer.WriteAsync("contents.bin", source); + } + + destination.Position = 0; + using var compressed = new SystemGZipCompressionProvider().CreateDecompressStream( + destination + ); + using var actual = new MemoryStream(); + compressed.CopyTo(actual); + + Assert.Equal(contents, actual.ToArray()); + } } diff --git a/tests/SharpCompress.Test/GZip/GZipWriterDirectoryTests.cs b/tests/SharpCompress.Test/GZip/GZipWriterDirectoryTests.cs index 5d51c441..9941d256 100644 --- a/tests/SharpCompress.Test/GZip/GZipWriterDirectoryTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipWriterDirectoryTests.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Threading.Tasks; using SharpCompress.Common; using SharpCompress.Writers.GZip; using Xunit; @@ -16,4 +17,15 @@ public class GZipWriterDirectoryTests : TestBase Assert.Throws(() => writer.WriteDirectory("test-dir", DateTime.Now)); } + + [Fact] + public async ValueTask GZipWriter_WriteDirectoryAsync_ThrowsNotSupportedException() + { + using var memoryStream = new MemoryStream(); + using var writer = new GZipWriter(memoryStream, new GZipWriterOptions()); + + await Assert.ThrowsAsync(async () => + await writer.WriteDirectoryAsync("test-dir", DateTime.Now) + ); + } } diff --git a/tests/SharpCompress.Test/OptionsUsabilityTests.cs b/tests/SharpCompress.Test/OptionsUsabilityTests.cs index 1902e4ab..c8b6ba86 100644 --- a/tests/SharpCompress.Test/OptionsUsabilityTests.cs +++ b/tests/SharpCompress.Test/OptionsUsabilityTests.cs @@ -220,6 +220,21 @@ public class OptionsUsabilityTests : TestBase Assert.Equal(19, source.CopyBufferSize); } + [Fact] + public async Task GZipWriter_Uses_WriterOptions_BufferSize_Async() + { + await using var source = new TrackingReadStream(new byte[100]); + await using var destination = new MemoryStream(); + await using var writer = new GZipWriter( + destination, + new GZipWriterOptions { BufferSize = 23 } + ); + + await writer.WriteAsync("buffer-size.txt", source, DateTime.Now); + + Assert.Equal(23, source.CopyBufferSize); + } + [Fact] public void TarWriter_Uses_WriterOptions_BufferSize_ForTransfer() { From e8ee2264a9ef05c77a67439063c63b2692fce3bb Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Mon, 3 Aug 2026 15:18:44 +0100 Subject: [PATCH 16/40] converted a lot of zip items to sync method generator --- docs/SYNC_METHOD_GENERATION.md | 41 ++- .../Common/Zip/Headers/ZipFileEntry.Async.cs | 3 +- .../Common/Zip/Headers/ZipFileEntry.cs | 12 - .../PkwareTraditionalCryptoStream.Async.cs | 2 + .../Zip/PkwareTraditionalCryptoStream.cs | 43 --- .../Common/Zip/SeekableZipFilePart.Async.cs | 2 + .../Common/Zip/SeekableZipFilePart.cs | 14 - .../Common/Zip/StreamingZipFilePart.Async.cs | 8 +- .../Common/Zip/StreamingZipFilePart.cs | 24 +- .../Common/Zip/WinzipAesCryptoStream.Async.cs | 1 + .../Common/Zip/WinzipAesCryptoStream.cs | 19 -- .../Common/Zip/ZipFilePart.Async.cs | 3 + src/SharpCompress/Common/Zip/ZipFilePart.cs | 127 -------- .../Deflate/DeflateStream.Async.cs | 39 +++ .../Compressors/Deflate/DeflateStream.cs | 88 ------ .../Compressors/Deflate/ZlibBaseStream.cs | 290 +----------------- .../Compressors/Deflate/ZlibStream.Async.cs | 27 ++ .../Compressors/Deflate/ZlibStream.cs | 84 ----- .../Deflate64/Deflate64Stream.Async.cs | 1 + .../Compressors/Deflate64/Deflate64Stream.cs | 47 --- .../Providers/CompressionProviderRegistry.cs | 113 +------ .../Writers/Zip/ZipWriter.Async.cs | 21 +- src/SharpCompress/Writers/Zip/ZipWriter.cs | 67 ---- .../Writers/Zip/ZipWritingStream.cs | 150 +-------- src/SharpCompress/packages.lock.json | 2 +- .../OptionsUsabilityTests.cs | 15 + 26 files changed, 165 insertions(+), 1078 deletions(-) diff --git a/docs/SYNC_METHOD_GENERATION.md b/docs/SYNC_METHOD_GENERATION.md index aac905ef..9bf9ca1b 100644 --- a/docs/SYNC_METHOD_GENERATION.md +++ b/docs/SYNC_METHOD_GENERATION.md @@ -25,7 +25,13 @@ the verification workflow, and the record of what is blocked and why. | 0 | `Archives/IArchiveEntryExtensions` (5 methods) | done — `c303856c` | | 1 | `Compressors/Filters/Filter` + 6 XZ branch filters + `Lzma2Filter` (9 methods, 147 lines) | done — verified identical on net48 + net10.0 | | 2 | `IO/` leaf stream shims + `Common/EntryStream` (17 methods, 190 lines) | done — verified identical on net48 + net10.0 | -| 3–12 | below | to do | +| 3–5 | below | to do | +| 6 | ZIP parts & header factories | 6a done; header factories remain | +| 7 | Concrete writers | ZIP done; Tar, SevenZip, and remaining writers to do | +| 8 | 7-Zip | to do | +| 9 | Streaming compressors | Deflate, Zlib, and Deflate64 done; remaining compressors to do | +| 10–11 | below | to do | +| 12 | Providers | registry done; provider implementations remain | | Rar reader unification | below | to do, needs its own design review | --- @@ -207,16 +213,17 @@ the async one `new byte[4]`, and the method is 4 lines — `SYNC_ONLY` would be `Arj/HuffmanTree`, `Arj/BitReader`, `Arj/LhaStream` (11 pairs, all ≥0.99), `Arj/LHDecoderStream`, `Squeezed/BitReader`. Best win per line of review — pure parsing, all clean. -### 6 — Zip parts & header factories · ~250 lines · med risk +### 6 — Zip parts & header factories · ~250 lines · med risk · **in progress** 6a: `ZipFilePart` (incl. `GetCryptoStreamAsync`), `SeekableZipFilePart`, `StreamingZipFilePart`, -`GZipFilePart`, `Zip/Headers/ZipFileEntry`, `PkwareTraditionalCryptoStream`, `WinzipAesCryptoStream`. +`Zip/Headers/ZipFileEntry`, `PkwareTraditionalCryptoStream`, and `WinzipAesCryptoStream` — **done**. +`GZipFilePart` was completed in the prior GZip batch. 6b: `ZipHeaderFactory` (`LoadHeaderAsync` identical), `SeekableZipHeaderFactory`. -### 7 — Concrete writers · ~250 lines · med risk +### 7 — Concrete writers · ~250 lines · med risk · **in progress** -`ZipWriter`, `ZipWritingStream` (`GetWriteStreamAsync` is a 131-line duplicate), `TarWriter`, -`SevenZipWriter`, `GZipWriter`. +`ZipWriter` and `ZipWritingStream` (`GetWriteStreamAsync` is a 131-line duplicate) — **done**. +`TarWriter` and `SevenZipWriter` remain. `GZipWriter` was completed in the prior GZip batch. This is the **reachable Writers win**: `AbstractWriter` already implements *both* `IWriter` and `IAsyncWriter` (`AbstractWriter.cs:11`), so `ZipWriter.Async.cs:52 @@ -233,14 +240,15 @@ attempt `IWriterExtensions` (see Blocked). Clean: these use the sync `DataReader`, not an async-only reader type. Keep the file split (`ArchiveReader.cs` is 1,377 lines). -### 9 — Streaming compressors · ~300 lines · med-high risk +### 9 — Streaming compressors · ~300 lines · med-high risk · **in progress** -`Deflate64Stream`, `Deflate/ZlibBaseStream` (`ReadAsync` 192 lines, `WriteAsync`, `FlushAsync`), -`DeflateStream`, `GZipStream`, `ZlibStream`, `ZStandard/*`, `Reduce`, `Explode`, `Lzw/LzwStream` -(`ReadAsync` 208 lines), `RLE90`, `Shrink`, `ArcLzw`. +`Deflate64Stream`, `Deflate/ZlibBaseStream` (`ReadAsync`, `WriteAsync`, `FlushAsync`), +`DeflateStream`, and `ZlibStream` — **done**. `GZipStream` was completed in the prior GZip batch. +`ZStandard/*`, `Reduce`, `Explode`, `Lzw/LzwStream` (`ReadAsync` 208 lines), `RLE90`, `Shrink`, +and `ArcLzw` remain. -- `ZlibBaseStream` needs `partial` added. -- `[SkipSyncVersion]` on all four Deflate-family `DisposeAsync`. +- `ZlibBaseStream` is partial. Deflate-family `DisposeAsync`, byte methods, unmatched memory + overloads, GZip-header parsing, and finalization remain hand-written. - `RunLength90Stream` is the canonical `SYNC_ONLY` case (see below). - `GZipCompressionProvider.cs:41` returns `new ValueTask(...)` from a non-`async` method — not a Zomp rewrite; make it `async` first. (Non-`async` methods that just *return* an `XAsync(...)` @@ -264,11 +272,12 @@ generated `ReadByte()`/`WriteByte()` are emitted without `override` and would hi Fully clean — these use raw `Stream`, **not** the async reader types. The least readable diffs in the repo, so do them last of the mechanical work. -### 12 — Providers · ~80 lines · low risk +### 12 — Providers · ~80 lines · low risk · **in progress** -`CompressionProviderRegistry` (4 identical pairs), `Default/*` providers (`XStream.CreateAsync(...)` -→ `Create(...)` maps cleanly), `CompressionProviderBase`, -`ContextRequiredDecompressionProviderBase`. Blocked on the `GZipCompressionProvider` rewrite above. +`CompressionProviderRegistry` (4 identical pairs) — **done**. `Default/*` providers +(`XStream.CreateAsync(...)` → `Create(...)` maps cleanly), `CompressionProviderBase`, and +`ContextRequiredDecompressionProviderBase` remain. The `GZipCompressionProvider` rewrite was +completed in the prior GZip batch. --- diff --git a/src/SharpCompress/Common/Zip/Headers/ZipFileEntry.Async.cs b/src/SharpCompress/Common/Zip/Headers/ZipFileEntry.Async.cs index f541fc8d..e43af8c7 100644 --- a/src/SharpCompress/Common/Zip/Headers/ZipFileEntry.Async.cs +++ b/src/SharpCompress/Common/Zip/Headers/ZipFileEntry.Async.cs @@ -7,6 +7,7 @@ namespace SharpCompress.Common.Zip.Headers; internal abstract partial class ZipFileEntry { + [Zomp.SyncMethodGenerator.CreateSyncVersion] internal async ValueTask ComposeEncryptionDataAsync( Stream archiveStream, CancellationToken cancellationToken = default @@ -15,7 +16,7 @@ internal abstract partial class ZipFileEntry ThrowHelper.ThrowIfNull(archiveStream); var buffer = new byte[12]; - await archiveStream.ReadFullyAsync(buffer, 0, 12, cancellationToken).ConfigureAwait(false); + await archiveStream.ReadFullyAsync(buffer, cancellationToken).ConfigureAwait(false); var encryptionData = PkwareTraditionalEncryptionData.ForRead(Password!, this, buffer); diff --git a/src/SharpCompress/Common/Zip/Headers/ZipFileEntry.cs b/src/SharpCompress/Common/Zip/Headers/ZipFileEntry.cs index e7f0b042..68fa65d3 100644 --- a/src/SharpCompress/Common/Zip/Headers/ZipFileEntry.cs +++ b/src/SharpCompress/Common/Zip/Headers/ZipFileEntry.cs @@ -42,18 +42,6 @@ internal abstract partial class ZipFileEntry(ZipHeaderType type, IArchiveEncodin public string? Password { get; set; } - internal PkwareTraditionalEncryptionData ComposeEncryptionData(Stream archiveStream) - { - ThrowHelper.ThrowIfNull(archiveStream); - - var buffer = new byte[12]; - archiveStream.ReadFully(buffer); - - var encryptionData = PkwareTraditionalEncryptionData.ForRead(Password!, this, buffer); - - return encryptionData; - } - internal WinzipAesEncryptionData? WinzipAesEncryptionData { get; set; } /// diff --git a/src/SharpCompress/Common/Zip/PkwareTraditionalCryptoStream.Async.cs b/src/SharpCompress/Common/Zip/PkwareTraditionalCryptoStream.Async.cs index 32a0a7bf..63abcf4d 100644 --- a/src/SharpCompress/Common/Zip/PkwareTraditionalCryptoStream.Async.cs +++ b/src/SharpCompress/Common/Zip/PkwareTraditionalCryptoStream.Async.cs @@ -8,6 +8,7 @@ namespace SharpCompress.Common.Zip; internal partial class PkwareTraditionalCryptoStream { + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override async Task ReadAsync( byte[] buffer, int offset, @@ -59,6 +60,7 @@ internal partial class PkwareTraditionalCryptoStream } #endif + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override async Task WriteAsync( byte[] buffer, int offset, diff --git a/src/SharpCompress/Common/Zip/PkwareTraditionalCryptoStream.cs b/src/SharpCompress/Common/Zip/PkwareTraditionalCryptoStream.cs index 2efd036e..9545d8de 100644 --- a/src/SharpCompress/Common/Zip/PkwareTraditionalCryptoStream.cs +++ b/src/SharpCompress/Common/Zip/PkwareTraditionalCryptoStream.cs @@ -41,49 +41,6 @@ internal partial class PkwareTraditionalCryptoStream : Stream set => throw new NotSupportedException(); } - public override int Read(byte[] buffer, int offset, int count) - { - if (_mode == CryptoMode.Encrypt) - { - throw new NotSupportedException("This stream does not encrypt via Read()"); - } - - ThrowHelper.ThrowIfNull(buffer); - - var temp = new byte[count]; - var readBytes = _stream.Read(temp, 0, count); - var decrypted = _encryptor.Decrypt(temp, readBytes); - Buffer.BlockCopy(decrypted, 0, buffer, offset, readBytes); - return readBytes; - } - - public override void Write(byte[] buffer, int offset, int count) - { - if (_mode == CryptoMode.Decrypt) - { - throw new NotSupportedException("This stream does not Decrypt via Write()"); - } - - if (count == 0) - { - return; - } - - byte[] plaintext; - if (offset != 0) - { - plaintext = new byte[count]; - Buffer.BlockCopy(buffer, offset, plaintext, 0, count); - } - else - { - plaintext = buffer; - } - - var encrypted = _encryptor.Encrypt(plaintext, count); - _stream.Write(encrypted, 0, encrypted.Length); - } - public override void Flush() { } public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); diff --git a/src/SharpCompress/Common/Zip/SeekableZipFilePart.Async.cs b/src/SharpCompress/Common/Zip/SeekableZipFilePart.Async.cs index 8fff8436..da9ea359 100644 --- a/src/SharpCompress/Common/Zip/SeekableZipFilePart.Async.cs +++ b/src/SharpCompress/Common/Zip/SeekableZipFilePart.Async.cs @@ -7,6 +7,7 @@ namespace SharpCompress.Common.Zip; internal partial class SeekableZipFilePart { + [Zomp.SyncMethodGenerator.CreateSyncVersion] internal override async ValueTask GetCompressedStreamAsync( CancellationToken cancellationToken = default ) @@ -19,6 +20,7 @@ internal partial class SeekableZipFilePart return await base.GetCompressedStreamAsync(cancellationToken).ConfigureAwait(false); } + [Zomp.SyncMethodGenerator.CreateSyncVersion] private async ValueTask LoadLocalHeaderAsync(CancellationToken cancellationToken = default) => Header = await _headerFactory .GetLocalHeaderAsync(BaseStream, (DirectoryEntryHeader)Header) diff --git a/src/SharpCompress/Common/Zip/SeekableZipFilePart.cs b/src/SharpCompress/Common/Zip/SeekableZipFilePart.cs index f9c59dfd..bed94da3 100644 --- a/src/SharpCompress/Common/Zip/SeekableZipFilePart.cs +++ b/src/SharpCompress/Common/Zip/SeekableZipFilePart.cs @@ -1,6 +1,5 @@ using System.IO; using SharpCompress.Common.Zip.Headers; -using SharpCompress.Compressors; using SharpCompress.Providers; namespace SharpCompress.Common.Zip; @@ -18,19 +17,6 @@ internal partial class SeekableZipFilePart : ZipFilePart ) : base(header, stream, compressionProviders) => _headerFactory = headerFactory; - internal override Stream GetCompressedStream() - { - if (!_isLocalHeaderLoaded) - { - LoadLocalHeader(); - _isLocalHeaderLoaded = true; - } - return base.GetCompressedStream(); - } - - private void LoadLocalHeader() => - Header = _headerFactory.GetLocalHeader(BaseStream, (DirectoryEntryHeader)Header); - protected override Stream CreateBaseStream() { BaseStream.Position = Header.DataStartPosition.NotNull(); diff --git a/src/SharpCompress/Common/Zip/StreamingZipFilePart.Async.cs b/src/SharpCompress/Common/Zip/StreamingZipFilePart.Async.cs index dbd62841..fa372826 100644 --- a/src/SharpCompress/Common/Zip/StreamingZipFilePart.Async.cs +++ b/src/SharpCompress/Common/Zip/StreamingZipFilePart.Async.cs @@ -7,6 +7,7 @@ namespace SharpCompress.Common.Zip; internal sealed partial class StreamingZipFilePart { + [Zomp.SyncMethodGenerator.CreateSyncVersion] internal override async ValueTask GetCompressedStreamAsync( CancellationToken cancellationToken = default ) @@ -15,17 +16,18 @@ internal sealed partial class StreamingZipFilePart { return Stream.Null; } - _decompressionStream = await CreateDecompressionStreamAsync( + var decompressionStream = await CreateDecompressionStreamAsync( await GetCryptoStreamAsync(CreateBaseStream(), cancellationToken) .ConfigureAwait(false), Header.CompressionMethod, cancellationToken ) .ConfigureAwait(false); + _decompressionStream = decompressionStream; if (LeaveStreamOpen) { - return SharpCompressStream.CreateNonDisposing(_decompressionStream); + return SharpCompressStream.CreateNonDisposing(decompressionStream); } - return _decompressionStream; + return decompressionStream; } } diff --git a/src/SharpCompress/Common/Zip/StreamingZipFilePart.cs b/src/SharpCompress/Common/Zip/StreamingZipFilePart.cs index e8ef7c55..9240e16c 100644 --- a/src/SharpCompress/Common/Zip/StreamingZipFilePart.cs +++ b/src/SharpCompress/Common/Zip/StreamingZipFilePart.cs @@ -1,6 +1,5 @@ using System.IO; using SharpCompress.Common.Zip.Headers; -using SharpCompress.Compressors; using SharpCompress.IO; using SharpCompress.Providers; @@ -19,23 +18,6 @@ internal sealed partial class StreamingZipFilePart : ZipFilePart protected override Stream CreateBaseStream() => Header.PackedStream.NotNull(); - internal override Stream GetCompressedStream() - { - if (!Header.HasData) - { - return Stream.Null; - } - _decompressionStream = CreateDecompressionStream( - GetCryptoStream(CreateBaseStream()), - Header.CompressionMethod - ); - if (LeaveStreamOpen) - { - return SharpCompressStream.CreateNonDisposing(_decompressionStream); - } - return _decompressionStream; - } - internal BinaryReader FixStreamedFileLocation(ref Stream stream) { if (Header.IsDirectory) @@ -45,12 +27,12 @@ internal sealed partial class StreamingZipFilePart : ZipFilePart if (Header.HasData && !Skipped) { - _decompressionStream ??= GetCompressedStream(); + var decompressionStream = _decompressionStream ??= GetCompressedStream().NotNull(); - _decompressionStream.Skip(); + decompressionStream.Skip(); // If we had TotalIn / TotalOut we could have used them - Header.CompressedSize = _decompressionStream.Position; + Header.CompressedSize = decompressionStream.Position; Skipped = true; } diff --git a/src/SharpCompress/Common/Zip/WinzipAesCryptoStream.Async.cs b/src/SharpCompress/Common/Zip/WinzipAesCryptoStream.Async.cs index d68b8a87..04863349 100644 --- a/src/SharpCompress/Common/Zip/WinzipAesCryptoStream.Async.cs +++ b/src/SharpCompress/Common/Zip/WinzipAesCryptoStream.Async.cs @@ -32,6 +32,7 @@ internal partial class WinzipAesCryptoStream } #endif + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override async Task ReadAsync( byte[] buffer, int offset, diff --git a/src/SharpCompress/Common/Zip/WinzipAesCryptoStream.cs b/src/SharpCompress/Common/Zip/WinzipAesCryptoStream.cs index 9d269473..f6343c02 100644 --- a/src/SharpCompress/Common/Zip/WinzipAesCryptoStream.cs +++ b/src/SharpCompress/Common/Zip/WinzipAesCryptoStream.cs @@ -104,25 +104,6 @@ internal partial class WinzipAesCryptoStream : Stream public override void Flush() { } - public override int Read(byte[] buffer, int offset, int count) - { - if (_totalBytesLeftToRead == 0) - { - return 0; - } - var bytesToRead = count; - if (count > _totalBytesLeftToRead) - { - bytesToRead = (int)_totalBytesLeftToRead; - } - var read = _stream.Read(buffer, offset, bytesToRead); - _totalBytesLeftToRead -= read; - - ReadTransformBlocks(buffer, offset, read); - - return read; - } - private void FillCounterOut() { // update the counter diff --git a/src/SharpCompress/Common/Zip/ZipFilePart.Async.cs b/src/SharpCompress/Common/Zip/ZipFilePart.Async.cs index 29f523a6..2902de0b 100644 --- a/src/SharpCompress/Common/Zip/ZipFilePart.Async.cs +++ b/src/SharpCompress/Common/Zip/ZipFilePart.Async.cs @@ -13,6 +13,7 @@ namespace SharpCompress.Common.Zip; internal abstract partial class ZipFilePart { + [Zomp.SyncMethodGenerator.CreateSyncVersion] internal override async ValueTask GetCompressedStreamAsync( CancellationToken cancellationToken = default ) @@ -35,6 +36,7 @@ internal abstract partial class ZipFilePart return decompressionStream; } + [Zomp.SyncMethodGenerator.CreateSyncVersion] protected async ValueTask GetCryptoStreamAsync( Stream plainStream, CancellationToken cancellationToken = default @@ -242,6 +244,7 @@ internal abstract partial class ZipFilePart } } + [Zomp.SyncMethodGenerator.CreateSyncVersion] private async ValueTask CreateWinzipAesDecompressionStreamAsync( Stream stream, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Common/Zip/ZipFilePart.cs b/src/SharpCompress/Common/Zip/ZipFilePart.cs index de182f37..8597b825 100644 --- a/src/SharpCompress/Common/Zip/ZipFilePart.cs +++ b/src/SharpCompress/Common/Zip/ZipFilePart.cs @@ -1,19 +1,6 @@ using System; -using System.Buffers.Binary; using System.IO; -using System.Linq; using SharpCompress.Common.Zip.Headers; -using SharpCompress.Compressors; -using SharpCompress.Compressors.BZip2; -using SharpCompress.Compressors.Deflate; -using SharpCompress.Compressors.Deflate64; -using SharpCompress.Compressors.Explode; -using SharpCompress.Compressors.LZMA; -using SharpCompress.Compressors.PPMd; -using SharpCompress.Compressors.Reduce; -using SharpCompress.Compressors.Shrink; -using SharpCompress.Compressors.Xz; -using SharpCompress.Compressors.ZStandard; using SharpCompress.IO; using SharpCompress.Providers; @@ -41,23 +28,6 @@ internal abstract partial class ZipFilePart : FilePart internal override string? FilePartName => Header.Name; - internal override Stream GetCompressedStream() - { - if (!Header.HasData) - { - return Stream.Null; - } - var decompressionStream = CreateDecompressionStream( - GetCryptoStream(CreateBaseStream()), - Header.CompressionMethod - ); - if (LeaveStreamOpen) - { - return SharpCompressStream.CreateNonDisposing(decompressionStream); - } - return decompressionStream; - } - internal override Stream GetRawStream() { if (!Header.HasData) @@ -192,101 +162,4 @@ internal abstract partial class ZipFilePart : FilePart // For simple methods, use the basic decompress return providers.CreateDecompressStream(compressionType, stream, context); } - - private Stream CreateWinzipAesDecompressionStream(Stream stream) - { - var data = Header.Extra.SingleOrDefault(x => x.Type == ExtraDataType.WinZipAes); - if (data is null) - { - throw new InvalidFormatException("No Winzip AES extra data found."); - } - if (data.Length != 7) - { - throw new InvalidFormatException("Winzip data length is not 7."); - } - var compressedMethod = BinaryPrimitives.ReadUInt16LittleEndian(data.DataBytes); - - if (compressedMethod != 0x01 && compressedMethod != 0x02) - { - throw new InvalidFormatException( - "Unexpected vendor version number for WinZip AES metadata" - ); - } - - var vendorId = BinaryPrimitives.ReadUInt16LittleEndian(data.DataBytes.AsSpan(2)); - if (vendorId != 0x4541) - { - throw new InvalidFormatException("Unexpected vendor ID for WinZip AES metadata"); - } - return CreateDecompressionStream( - stream, - (ZipCompressionMethod)BinaryPrimitives.ReadUInt16LittleEndian(data.DataBytes.AsSpan(5)) - ); - } - - protected Stream GetCryptoStream(Stream plainStream) - { - var isFileEncrypted = FlagUtility.HasFlag(Header.Flags, HeaderFlags.Encrypted); - - if (Header.CompressedSize == 0 && isFileEncrypted) - { - throw new NotSupportedException("Cannot encrypt file with unknown size at start."); - } - - if ( - Header.CompressedSize == 0 - && FlagUtility.HasFlag(Header.Flags, HeaderFlags.UsePostDataDescriptor) - ) - { - plainStream = SharpCompressStream.CreateNonDisposing(plainStream); //make sure AES doesn't close - } - else - { - plainStream = new ReadOnlySubStream(plainStream, Header.CompressedSize); //make sure AES doesn't close - } - - if (isFileEncrypted) - { - switch (Header.CompressionMethod) - { - case ZipCompressionMethod.None: - case ZipCompressionMethod.Shrink: - case ZipCompressionMethod.Reduce1: - case ZipCompressionMethod.Reduce2: - case ZipCompressionMethod.Reduce3: - case ZipCompressionMethod.Reduce4: - case ZipCompressionMethod.Deflate: - case ZipCompressionMethod.Deflate64: - case ZipCompressionMethod.BZip2: - case ZipCompressionMethod.LZMA: - case ZipCompressionMethod.PPMd: - { - return new PkwareTraditionalCryptoStream( - plainStream, - Header.ComposeEncryptionData(plainStream), - CryptoMode.Decrypt - ); - } - - case ZipCompressionMethod.WinzipAes: - { - if (Header.WinzipAesEncryptionData != null) - { - return new WinzipAesCryptoStream( - plainStream, - Header.WinzipAesEncryptionData, - Header.CompressedSize - 10 - ); - } - return plainStream; - } - - default: - { - throw new ArchiveOperationException("Header.CompressionMethod is invalid"); - } - } - } - return plainStream; - } } diff --git a/src/SharpCompress/Compressors/Deflate/DeflateStream.Async.cs b/src/SharpCompress/Compressors/Deflate/DeflateStream.Async.cs index 454ba022..ae6343cd 100644 --- a/src/SharpCompress/Compressors/Deflate/DeflateStream.Async.cs +++ b/src/SharpCompress/Compressors/Deflate/DeflateStream.Async.cs @@ -29,6 +29,10 @@ public partial class DeflateStream #endif } + /// + /// Flush the stream. + /// + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override async Task FlushAsync(CancellationToken cancellationToken) { if (_disposed) @@ -38,6 +42,24 @@ public partial class DeflateStream await _baseStream.FlushAsync(cancellationToken).ConfigureAwait(false); } + /// + /// Read data from the stream. + /// + /// + /// + /// If you wish to use the DeflateStream to compress data while reading, create it with + /// CompressionMode.Compress and an uncompressed data stream. Reading then returns + /// compressed data. With CompressionMode.Decompress, reading returns decompressed data. + /// + /// + /// A DeflateStream can be used for Read() or Write(), but not both. + /// + /// + /// The buffer into which the read data should be placed. + /// The offset within that data array to put the first byte read. + /// The number of bytes to read. + /// The number of bytes actually read. + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override async Task ReadAsync( byte[] buffer, int offset, @@ -68,6 +90,23 @@ public partial class DeflateStream } #endif + /// + /// Write data to the stream. + /// + /// + /// + /// With CompressionMode.Compress, written uncompressed data is compressed to the + /// destination. With CompressionMode.Decompress, written compressed data is decompressed + /// to the destination. + /// + /// + /// A DeflateStream can be used for Read() or Write(), but not both. + /// + /// + /// The buffer holding data to write to the stream. + /// The offset within that data array to find the first byte to write. + /// The number of bytes to write. + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override async Task WriteAsync( byte[] buffer, int offset, diff --git a/src/SharpCompress/Compressors/Deflate/DeflateStream.cs b/src/SharpCompress/Compressors/Deflate/DeflateStream.cs index 13f3b356..47ab0c7f 100644 --- a/src/SharpCompress/Compressors/Deflate/DeflateStream.cs +++ b/src/SharpCompress/Compressors/Deflate/DeflateStream.cs @@ -27,8 +27,6 @@ using System; using System.IO; using System.Text; -using System.Threading; -using System.Threading.Tasks; using SharpCompress.Common; using SharpCompress.IO; @@ -270,54 +268,6 @@ public partial class DeflateStream : Stream, IStreamStack Stream IStreamStack.BaseStream() => _baseStream; - /// - /// Flush the stream. - /// - public override void Flush() - { - if (_disposed) - { - throw new ObjectDisposedException("DeflateStream"); - } - _baseStream.Flush(); - } - - /// - /// Read data from the stream. - /// - /// - /// - /// - /// If you wish to use the DeflateStream to compress data while - /// reading, you can create a DeflateStream with - /// CompressionMode.Compress, providing an uncompressed data stream. - /// Then call Read() on that DeflateStream, and the data read will be - /// compressed as you read. If you wish to use the DeflateStream to - /// decompress data while reading, you can create a DeflateStream with - /// CompressionMode.Decompress, providing a readable compressed data - /// stream. Then call Read() on that DeflateStream, and the data read - /// will be decompressed as you read. - /// - /// - /// - /// A DeflateStream can be used for Read() or Write(), but not both. - /// - /// - /// - /// The buffer into which the read data should be placed. - /// the offset within that data array to put the first byte read. - /// the number of bytes to read. - /// the number of bytes actually read - public override int Read(byte[] buffer, int offset, int count) - { - if (_disposed) - { - throw new ObjectDisposedException("DeflateStream"); - } - - return _baseStream.Read(buffer, offset, count); - } - public override int ReadByte() { if (_disposed) @@ -341,44 +291,6 @@ public partial class DeflateStream : Stream, IStreamStack /// this is irrelevant, since it will always throw! public override void SetLength(long value) => throw new NotSupportedException(); - /// - /// Write data to the stream. - /// - /// - /// - /// - /// If you wish to use the DeflateStream to compress data while - /// writing, you can create a DeflateStream with - /// CompressionMode.Compress, and a writable output stream. Then call - /// Write() on that DeflateStream, providing uncompressed data - /// as input. The data sent to the output stream will be the compressed form - /// of the data written. If you wish to use the DeflateStream to - /// decompress data while writing, you can create a DeflateStream with - /// CompressionMode.Decompress, and a writable output stream. Then - /// call Write() on that stream, providing previously compressed - /// data. The data sent to the output stream will be the decompressed form of - /// the data written. - /// - /// - /// - /// A DeflateStream can be used for Read() or Write(), - /// but not both. - /// - /// - /// - /// - /// The buffer holding data to write to the stream. - /// the offset within that data array to find the first byte to write. - /// the number of bytes to write. - public override void Write(byte[] buffer, int offset, int count) - { - if (_disposed) - { - throw new ObjectDisposedException("DeflateStream"); - } - _baseStream.Write(buffer, offset, count); - } - public override void WriteByte(byte value) { if (_disposed) diff --git a/src/SharpCompress/Compressors/Deflate/ZlibBaseStream.cs b/src/SharpCompress/Compressors/Deflate/ZlibBaseStream.cs index 04ac63cf..4815ec33 100644 --- a/src/SharpCompress/Compressors/Deflate/ZlibBaseStream.cs +++ b/src/SharpCompress/Compressors/Deflate/ZlibBaseStream.cs @@ -47,7 +47,7 @@ internal enum ZlibStreamFlavor GZIP = 1952, } -internal class ZlibBaseStream : Stream, IStreamStack +internal partial class ZlibBaseStream : Stream, IStreamStack { Stream IStreamStack.BaseStream() => _stream; @@ -139,58 +139,7 @@ internal class ZlibBaseStream : Stream, IStreamStack private byte[] workingBuffer => _workingBuffer ??= ArrayPool.Shared.Rent(_bufferSize); - public override void Write(byte[] buffer, int offset, int count) - { - // workitem 7159 - // calculate the CRC on the unccompressed data (before writing) - if (crc != null) - { - crc.SlurpBlock(buffer, offset, count); - } - - if (_streamMode == StreamMode.Undefined) - { - _streamMode = StreamMode.Writer; - } - else if (_streamMode != StreamMode.Writer) - { - throw new ZlibException("Cannot Write after Reading."); - } - - if (count == 0) - { - return; - } - - // first reference of z property will initialize the private var _z - z.InputBuffer = buffer; - _z.NextIn = offset; - _z.AvailableBytesIn = count; - var done = false; - do - { - _z.OutputBuffer = workingBuffer; - _z.NextOut = 0; - _z.AvailableBytesOut = _workingBuffer.Length; - var rc = (_wantCompress) ? _z.Deflate(_flushMode) : _z.Inflate(_flushMode); - if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END) - { - throw new ZlibException((_wantCompress ? "de" : "in") + "flating: " + _z.Message); - } - - //if (_workingBuffer.Length - _z.AvailableBytesOut > 0) - _stream.Write(_workingBuffer, 0, _workingBuffer.Length - _z.AvailableBytesOut); - - done = _z.AvailableBytesIn == 0 && _z.AvailableBytesOut != 0; - - // If GZIP and de-compress, we're done when 8 bytes remain. - if (_flavor == ZlibStreamFlavor.GZIP && !_wantCompress) - { - done = (_z.AvailableBytesIn == 8 && _z.AvailableBytesOut != 0); - } - } while (!done); - } - + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override async Task WriteAsync( byte[] buffer, int offset, @@ -607,27 +556,7 @@ internal class ZlibBaseStream : Stream, IStreamStack _workingBuffer = null; } - public override void Flush() - { - // Only flush the underlying stream when in write mode - // Flushing input streams during read operations is not meaningful - // and can cause issues with forward-only/non-seekable streams - if (_streamMode == StreamMode.Writer) - { - _stream.Flush(); - } - else if (z.AvailableBytesIn > 0) - { - // Rewind the underlying stream by the number of unconsumed bytes in the buffer - // This handles the case where the decompressor over-read past the end of the entry - if (_stream is IStreamStack stack) - { - stack.RewindBytes(z.AvailableBytesIn); - } - z.AvailableBytesIn = 0; - } - } - + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override async Task FlushAsync(CancellationToken cancellationToken) { // Only flush the underlying stream when in write mode @@ -670,31 +599,7 @@ internal class ZlibBaseStream : Stream, IStreamStack private bool nomoreinput; private bool isDisposed; - private string ReadZeroTerminatedString() - { - var list = new List(); - var done = false; - do - { - // workitem 7740 - var n = _stream.Read(_buf1, 0, 1); - if (n != 1) - { - throw new ZlibException("Unexpected EOF reading GZIP header."); - } - if (_buf1[0] == 0) - { - done = true; - } - else - { - list.Add(_buf1[0]); - } - } while (!done); - var buffer = list.ToArray(); - return _encoding.GetString(buffer, 0, buffer.Length); - } - + [Zomp.SyncMethodGenerator.CreateSyncVersion] private async ValueTask ReadZeroTerminatedStringAsync( CancellationToken cancellationToken ) @@ -844,192 +749,7 @@ internal class ZlibBaseStream : Stream, IStreamStack return totalBytesRead; } - public override Int32 Read(Byte[] buffer, Int32 offset, Int32 count) - { - // According to MS documentation, any implementation of the IO.Stream.Read function must: - // (a) throw an exception if offset & count reference an invalid part of the buffer, - // or if count < 0, or if buffer is null - // (b) return 0 only upon EOF, or if count = 0 - // (c) if not EOF, then return at least 1 byte, up to bytes - - if (_streamMode == StreamMode.Undefined) - { - if (!_stream.CanRead) - { - throw new ZlibException("The stream is not readable."); - } - - // for the first read, set up some controls. - _streamMode = StreamMode.Reader; - - // (The first reference to _z goes through the private accessor which - // may initialize it.) - z.AvailableBytesIn = 0; - if (_flavor == ZlibStreamFlavor.GZIP) - { - _gzipHeaderByteCount = _ReadAndValidateGzipHeader(); - - // workitem 8501: handle edge case (decompress empty stream) - if (_gzipHeaderByteCount == 0) - { - return 0; - } - } - } - - if (_streamMode != StreamMode.Reader) - { - throw new ZlibException("Cannot Read after Writing."); - } - - var rc = 0; - - // set up the output of the deflate/inflate codec: - _z.OutputBuffer = buffer; - _z.NextOut = offset; - _z.AvailableBytesOut = count; - - if (count == 0) - { - return 0; - } - if (nomoreinput && _wantCompress) - { - // no more input data available; therefore we flush to - // try to complete the read - rc = _z.Deflate(FlushType.Finish); - - if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END) - { - throw new ZlibException( - String.Format( - Constants.DefaultCultureInfo, - "Deflating: rc={0} msg={1}", - rc, - _z.Message - ) - ); - } - - rc = (count - _z.AvailableBytesOut); - - // calculate CRC after reading - if (crc != null) - { - crc.SlurpBlock(buffer, offset, rc); - } - - return rc; - } - ThrowHelper.ThrowIfNull(buffer); - ThrowHelper.ThrowIfNegative(count); - ThrowHelper.ThrowIfLessThan(offset, buffer.GetLowerBound(0)); - if ((offset + count) > buffer.GetLength(0)) - { - throw new ArgumentOutOfRangeException(nameof(count)); - } - - // This is necessary in case _workingBuffer has been resized. (new byte[]) - // (The first reference to _workingBuffer goes through the private accessor which - // may initialize it.) - _z.InputBuffer = workingBuffer; - - do - { - // need data in _workingBuffer in order to deflate/inflate. Here, we check if we have any. - if ((_z.AvailableBytesIn == 0) && (!nomoreinput)) - { - // No data available, so try to Read data from the captive stream. - _z.NextIn = 0; - _z.AvailableBytesIn = _stream.Read(_workingBuffer, 0, _workingBuffer.Length); - if (_z.AvailableBytesIn == 0) - { - nomoreinput = true; - } - } - - // we have data in InputBuffer; now compress or decompress as appropriate - rc = (_wantCompress) ? _z.Deflate(_flushMode) : _z.Inflate(_flushMode); - - if (nomoreinput && (rc == ZlibConstants.Z_BUF_ERROR)) - { - return 0; - } - - if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END) - { - throw new ZlibException( - String.Format( - Constants.DefaultCultureInfo, - "{0}flating: rc={1} msg={2}", - (_wantCompress ? "de" : "in"), - rc, - _z.Message - ) - ); - } - - if ( - (nomoreinput || rc == ZlibConstants.Z_STREAM_END) && (_z.AvailableBytesOut == count) - ) - { - break; // nothing more to read - } - } //while (_z.AvailableBytesOut == count && rc == ZlibConstants.Z_OK); - while (_z.AvailableBytesOut > 0 && !nomoreinput && rc == ZlibConstants.Z_OK); - - // workitem 8557 - // is there more room in output? - if (_z.AvailableBytesOut > 0) - { - if (rc == ZlibConstants.Z_OK && _z.AvailableBytesIn == 0) - { - // deferred - } - - // are we completely done reading? - if (nomoreinput) - { - // and in compression? - if (_wantCompress) - { - // no more input data available; therefore we flush to - // try to complete the read - rc = _z.Deflate(FlushType.Finish); - - if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END) - { - throw new ZlibException( - String.Format( - Constants.DefaultCultureInfo, - "Deflating: rc={0} msg={1}", - rc, - _z.Message - ) - ); - } - } - } - } - - rc = (count - _z.AvailableBytesOut); - - // calculate CRC after reading - if (crc != null) - { - crc.SlurpBlock(buffer, offset, rc); - } - - if (rc == ZlibConstants.Z_STREAM_END && z.AvailableBytesIn != 0 && !_wantCompress) - { - //rewind the buffer - this.RewindBytes(z.AvailableBytesIn); - z.AvailableBytesIn = 0; - } - - return rc; - } - + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override async Task ReadAsync( byte[] buffer, int offset, diff --git a/src/SharpCompress/Compressors/Deflate/ZlibStream.Async.cs b/src/SharpCompress/Compressors/Deflate/ZlibStream.Async.cs index 0dee2197..058bd28e 100644 --- a/src/SharpCompress/Compressors/Deflate/ZlibStream.Async.cs +++ b/src/SharpCompress/Compressors/Deflate/ZlibStream.Async.cs @@ -8,6 +8,10 @@ namespace SharpCompress.Compressors.Deflate; public partial class ZlibStream { + /// + /// Flush the stream. + /// + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override async Task FlushAsync(CancellationToken cancellationToken) { if (_disposed) @@ -33,6 +37,18 @@ public partial class ZlibStream } #endif + /// + /// Read data from the stream. + /// + /// + /// A ZlibStream compresses or decompresses according to its + /// CompressionMode. It can be used for Read() or Write(), but not both. + /// + /// The buffer into which the read data should be placed. + /// The offset within that data array to put the first byte read. + /// The number of bytes to read. + /// The number of bytes actually read. + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override async Task ReadAsync( byte[] buffer, int offset, @@ -63,6 +79,17 @@ public partial class ZlibStream } #endif + /// + /// Write data to the stream. + /// + /// + /// A ZlibStream compresses or decompresses according to its + /// CompressionMode. It can be used for Read() or Write(), but not both. + /// + /// The buffer holding data to write to the stream. + /// The offset within that data array to find the first byte to write. + /// The number of bytes to write. + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override async Task WriteAsync( byte[] buffer, int offset, diff --git a/src/SharpCompress/Compressors/Deflate/ZlibStream.cs b/src/SharpCompress/Compressors/Deflate/ZlibStream.cs index c4652d44..580961d7 100644 --- a/src/SharpCompress/Compressors/Deflate/ZlibStream.cs +++ b/src/SharpCompress/Compressors/Deflate/ZlibStream.cs @@ -28,8 +28,6 @@ using System; using System.IO; using System.Text; -using System.Threading; -using System.Threading.Tasks; using SharpCompress.Common; namespace SharpCompress.Compressors.Deflate; @@ -231,53 +229,6 @@ public partial class ZlibStream : Stream } } - /// - /// Flush the stream. - /// - public override void Flush() - { - if (_disposed) - { - throw new ObjectDisposedException("ZlibStream"); - } - _baseStream.Flush(); - } - - /// - /// Read data from the stream. - /// - /// - /// - /// - /// - /// If you wish to use the ZlibStream to compress data while reading, - /// you can create a ZlibStream with CompressionMode.Compress, - /// providing an uncompressed data stream. Then call Read() on that - /// ZlibStream, and the data read will be compressed. If you wish to - /// use the ZlibStream to decompress data while reading, you can create - /// a ZlibStream with CompressionMode.Decompress, providing a - /// readable compressed data stream. Then call Read() on that - /// ZlibStream, and the data will be decompressed as it is read. - /// - /// - /// - /// A ZlibStream can be used for Read() or Write(), but - /// not both. - /// - /// - /// - /// The buffer into which the read data should be placed. - /// the offset within that data array to put the first byte read. - /// the number of bytes to read. - public override int Read(byte[] buffer, int offset, int count) - { - if (_disposed) - { - throw new ObjectDisposedException("ZlibStream"); - } - return _baseStream.Read(buffer, offset, count); - } - public override int ReadByte() { if (_disposed) @@ -297,41 +248,6 @@ public partial class ZlibStream : Stream /// public override void SetLength(long value) => throw new NotSupportedException(); - /// - /// Write data to the stream. - /// - /// - /// - /// - /// - /// If you wish to use the ZlibStream to compress data while writing, - /// you can create a ZlibStream with CompressionMode.Compress, - /// and a writable output stream. Then call Write() on that - /// ZlibStream, providing uncompressed data as input. The data sent to - /// the output stream will be the compressed form of the data written. If you - /// wish to use the ZlibStream to decompress data while writing, you - /// can create a ZlibStream with CompressionMode.Decompress, and a - /// writable output stream. Then call Write() on that stream, - /// providing previously compressed data. The data sent to the output stream - /// will be the decompressed form of the data written. - /// - /// - /// - /// A ZlibStream can be used for Read() or Write(), but not both. - /// - /// - /// The buffer holding data to write to the stream. - /// the offset within that data array to find the first byte to write. - /// the number of bytes to write. - public override void Write(byte[] buffer, int offset, int count) - { - if (_disposed) - { - throw new ObjectDisposedException("ZlibStream"); - } - _baseStream.Write(buffer, offset, count); - } - public override void WriteByte(byte value) { if (_disposed) diff --git a/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.Async.cs b/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.Async.cs index 64652872..dbb8afa6 100644 --- a/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.Async.cs +++ b/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.Async.cs @@ -10,6 +10,7 @@ namespace SharpCompress.Compressors.Deflate64; public sealed partial class Deflate64Stream { + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override async Task ReadAsync( byte[] buffer, int offset, diff --git a/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.cs b/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.cs index 9e9ff7db..8d06436f 100644 --- a/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.cs +++ b/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.cs @@ -5,10 +5,7 @@ using System; using System.IO; using System.Runtime.CompilerServices; -using System.Threading; -using System.Threading.Tasks; using SharpCompress.Common; -using SharpCompress.Common.Zip; namespace SharpCompress.Compressors.Deflate64; @@ -69,50 +66,6 @@ public sealed partial class Deflate64Stream : Stream public override void SetLength(long value) => throw new NotSupportedException("Deflate64: not supported"); - public override int Read(byte[] buffer, int offset, int count) - { - ValidateParameters(buffer, offset, count); - EnsureNotDisposed(); - - int bytesRead; - var currentOffset = offset; - var remainingCount = count; - - while (true) - { - bytesRead = _inflater.Inflate(buffer, currentOffset, remainingCount); - currentOffset += bytesRead; - remainingCount -= bytesRead; - - if (remainingCount == 0) - { - break; - } - - if (_inflater.Finished()) - { - // if we finished decompressing, we can't have anything left in the outputwindow. - break; - } - - var bytes = _stream.Read(_buffer, 0, _buffer.Length); - if (bytes <= 0) - { - break; - } - else if (bytes > _buffer.Length) - { - // The stream is either malicious or poorly implemented and returned a number of - // bytes larger than the buffer supplied to it. - throw new InvalidFormatException("Deflate64: invalid data"); - } - - _inflater.SetInput(_buffer, 0, bytes); - } - - return count - remainingCount; - } - private void ValidateParameters(byte[] array, int offset, int count) { ThrowHelper.ThrowIfNull(array); diff --git a/src/SharpCompress/Providers/CompressionProviderRegistry.cs b/src/SharpCompress/Providers/CompressionProviderRegistry.cs index ea0e217a..289487a0 100644 --- a/src/SharpCompress/Providers/CompressionProviderRegistry.cs +++ b/src/SharpCompress/Providers/CompressionProviderRegistry.cs @@ -26,7 +26,7 @@ namespace SharpCompress.Providers; /// }; /// /// -public sealed class CompressionProviderRegistry +public sealed partial class CompressionProviderRegistry { /// /// The default registry using SharpCompress internal implementations. @@ -61,103 +61,11 @@ public sealed class CompressionProviderRegistry /// The compression type. /// The destination stream. /// The compression level. - /// A compression stream. - /// If no provider is registered for the type. - /// If the provider does not support compression. - public Stream CreateCompressStream(CompressionType type, Stream destination, int level) - { - var provider = GetProvider(type); - if (provider is null) - { - throw new ArchiveOperationException( - $"No compression provider registered for type: {type}" - ); - } - return provider.CreateCompressStream(destination, level); - } - - /// - /// Creates a decompression stream for the specified type. - /// - /// The compression type. - /// The source stream. - /// A decompression stream. - /// If no provider is registered for the type. - /// If the provider does not support decompression. - public Stream CreateDecompressStream(CompressionType type, Stream source) - { - var provider = GetProvider(type); - if (provider is null) - { - throw new ArchiveOperationException( - $"No compression provider registered for type: {type}" - ); - } - return provider.CreateDecompressStream(source); - } - - /// - /// Creates a compression stream for the specified type with context. - /// - /// The compression type. - /// The destination stream. - /// The compression level. - /// Context information for the compression. - /// A compression stream. - /// If no provider is registered for the type. - /// If the provider does not support compression. - public Stream CreateCompressStream( - CompressionType type, - Stream destination, - int level, - CompressionContext context - ) - { - var provider = GetProvider(type); - if (provider is null) - { - throw new ArchiveOperationException( - $"No compression provider registered for type: {type}" - ); - } - return provider.CreateCompressStream(destination, level, context); - } - - /// - /// Creates a decompression stream for the specified type with context. - /// - /// The compression type. - /// The source stream. - /// Context information for the decompression. - /// A decompression stream. - /// If no provider is registered for the type. - /// If the provider does not support decompression. - public Stream CreateDecompressStream( - CompressionType type, - Stream source, - CompressionContext context - ) - { - var provider = GetProvider(type); - if (provider is null) - { - throw new ArchiveOperationException( - $"No compression provider registered for type: {type}" - ); - } - return provider.CreateDecompressStream(source, context); - } - - /// - /// Asynchronously creates a compression stream for the specified type. - /// - /// The compression type. - /// The destination stream. - /// The compression level. /// Cancellation token. - /// A task containing the compression stream. + /// A compression stream. /// If no provider is registered for the type. /// If the provider does not support compression. + [Zomp.SyncMethodGenerator.CreateSyncVersion] public ValueTask CreateCompressStreamAsync( CompressionType type, Stream destination, @@ -176,14 +84,15 @@ public sealed class CompressionProviderRegistry } /// - /// Asynchronously creates a decompression stream for the specified type. + /// Creates a decompression stream for the specified type. /// /// The compression type. /// The source stream. /// Cancellation token. - /// A task containing the decompression stream. + /// A decompression stream. /// If no provider is registered for the type. /// If the provider does not support decompression. + [Zomp.SyncMethodGenerator.CreateSyncVersion] public ValueTask CreateDecompressStreamAsync( CompressionType type, Stream source, @@ -201,16 +110,17 @@ public sealed class CompressionProviderRegistry } /// - /// Asynchronously creates a compression stream for the specified type with context. + /// Creates a compression stream for the specified type with context. /// /// The compression type. /// The destination stream. /// The compression level. /// Context information for the compression. /// Cancellation token. - /// A task containing the compression stream. + /// A compression stream. /// If no provider is registered for the type. /// If the provider does not support compression. + [Zomp.SyncMethodGenerator.CreateSyncVersion] public ValueTask CreateCompressStreamAsync( CompressionType type, Stream destination, @@ -230,15 +140,16 @@ public sealed class CompressionProviderRegistry } /// - /// Asynchronously creates a decompression stream for the specified type with context. + /// Creates a decompression stream for the specified type with context. /// /// The compression type. /// The source stream. /// Context information for the decompression. /// Cancellation token. - /// A task containing the decompression stream. + /// A decompression stream. /// If no provider is registered for the type. /// If the provider does not support decompression. + [Zomp.SyncMethodGenerator.CreateSyncVersion] public ValueTask CreateDecompressStreamAsync( CompressionType type, Stream source, diff --git a/src/SharpCompress/Writers/Zip/ZipWriter.Async.cs b/src/SharpCompress/Writers/Zip/ZipWriter.Async.cs index b934c6e1..ec0d9ca4 100644 --- a/src/SharpCompress/Writers/Zip/ZipWriter.Async.cs +++ b/src/SharpCompress/Writers/Zip/ZipWriter.Async.cs @@ -47,8 +47,9 @@ public partial class ZipWriter } /// - /// Asynchronously writes an entry to the ZIP archive. + /// Writes an entry to the ZIP archive. /// + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override async ValueTask WriteAsync( string filename, Stream source, @@ -56,7 +57,9 @@ public partial class ZipWriter CancellationToken cancellationToken = default ) { +#if !SYNC_ONLY cancellationToken.ThrowIfCancellationRequested(); +#endif await WriteAsync( filename, source, @@ -67,8 +70,9 @@ public partial class ZipWriter } /// - /// Asynchronously writes an entry to the ZIP archive with specified options. + /// Writes an entry to the ZIP archive with specified options. /// + [Zomp.SyncMethodGenerator.CreateSyncVersion] public async ValueTask WriteAsync( string entryPath, Stream source, @@ -76,7 +80,9 @@ public partial class ZipWriter CancellationToken cancellationToken = default ) { +#if !SYNC_ONLY cancellationToken.ThrowIfCancellationRequested(); +#endif await using var output = await WriteToStreamAsync( entryPath, zipWriterEntryOptions, @@ -84,7 +90,9 @@ public partial class ZipWriter ) .ConfigureAwait(false); var progressStream = WrapWithProgress(source, entryPath); - await progressStream.CopyToAsync(output, 81920, cancellationToken).ConfigureAwait(false); + await progressStream + .CopyToAsync(output, WriterOptions.BufferSize, cancellationToken) + .ConfigureAwait(false); } private async ValueTask WriteToStreamAsync( @@ -168,16 +176,18 @@ public partial class ZipWriter } /// - /// Asynchronously writes a directory entry to the ZIP archive. - /// Uses synchronous implementation for directory entries as they are lightweight. + /// Writes a directory entry to the ZIP archive. /// + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override async ValueTask WriteDirectoryAsync( string directoryName, DateTime? modificationTime, CancellationToken cancellationToken = default ) { +#if !SYNC_ONLY cancellationToken.ThrowIfCancellationRequested(); +#endif var normalizedName = NormalizeDirectoryName(directoryName); if (string.IsNullOrEmpty(normalizedName)) @@ -190,6 +200,7 @@ public partial class ZipWriter .ConfigureAwait(false); } + [Zomp.SyncMethodGenerator.CreateSyncVersion] private async ValueTask WriteDirectoryEntryAsync( string directoryPath, ZipWriterEntryOptions options, diff --git a/src/SharpCompress/Writers/Zip/ZipWriter.cs b/src/SharpCompress/Writers/Zip/ZipWriter.cs index 8775253c..f9640c32 100644 --- a/src/SharpCompress/Writers/Zip/ZipWriter.cs +++ b/src/SharpCompress/Writers/Zip/ZipWriter.cs @@ -77,20 +77,6 @@ public partial class ZipWriter : AbstractWriter _ => throw new InvalidFormatException("Invalid compression method: " + compressionType), }; - public override void Write(string filename, Stream source, DateTime? modificationTime) => - Write( - filename, - source, - new ZipWriterEntryOptions() { ModificationDateTime = modificationTime } - ); - - public void Write(string entryPath, Stream source, ZipWriterEntryOptions zipWriterEntryOptions) - { - using var output = WriteToStream(entryPath, zipWriterEntryOptions); - var progressStream = WrapWithProgress(source, entryPath); - progressStream.CopyTo(output, WriterOptions.BufferSize); - } - public Stream WriteToStream(string entryPath, ZipWriterEntryOptions options) { options.ValidateWithFallback(compressionType, compressionLevel); @@ -162,59 +148,6 @@ public partial class ZipWriter : AbstractWriter return directoryName; } - public override void WriteDirectory(string directoryName, DateTime? modificationTime) - { - var normalizedName = NormalizeDirectoryName(directoryName); - if (string.IsNullOrEmpty(normalizedName)) - { - return; // Skip empty or root directory - } - - var options = new ZipWriterEntryOptions { ModificationDateTime = modificationTime }; - WriteDirectoryEntry(normalizedName, options); - } - - // WriteDirectoryAsync moved to ZipWriter.Async.cs - - private void WriteDirectoryEntry(string directoryPath, ZipWriterEntryOptions options) - { - var compression = ZipCompressionMethod.None; - - options.ModificationDateTime ??= DateTime.Now; - options.EntryComment ??= string.Empty; - - var entry = new ZipCentralDirectoryEntry( - compression, - directoryPath, - (ulong)streamPosition, - WriterOptions.ArchiveEncoding - ) - { - Comment = options.EntryComment, - ModificationTime = options.ModificationDateTime, - Crc = 0, - Compressed = 0, - Decompressed = 0, - }; - - // Use the archive default setting for zip64 and allow overrides - var useZip64 = isZip64; - if (options.EnableZip64.HasValue) - { - useZip64 = options.EnableZip64.Value; - } - - var headersize = (uint)WriteHeader( - directoryPath, - options, - entry, - useZip64, - usesDataDescriptor: false - ); - streamPosition += headersize; - entries.Add(entry); - } - private int WriteHeader( string filename, ZipWriterEntryOptions zipWriterEntryOptions, diff --git a/src/SharpCompress/Writers/Zip/ZipWritingStream.cs b/src/SharpCompress/Writers/Zip/ZipWritingStream.cs index ad7c7473..e84b5155 100644 --- a/src/SharpCompress/Writers/Zip/ZipWritingStream.cs +++ b/src/SharpCompress/Writers/Zip/ZipWritingStream.cs @@ -15,7 +15,7 @@ namespace SharpCompress.Writers.Zip; public partial class ZipWriter { - internal class ZipWritingStream : Stream + internal partial class ZipWritingStream : Stream { private readonly CRC32 crc = new(); private readonly ZipCentralDirectoryEntry entry; @@ -98,114 +98,7 @@ public partial class ZipWriter set => throw new NotSupportedException(); } - private Stream GetWriteStream(Stream writeStream) - { - counting = new CountingStream(SharpCompressStream.CreateNonDisposing(writeStream)); - Stream output = counting; - - var providers = writer.WriterOptions.Providers; - - switch (zipCompressionMethod) - { - case ZipCompressionMethod.None: - { - return output; - } - case ZipCompressionMethod.Deflate: - { - return providers.CreateCompressStream( - CompressionType.Deflate, - counting, - compressionLevel - ); - } - case ZipCompressionMethod.BZip2: - { - return providers.CreateCompressStream( - CompressionType.BZip2, - counting, - compressionLevel - ); - } - case ZipCompressionMethod.LZMA: - { - var compressingProvider = providers.GetCompressingProvider( - CompressionType.LZMA - ); - if (compressingProvider is null) - { - throw new ArchiveOperationException("LZMA compression provider not found."); - } - - var context = new CompressionContext { CanSeek = originalStream.CanSeek }; - compressionProviderHooks = compressingProvider; - compressionContext = context; - - var preData = compressingProvider.GetPreCompressionData(context); - if (preData is not null) - { - counting.Write(preData, 0, preData.Length); - } - - var lzmaStream = compressingProvider.CreateCompressStream( - counting, - compressionLevel, - context - ); - - var props = compressingProvider.GetCompressionProperties(lzmaStream, context); - if (props is not null) - { - counting.Write(props, 0, props.Length); - } - - return lzmaStream; - } - case ZipCompressionMethod.PPMd: - { - var compressingProvider = providers.GetCompressingProvider( - CompressionType.PPMd - ); - if (compressingProvider is null) - { - throw new ArchiveOperationException("PPMd compression provider not found."); - } - - var context = new CompressionContext - { - CanSeek = originalStream.CanSeek, - FormatOptions = writer.PpmdProperties, - }; - compressionProviderHooks = compressingProvider; - compressionContext = context; - - var preData = compressingProvider.GetPreCompressionData(context); - if (preData is not null) - { - counting.Write(preData, 0, preData.Length); - } - - return compressingProvider.CreateCompressStream( - counting, - compressionLevel, - context - ); - } - case ZipCompressionMethod.ZStandard: - { - return providers.CreateCompressStream( - CompressionType.ZStandard, - counting, - compressionLevel - ); - } - default: - { - throw new NotSupportedException("CompressionMethod: " + zipCompressionMethod); - } - } - } - + [Zomp.SyncMethodGenerator.CreateSyncVersion] private async ValueTask GetWriteStreamAsync( Stream writeStream, CancellationToken cancellationToken @@ -459,17 +352,7 @@ public partial class ZipWriter public override void SetLength(long value) => throw new NotSupportedException(); - public override void Write(byte[] buffer, int offset, int count) - { - CheckWriteLimits(count); - - decompressed += (uint)count; - crc.SlurpBlock(buffer, offset, count); - writeStream.Write(buffer, offset, count); - - CheckPostWriteLimits(); - } - + [Zomp.SyncMethodGenerator.CreateSyncVersion] public override async Task WriteAsync( byte[] buffer, int offset, @@ -477,7 +360,9 @@ public partial class ZipWriter CancellationToken cancellationToken ) { +#if !SYNC_ONLY cancellationToken.ThrowIfCancellationRequested(); +#endif CheckWriteLimits(count); decompressed += (uint)count; @@ -556,30 +441,7 @@ public partial class ZipWriter } } - private void WritePostCompressionData() - { - if ( - compressionProviderHooks is null - || compressionContext is null - || counting is null - || zipCompressionMethod == ZipCompressionMethod.None - ) - { - return; - } - - var postData = compressionProviderHooks.GetPostCompressionData( - writeStream, - compressionContext - ); - if (postData is null || postData.Length == 0) - { - return; - } - - counting.Write(postData, 0, postData.Length); - } - + [Zomp.SyncMethodGenerator.CreateSyncVersion] private async ValueTask WritePostCompressionDataAsync(CancellationToken cancellationToken) { if ( diff --git a/src/SharpCompress/packages.lock.json b/src/SharpCompress/packages.lock.json index 78c97f48..99915dcb 100644 --- a/src/SharpCompress/packages.lock.json +++ b/src/SharpCompress/packages.lock.json @@ -538,4 +538,4 @@ } } } -} \ No newline at end of file +} diff --git a/tests/SharpCompress.Test/OptionsUsabilityTests.cs b/tests/SharpCompress.Test/OptionsUsabilityTests.cs index c8b6ba86..0226aed8 100644 --- a/tests/SharpCompress.Test/OptionsUsabilityTests.cs +++ b/tests/SharpCompress.Test/OptionsUsabilityTests.cs @@ -235,6 +235,21 @@ public class OptionsUsabilityTests : TestBase Assert.Equal(23, source.CopyBufferSize); } + [Fact] + public async Task ZipWriter_Uses_WriterOptions_BufferSize_Async() + { + await using var source = new TrackingReadStream(new byte[100]); + await using var destination = new MemoryStream(); + await using var writer = new ZipWriter( + destination, + new ZipWriterOptions(CompressionType.None) { BufferSize = 29 } + ); + + await writer.WriteAsync("buffer-size.txt", source, DateTime.Now); + + Assert.Equal(29, source.CopyBufferSize); + } + [Fact] public void TarWriter_Uses_WriterOptions_BufferSize_ForTransfer() { From c6981248963c495134a400c71882250c48e3907e Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Mon, 3 Aug 2026 15:49:01 +0100 Subject: [PATCH 17/40] Fixes for parallel lzma usage --- .../Common/SevenZip/ArchiveDatabase.cs | 49 +++- .../Common/SevenZip/ArchiveReader.Async.cs | 6 +- .../Compressors/LZMA/Lzma2ParallelDecoder.cs | 220 +++++++++++++++--- .../Compressors/LZMA/LzmaStream.Async.cs | 10 + .../Compressors/LZMA/LzmaStream.cs | 31 +++ .../SevenZip/SevenZipArchiveTests.cs | 100 +++++--- .../Streams/LzmaStreamAsyncTests.cs | 51 ++++ .../Streams/LzmaStreamTests.cs | 169 +++++++++++++- 8 files changed, 556 insertions(+), 80 deletions(-) diff --git a/src/SharpCompress/Common/SevenZip/ArchiveDatabase.cs b/src/SharpCompress/Common/SevenZip/ArchiveDatabase.cs index f616a77f..612a0b15 100644 --- a/src/SharpCompress/Common/SevenZip/ArchiveDatabase.cs +++ b/src/SharpCompress/Common/SevenZip/ArchiveDatabase.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.IO; +using System.Linq; using SharpCompress.Compressors.LZMA; using SharpCompress.Compressors.LZMA.Utilities; using SharpCompress.IO; @@ -229,12 +231,11 @@ internal partial class ArchiveDatabase return false; } - // Parallel decoding needs positional (random-access) reads from the source file and - // positional writes to the temp output file across multiple threads; a real, seekable - // file handle is required for that. Streams the archive was opened from may be wrapped - // (buffering, source-stream indirection, etc.), so unwrap looking for the underlying file. - var inputFile = stream as FileStream ?? (stream as IStreamStack)?.GetStream(); - if (inputFile is null) + // A file handle is not sufficient by itself: packStart is an offset in this stream's + // logical byte sequence. Only use positional I/O when that sequence is exactly one file. + // In particular, SourceStream concatenates multipart archives, where a logical offset + // cannot be passed directly to a handle for just one physical part. + if (!TryGetPositionallyEquivalentInputFile(stream, out var inputFile)) { return false; } @@ -261,6 +262,7 @@ internal partial class ArchiveDatabase try { + tempFile.SetLength(unpackSize); Lzma2ParallelDecoder.DecodeBlocksParallel( inputFile.SafeFileHandle, props, @@ -280,6 +282,41 @@ internal partial class ArchiveDatabase decodedStream = tempFile; return true; } + + private static bool TryGetPositionallyEquivalentInputFile( + Stream stream, + [NotNullWhen(true)] out FileStream? inputFile + ) + { + inputFile = null; + + if (stream is FileStream fileStream) + { + inputFile = fileStream; + return true; + } + + // Volume wraps caller-owned streams in this non-disposing passthrough wrapper. It does + // not change positions, so its single SourceStream input can still be used safely. + if (stream is SharpCompressStream { IsPassthrough: true } passthrough) + { + stream = passthrough.BaseStream(); + } + + if (stream is not SourceStream sourceStream) + { + return false; + } + + var streams = sourceStream.Streams.Take(2).ToArray(); + if (streams.Length != 1 || streams[0] is not FileStream sourceFile) + { + return false; + } + + inputFile = sourceFile; + return true; + } #endif // Cache used to avoid re-decoding a solid folder from scratch for every file it contains. diff --git a/src/SharpCompress/Common/SevenZip/ArchiveReader.Async.cs b/src/SharpCompress/Common/SevenZip/ArchiveReader.Async.cs index 52964077..45420797 100644 --- a/src/SharpCompress/Common/SevenZip/ArchiveReader.Async.cs +++ b/src/SharpCompress/Common/SevenZip/ArchiveReader.Async.cs @@ -211,7 +211,11 @@ internal sealed partial class ArchiveReader await outStream .ReadExactAsync(data, 0, data.Length, cancellationToken) .ConfigureAwait(false); - if (outStream.ReadByte() >= 0) + if ( + await outStream + .ReadAsync(new byte[1], 0, 1, cancellationToken) + .ConfigureAwait(false) > 0 + ) { throw new InvalidFormatException("Decoded stream is longer than expected."); } diff --git a/src/SharpCompress/Compressors/LZMA/Lzma2ParallelDecoder.cs b/src/SharpCompress/Compressors/LZMA/Lzma2ParallelDecoder.cs index 0539c195..cb63751b 100644 --- a/src/SharpCompress/Compressors/LZMA/Lzma2ParallelDecoder.cs +++ b/src/SharpCompress/Compressors/LZMA/Lzma2ParallelDecoder.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; @@ -42,6 +43,10 @@ internal static class Lzma2ParallelDecoder // 7-Zip MtDec.h: #define MTDEC_THREADS_MAX 32 internal const int MaxThreads = 32; + // Bound each worker's transient buffers independently of the compressed block size. + internal const int BlockInputBufferSize = 1 << 16; + internal const int BlockOutputBufferSize = 1 << 20; + /// /// Header-only walk of the LZMA2 chunk stream (no LZMA decoding) that locates every /// independent restart point and merges adjacent segments into blocks using the same size @@ -185,16 +190,6 @@ internal static class Lzma2ParallelDecoder ); } - // Guards against a single unmerged segment large enough to overflow a byte[] buffer - // (only possible with an extreme, effectively-pathological run with zero restarts). - foreach (var block in blocks) - { - if (block.PackLen > int.MaxValue - 16 || block.UnpackLen > int.MaxValue - 16) - { - return null; - } - } - return blocks; } catch (Exception) @@ -232,61 +227,210 @@ internal static class Lzma2ParallelDecoder IReadOnlyList blocks, SafeFileHandle outputHandle, int maxDegreeOfParallelism + ) => + DecodeBlocksParallel( + inputHandle, + lzma2Props, + packStart, + blocks, + outputHandle, + maxDegreeOfParallelism, + ArrayPool.Shared + ); + + internal static void DecodeBlocksParallel( + SafeFileHandle inputHandle, + byte[] lzma2Props, + long packStart, + IReadOnlyList blocks, + SafeFileHandle outputHandle, + int maxDegreeOfParallelism, + ArrayPool bufferPool ) { + ThrowHelper.ThrowIfNull(bufferPool); + Parallel.ForEach( blocks, new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism }, block => { - var packBuffer = new byte[block.PackLen]; - ReadFullyAt(inputHandle, packBuffer, packStart + block.PackOffset); - - using var packStream = new MemoryStream(packBuffer, writable: false); + using var packStream = new RandomAccessBlockStream( + inputHandle, + checked(packStart + block.PackOffset), + block.PackLen, + bufferPool + ); using var lzma = LzmaStream.Create( lzma2Props, packStream, - -1, + block.PackLen, block.UnpackLen, leaveOpen: true ); - var buffer = new byte[Math.Min(block.UnpackLen, 1 << 20)]; - long total = 0; - while (total < block.UnpackLen) + var bufferSize = checked( + (int)Math.Min(Math.Max(block.UnpackLen, 1), BlockOutputBufferSize) + ); + var buffer = bufferPool.Rent(bufferSize); + try { - var toRead = (int)Math.Min(buffer.Length, block.UnpackLen - total); - var read = lzma.Read(buffer, 0, toRead); - if (read <= 0) + long total = 0; + while (total < block.UnpackLen) { - throw new EndOfStreamException( - $"LZMA2 block at unpack offset {block.UnpackOffset:N0} ended early after {total:N0}/{block.UnpackLen:N0} bytes." + var toRead = (int)Math.Min(bufferSize, block.UnpackLen - total); + var read = lzma.Read(buffer, 0, toRead); + if (read <= 0) + { + throw new EndOfStreamException( + $"LZMA2 block at unpack offset {block.UnpackOffset:N0} ended early after {total:N0}/{block.UnpackLen:N0} bytes." + ); + } + RandomAccess.Write( + outputHandle, + buffer.AsSpan(0, read), + checked(block.UnpackOffset + total) ); + total += read; } - RandomAccess.Write( - outputHandle, - buffer.AsSpan(0, read), - block.UnpackOffset + total - ); - total += read; + } + finally + { + bufferPool.Return(buffer, clearArray: true); } } ); } - private static void ReadFullyAt(SafeFileHandle handle, byte[] buffer, long fileOffset) + private sealed class RandomAccessBlockStream : Stream { - var total = 0; - while (total < buffer.Length) + private readonly SafeFileHandle _inputHandle; + private readonly long _start; + private readonly long _length; + private readonly ArrayPool _bufferPool; + private byte[]? _buffer; + private long _bufferStart = -1; + private int _bufferLength; + private long _position; + + internal RandomAccessBlockStream( + SafeFileHandle inputHandle, + long start, + long length, + ArrayPool bufferPool + ) { - var read = RandomAccess.Read(handle, buffer.AsSpan(total), fileOffset + total); - if (read <= 0) + ThrowHelper.ThrowIfNegative(length); + + _inputHandle = inputHandle; + _start = start; + _length = length; + _bufferPool = bufferPool; + _buffer = bufferPool.Rent(BlockInputBufferSize); + } + + public override bool CanRead => true; + public override bool CanSeek => false; + public override bool CanWrite => false; + public override long Length => _length; + + public override long Position + { + get => _position; + set => throw new NotSupportedException(); + } + + public override void Flush() { } + + public override int Read(byte[] buffer, int offset, int count) => + Read(buffer.AsSpan(offset, count)); + + public override int Read(Span buffer) + { + var total = 0; + while (!buffer.IsEmpty && _position < _length) { - throw new EndOfStreamException( - "Unexpected end of pack stream while reading an LZMA2 block." - ); + if (!FillBuffer()) + { + break; + } + var bufferOffset = checked((int)(_position - _bufferStart)); + var count = Math.Min(_bufferLength - bufferOffset, buffer.Length); + _buffer!.AsSpan(bufferOffset, count).CopyTo(buffer); + _position += count; + total += count; + buffer = buffer.Slice(count); } - total += read; + return total; + } + + public override int ReadByte() + { + if (_position >= _length) + { + return -1; + } + + if (!FillBuffer()) + { + return -1; + } + var value = _buffer![checked((int)(_position - _bufferStart))]; + _position++; + return value; + } + + public override long Seek(long offset, SeekOrigin origin) => + throw new NotSupportedException(); + + public override void SetLength(long value) => throw new NotSupportedException(); + + public override void Write(byte[] buffer, int offset, int count) => + throw new NotSupportedException(); + + protected override void Dispose(bool disposing) + { + if (disposing && _buffer is not null) + { + _bufferPool.Return(_buffer, clearArray: true); + _buffer = null; + } + + base.Dispose(disposing); + } + + private bool FillBuffer() + { + if ( + _bufferStart >= 0 + && _position >= _bufferStart + && _position - _bufferStart < _bufferLength + ) + { + return true; + } + + var buffer = + _buffer ?? throw new ObjectDisposedException(nameof(RandomAccessBlockStream)); + var requested = (int)Math.Min(buffer.Length, _length - _position); + var total = 0; + while (total < requested) + { + var read = RandomAccess.Read( + _inputHandle, + buffer.AsSpan(total, requested - total), + checked(_start + _position + total) + ); + if (read <= 0) + { + break; + } + total += read; + } + + _bufferStart = _position; + _bufferLength = total; + return total > 0; } } #endif diff --git a/src/SharpCompress/Compressors/LZMA/LzmaStream.Async.cs b/src/SharpCompress/Compressors/LZMA/LzmaStream.Async.cs index c89fb923..1c314705 100644 --- a/src/SharpCompress/Compressors/LZMA/LzmaStream.Async.cs +++ b/src/SharpCompress/Compressors/LZMA/LzmaStream.Async.cs @@ -175,6 +175,11 @@ public partial class LzmaStream CancellationToken cancellationToken ) { + if (count > 0) + { + EnsureReadMode(ReadMode.Asynchronous); + } + if (_endReached) { return 0; @@ -289,6 +294,11 @@ public partial class LzmaStream CancellationToken cancellationToken = default ) { + if (!buffer.IsEmpty) + { + EnsureReadMode(ReadMode.Asynchronous); + } + if (_endReached) { return 0; diff --git a/src/SharpCompress/Compressors/LZMA/LzmaStream.cs b/src/SharpCompress/Compressors/LZMA/LzmaStream.cs index 7f296a15..832c0633 100644 --- a/src/SharpCompress/Compressors/LZMA/LzmaStream.cs +++ b/src/SharpCompress/Compressors/LZMA/LzmaStream.cs @@ -36,6 +36,14 @@ public partial class LzmaStream : Stream, IStreamStack, IAsyncDisposable private readonly Encoder? _encoder; private byte[]? _asyncHeaderBuffer; private bool _isDisposed; + private ReadMode _readMode; + + private enum ReadMode + { + None, + Synchronous, + Asynchronous, + } private LzmaStream( byte[] properties, @@ -249,6 +257,11 @@ public partial class LzmaStream : Stream, IStreamStack, IAsyncDisposable public override int Read(byte[] buffer, int offset, int count) { + if (count > 0) + { + EnsureReadMode(ReadMode.Synchronous); + } + if (_endReached) { return 0; @@ -344,6 +357,8 @@ public partial class LzmaStream : Stream, IStreamStack, IAsyncDisposable public override int ReadByte() { + EnsureReadMode(ReadMode.Synchronous); + if (_endReached) { return -1; @@ -423,6 +438,22 @@ public partial class LzmaStream : Stream, IStreamStack, IAsyncDisposable return value; } + private void EnsureReadMode(ReadMode readMode) + { + if (_readMode == ReadMode.None) + { + _readMode = readMode; + return; + } + + if (_readMode != readMode) + { + throw new InvalidOperationException( + "A LzmaStream cannot mix synchronous and asynchronous reads." + ); + } + } + private void DecodeChunkHeader() { var control = _inputStream!.ReadByte(); diff --git a/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs b/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs index d1a07532..dc3b1c93 100644 --- a/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs +++ b/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs @@ -452,39 +452,13 @@ public class SevenZipArchiveTests : ArchiveTests [Fact] public void SevenZipArchive_EnableParallelism_OptInEngagesParallelDecodePath() { - // Highly repetitive so it compresses well, and large enough that -- combined with the - // small dictionary below -- the encoder is forced to periodically reset the dictionary, - // producing multiple independently-decodable LZMA2 restart blocks (the only shape - // Lzma2ParallelDecoder can split across threads). - var payload = new byte[20_000_000]; - for (var i = 0; i < payload.Length; i++) - { - payload[i] = (byte)(i % 7); - } - - // A real (file-backed, seekable) archive is required: the parallel decode path only - // engages for a genuine FileStream, never for archives opened from in-memory streams. var archivePath = Path.Combine(SCRATCH_FILES_PATH, "parallel-decode-optin.7z"); - var writerOptions = new SevenZipWriterOptions(CompressionType.LZMA2) - { - LzmaProperties = new LzmaEncoderProperties( - eos: false, - dictionary: 1 << 20, - numFastBytes: 32 - ), - }; - using (var archiveStream = File.Create(archivePath)) - using (var writer = new SevenZipWriter(archiveStream, writerOptions)) - using (var source = new MemoryStream(payload)) - { - writer.Write("payload.bin", source, DateTime.UtcNow); - } + var payload = CreateParallelDecodeTestArchive(archivePath); Assert.Equal( payload, ReadSinglePayloadEntry( - archivePath, - ReaderOptions.ForFilePath, + () => SevenZipArchive.OpenArchive(archivePath, ReaderOptions.ForFilePath), out var sequentialFolderStreamType ) ); @@ -493,22 +467,80 @@ public class SevenZipArchiveTests : ArchiveTests Assert.Equal( payload, ReadSinglePayloadEntry( - archivePath, - ReaderOptions.ForFilePath.WithEnableParallelism(true), + () => + SevenZipArchive.OpenArchive( + archivePath, + ReaderOptions.ForFilePath.WithEnableParallelism(true) + ), out var parallelFolderStreamType ) ); Assert.Equal(typeof(FileStream), parallelFolderStreamType); } + [Fact] + public void SevenZipArchive_EnableParallelism_MultipartLzma2FallsBackToSequentialDecode() + { + var archivePath = Path.Combine(SCRATCH_FILES_PATH, "parallel-decode-multipart.7z"); + var payload = CreateParallelDecodeTestArchive(archivePath); + var archiveBytes = File.ReadAllBytes(archivePath); + const int splitOffset = 64; + Assert.True(archiveBytes.Length > splitOffset); + + var firstPartPath = Path.Combine(SCRATCH_FILES_PATH, "parallel-decode-multipart.001"); + var secondPartPath = Path.Combine(SCRATCH_FILES_PATH, "parallel-decode-multipart.002"); + File.WriteAllBytes(firstPartPath, archiveBytes[..splitOffset]); + File.WriteAllBytes(secondPartPath, archiveBytes[splitOffset..]); + var parts = new[] { new FileInfo(firstPartPath), new FileInfo(secondPartPath) }; + + Assert.Equal( + payload, + ReadSinglePayloadEntry( + () => + SevenZipArchive.OpenArchive( + parts, + ReaderOptions.ForFilePath.WithEnableParallelism(true) + ), + out var folderStreamType + ) + ); + Assert.NotEqual(typeof(FileStream), folderStreamType); + } + + private static byte[] CreateParallelDecodeTestArchive(string archivePath) + { + // Highly repetitive so it compresses well, and large enough that -- combined with the + // small dictionary below -- the encoder is forced to periodically reset the dictionary, + // producing multiple independently-decodable LZMA2 restart blocks. + var payload = new byte[20_000_000]; + for (var i = 0; i < payload.Length; i++) + { + payload[i] = (byte)(i % 7); + } + + using var archiveStream = File.Create(archivePath); + using var writer = new SevenZipWriter( + archiveStream, + new SevenZipWriterOptions(CompressionType.LZMA2) + { + LzmaProperties = new LzmaEncoderProperties( + eos: false, + dictionary: 1 << 20, + numFastBytes: 32 + ), + } + ); + using var source = new MemoryStream(payload); + writer.Write("payload.bin", source, DateTime.UtcNow); + return payload; + } + private static byte[] ReadSinglePayloadEntry( - string archivePath, - ReaderOptions readerOptions, + Func openArchive, out Type folderStreamType ) { - using var archive = (SevenZipArchive) - SevenZipArchive.OpenArchive(archivePath, readerOptions); + using var archive = Assert.IsType(openArchive()); using var reader = archive.ExtractAllEntries(); var sevenZipReader = Assert.IsType(reader); sevenZipReader.DiagnosticsEnabled = true; diff --git a/tests/SharpCompress.Test/Streams/LzmaStreamAsyncTests.cs b/tests/SharpCompress.Test/Streams/LzmaStreamAsyncTests.cs index cc41e831..8a467f73 100644 --- a/tests/SharpCompress.Test/Streams/LzmaStreamAsyncTests.cs +++ b/tests/SharpCompress.Test/Streams/LzmaStreamAsyncTests.cs @@ -37,6 +37,47 @@ public class LzmaStreamAsyncTests : TestBase Assert.Equal((byte)'X', buffer[0]); } + [Fact] + public async Task LzmaStream_SynchronousReadThenAsynchronousRead_Throws() + { + using var stream = CreateRawLzmaStream(); + var buffer = new byte[1]; + + Assert.Equal(1, stream.Read(buffer, 0, buffer.Length)); + + await Assert.ThrowsAsync(() => + stream.ReadAsync(buffer, 0, buffer.Length) + ); + } + +#if !LEGACY_DOTNET + [Fact] + public async Task LzmaStream_AsynchronousMemoryReadThenSynchronousRead_Throws() + { + using var stream = CreateRawLzmaStream(); + var buffer = new byte[1]; + + Assert.Equal(1, await stream.ReadAsync(buffer.AsMemory())); + + Assert.Throws(() => stream.Read(buffer, 0, buffer.Length)); + } +#endif + + [Fact] + public async Task LzmaStream_ZeroLengthReadsDoNotSelectReadMode() + { + using var stream = CreateRawLzmaStream(); + var empty = Array.Empty(); + + Assert.Equal(0, stream.Read(empty, 0, 0)); + Assert.Equal(0, await stream.ReadAsync(empty, 0, 0)); + Assert.Equal(1, stream.ReadByte()); + + await Assert.ThrowsAsync(() => + stream.ReadAsync(new byte[1], 0, 1) + ); + } + private static byte[] LzmaData { get; } = [ 0x5D, @@ -625,4 +666,14 @@ public class LzmaStreamAsyncTests : TestBase } } } + + private static LzmaStream CreateRawLzmaStream() + { + var input = new MemoryStream(LzmaData); + var properties = new byte[5]; + Buffer.BlockCopy(LzmaData, 0, properties, 0, properties.Length); + var outputSize = BitConverter.ToInt64(LzmaData, 5); + input.Position = 13; + return LzmaStream.Create(properties, input, input.Length - input.Position, outputSize); + } } diff --git a/tests/SharpCompress.Test/Streams/LzmaStreamTests.cs b/tests/SharpCompress.Test/Streams/LzmaStreamTests.cs index 5a0e77cf..887aaa94 100644 --- a/tests/SharpCompress.Test/Streams/LzmaStreamTests.cs +++ b/tests/SharpCompress.Test/Streams/LzmaStreamTests.cs @@ -866,6 +866,7 @@ public class LzmaStreamTests var inputPath = Path.GetTempFileName(); var outputPath = Path.GetTempFileName(); + var bufferPool = new TrackingArrayPool(); try { File.WriteAllBytes(inputPath, packBytes); @@ -894,12 +895,20 @@ public class LzmaStreamTests 0, blocks, outputFile.SafeFileHandle, - Environment.ProcessorCount + Environment.ProcessorCount, + bufferPool ); } var actual = File.ReadAllBytes(outputPath); Assert.Equal(expected, actual); + Assert.All( + bufferPool.RentRequests, + request => Assert.InRange(request, 1, Lzma2ParallelDecoder.BlockOutputBufferSize) + ); + Assert.Equal(bufferPool.RentRequests.Count, bufferPool.ReturnedLengths.Count); + Assert.Equal(0, bufferPool.OutstandingRentals); + Assert.All(bufferPool.ClearArrayRequests, Assert.True); } finally { @@ -907,5 +916,163 @@ public class LzmaStreamTests File.Delete(outputPath); } } + + [Fact] + public void Lzma2ParallelDecoder_DecodeBlocksParallel_ReturnsPooledBuffersOnFailure() + { + var packedData = new byte[] { 0x01, 0x00, 0x3F, 0xAA, 0xBB }; + var block = new Lzma2Block(0, packedData.Length, 0, 64); + var inputPath = Path.GetTempFileName(); + var outputPath = Path.GetTempFileName(); + var bufferPool = new TrackingArrayPool(); + + try + { + File.WriteAllBytes(inputPath, packedData); + + using ( + var inputFile = new FileStream( + inputPath, + FileMode.Open, + FileAccess.Read, + FileShare.Read + ) + ) + using ( + var outputFile = new FileStream( + outputPath, + FileMode.Open, + FileAccess.ReadWrite, + FileShare.None + ) + ) + { + outputFile.SetLength(block.UnpackLen); + Assert.ThrowsAny(() => + Lzma2ParallelDecoder.DecodeBlocksParallel( + inputFile.SafeFileHandle, + Lzma2Props, + 0, + [block], + outputFile.SafeFileHandle, + 1, + bufferPool + ) + ); + } + + Assert.Equal(bufferPool.RentRequests.Count, bufferPool.ReturnedLengths.Count); + Assert.Equal(0, bufferPool.OutstandingRentals); + Assert.All(bufferPool.ClearArrayRequests, Assert.True); + } + finally + { + File.Delete(inputPath); + File.Delete(outputPath); + } + } + + [Fact] + public void Lzma2ParallelDecoder_DecodeBlocksParallel_SupportsLongPackLengths() + { + var expected = RepeatingPayload(64, 7); + var packedData = UncompressedChunk(dictReset: true, expected); + var block = new Lzma2Block(0, (long)int.MaxValue + 1, 0, expected.Length); + var inputPath = Path.GetTempFileName(); + var outputPath = Path.GetTempFileName(); + var bufferPool = new TrackingArrayPool(); + + try + { + File.WriteAllBytes(inputPath, packedData); + + using ( + var inputFile = new FileStream( + inputPath, + FileMode.Open, + FileAccess.Read, + FileShare.Read + ) + ) + using ( + var outputFile = new FileStream( + outputPath, + FileMode.Open, + FileAccess.ReadWrite, + FileShare.None + ) + ) + { + outputFile.SetLength(block.UnpackLen); + Lzma2ParallelDecoder.DecodeBlocksParallel( + inputFile.SafeFileHandle, + Lzma2Props, + 0, + [block], + outputFile.SafeFileHandle, + 1, + bufferPool + ); + } + + Assert.Equal(expected, File.ReadAllBytes(outputPath)); + Assert.All( + bufferPool.RentRequests, + request => Assert.InRange(request, 1, Lzma2ParallelDecoder.BlockOutputBufferSize) + ); + } + finally + { + File.Delete(inputPath); + File.Delete(outputPath); + } + } + + private sealed class TrackingArrayPool : ArrayPool + { + private readonly object _lock = new(); + private readonly HashSet _rented = new(); + + public List RentRequests { get; } = new(); + public List ReturnedLengths { get; } = new(); + public List ClearArrayRequests { get; } = new(); + + public int OutstandingRentals + { + get + { + lock (_lock) + { + return _rented.Count; + } + } + } + + public override byte[] Rent(int minimumLength) + { + var buffer = new byte[minimumLength]; + lock (_lock) + { + RentRequests.Add(minimumLength); + _rented.Add(buffer); + } + return buffer; + } + + public override void Return(byte[] array, bool clearArray = false) + { + lock (_lock) + { + Assert.True(_rented.Remove(array), "A buffer must be returned only once."); + ReturnedLengths.Add(array.Length); + ClearArrayRequests.Add(clearArray); + } + + if (clearArray) + { + Array.Clear(array, 0, array.Length); + } + } + } #endif } From 208d263a08f086b8cfc2de489c10a9f29041744a Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Mon, 3 Aug 2026 15:49:01 +0100 Subject: [PATCH 18/40] Fixes for parallel lzma usage --- .../Common/SevenZip/ArchiveDatabase.cs | 49 +++- .../Common/SevenZip/ArchiveReader.Async.cs | 6 +- .../Compressors/LZMA/Lzma2ParallelDecoder.cs | 220 +++++++++++++++--- .../Compressors/LZMA/LzmaStream.Async.cs | 10 + .../Compressors/LZMA/LzmaStream.cs | 31 +++ .../SevenZip/SevenZipArchiveTests.cs | 100 +++++--- .../Streams/LzmaStreamAsyncTests.cs | 51 ++++ .../Streams/LzmaStreamTests.cs | 169 +++++++++++++- 8 files changed, 556 insertions(+), 80 deletions(-) diff --git a/src/SharpCompress/Common/SevenZip/ArchiveDatabase.cs b/src/SharpCompress/Common/SevenZip/ArchiveDatabase.cs index f616a77f..612a0b15 100644 --- a/src/SharpCompress/Common/SevenZip/ArchiveDatabase.cs +++ b/src/SharpCompress/Common/SevenZip/ArchiveDatabase.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.IO; +using System.Linq; using SharpCompress.Compressors.LZMA; using SharpCompress.Compressors.LZMA.Utilities; using SharpCompress.IO; @@ -229,12 +231,11 @@ internal partial class ArchiveDatabase return false; } - // Parallel decoding needs positional (random-access) reads from the source file and - // positional writes to the temp output file across multiple threads; a real, seekable - // file handle is required for that. Streams the archive was opened from may be wrapped - // (buffering, source-stream indirection, etc.), so unwrap looking for the underlying file. - var inputFile = stream as FileStream ?? (stream as IStreamStack)?.GetStream(); - if (inputFile is null) + // A file handle is not sufficient by itself: packStart is an offset in this stream's + // logical byte sequence. Only use positional I/O when that sequence is exactly one file. + // In particular, SourceStream concatenates multipart archives, where a logical offset + // cannot be passed directly to a handle for just one physical part. + if (!TryGetPositionallyEquivalentInputFile(stream, out var inputFile)) { return false; } @@ -261,6 +262,7 @@ internal partial class ArchiveDatabase try { + tempFile.SetLength(unpackSize); Lzma2ParallelDecoder.DecodeBlocksParallel( inputFile.SafeFileHandle, props, @@ -280,6 +282,41 @@ internal partial class ArchiveDatabase decodedStream = tempFile; return true; } + + private static bool TryGetPositionallyEquivalentInputFile( + Stream stream, + [NotNullWhen(true)] out FileStream? inputFile + ) + { + inputFile = null; + + if (stream is FileStream fileStream) + { + inputFile = fileStream; + return true; + } + + // Volume wraps caller-owned streams in this non-disposing passthrough wrapper. It does + // not change positions, so its single SourceStream input can still be used safely. + if (stream is SharpCompressStream { IsPassthrough: true } passthrough) + { + stream = passthrough.BaseStream(); + } + + if (stream is not SourceStream sourceStream) + { + return false; + } + + var streams = sourceStream.Streams.Take(2).ToArray(); + if (streams.Length != 1 || streams[0] is not FileStream sourceFile) + { + return false; + } + + inputFile = sourceFile; + return true; + } #endif // Cache used to avoid re-decoding a solid folder from scratch for every file it contains. diff --git a/src/SharpCompress/Common/SevenZip/ArchiveReader.Async.cs b/src/SharpCompress/Common/SevenZip/ArchiveReader.Async.cs index 52964077..45420797 100644 --- a/src/SharpCompress/Common/SevenZip/ArchiveReader.Async.cs +++ b/src/SharpCompress/Common/SevenZip/ArchiveReader.Async.cs @@ -211,7 +211,11 @@ internal sealed partial class ArchiveReader await outStream .ReadExactAsync(data, 0, data.Length, cancellationToken) .ConfigureAwait(false); - if (outStream.ReadByte() >= 0) + if ( + await outStream + .ReadAsync(new byte[1], 0, 1, cancellationToken) + .ConfigureAwait(false) > 0 + ) { throw new InvalidFormatException("Decoded stream is longer than expected."); } diff --git a/src/SharpCompress/Compressors/LZMA/Lzma2ParallelDecoder.cs b/src/SharpCompress/Compressors/LZMA/Lzma2ParallelDecoder.cs index 0539c195..cb63751b 100644 --- a/src/SharpCompress/Compressors/LZMA/Lzma2ParallelDecoder.cs +++ b/src/SharpCompress/Compressors/LZMA/Lzma2ParallelDecoder.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; @@ -42,6 +43,10 @@ internal static class Lzma2ParallelDecoder // 7-Zip MtDec.h: #define MTDEC_THREADS_MAX 32 internal const int MaxThreads = 32; + // Bound each worker's transient buffers independently of the compressed block size. + internal const int BlockInputBufferSize = 1 << 16; + internal const int BlockOutputBufferSize = 1 << 20; + /// /// Header-only walk of the LZMA2 chunk stream (no LZMA decoding) that locates every /// independent restart point and merges adjacent segments into blocks using the same size @@ -185,16 +190,6 @@ internal static class Lzma2ParallelDecoder ); } - // Guards against a single unmerged segment large enough to overflow a byte[] buffer - // (only possible with an extreme, effectively-pathological run with zero restarts). - foreach (var block in blocks) - { - if (block.PackLen > int.MaxValue - 16 || block.UnpackLen > int.MaxValue - 16) - { - return null; - } - } - return blocks; } catch (Exception) @@ -232,61 +227,210 @@ internal static class Lzma2ParallelDecoder IReadOnlyList blocks, SafeFileHandle outputHandle, int maxDegreeOfParallelism + ) => + DecodeBlocksParallel( + inputHandle, + lzma2Props, + packStart, + blocks, + outputHandle, + maxDegreeOfParallelism, + ArrayPool.Shared + ); + + internal static void DecodeBlocksParallel( + SafeFileHandle inputHandle, + byte[] lzma2Props, + long packStart, + IReadOnlyList blocks, + SafeFileHandle outputHandle, + int maxDegreeOfParallelism, + ArrayPool bufferPool ) { + ThrowHelper.ThrowIfNull(bufferPool); + Parallel.ForEach( blocks, new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism }, block => { - var packBuffer = new byte[block.PackLen]; - ReadFullyAt(inputHandle, packBuffer, packStart + block.PackOffset); - - using var packStream = new MemoryStream(packBuffer, writable: false); + using var packStream = new RandomAccessBlockStream( + inputHandle, + checked(packStart + block.PackOffset), + block.PackLen, + bufferPool + ); using var lzma = LzmaStream.Create( lzma2Props, packStream, - -1, + block.PackLen, block.UnpackLen, leaveOpen: true ); - var buffer = new byte[Math.Min(block.UnpackLen, 1 << 20)]; - long total = 0; - while (total < block.UnpackLen) + var bufferSize = checked( + (int)Math.Min(Math.Max(block.UnpackLen, 1), BlockOutputBufferSize) + ); + var buffer = bufferPool.Rent(bufferSize); + try { - var toRead = (int)Math.Min(buffer.Length, block.UnpackLen - total); - var read = lzma.Read(buffer, 0, toRead); - if (read <= 0) + long total = 0; + while (total < block.UnpackLen) { - throw new EndOfStreamException( - $"LZMA2 block at unpack offset {block.UnpackOffset:N0} ended early after {total:N0}/{block.UnpackLen:N0} bytes." + var toRead = (int)Math.Min(bufferSize, block.UnpackLen - total); + var read = lzma.Read(buffer, 0, toRead); + if (read <= 0) + { + throw new EndOfStreamException( + $"LZMA2 block at unpack offset {block.UnpackOffset:N0} ended early after {total:N0}/{block.UnpackLen:N0} bytes." + ); + } + RandomAccess.Write( + outputHandle, + buffer.AsSpan(0, read), + checked(block.UnpackOffset + total) ); + total += read; } - RandomAccess.Write( - outputHandle, - buffer.AsSpan(0, read), - block.UnpackOffset + total - ); - total += read; + } + finally + { + bufferPool.Return(buffer, clearArray: true); } } ); } - private static void ReadFullyAt(SafeFileHandle handle, byte[] buffer, long fileOffset) + private sealed class RandomAccessBlockStream : Stream { - var total = 0; - while (total < buffer.Length) + private readonly SafeFileHandle _inputHandle; + private readonly long _start; + private readonly long _length; + private readonly ArrayPool _bufferPool; + private byte[]? _buffer; + private long _bufferStart = -1; + private int _bufferLength; + private long _position; + + internal RandomAccessBlockStream( + SafeFileHandle inputHandle, + long start, + long length, + ArrayPool bufferPool + ) { - var read = RandomAccess.Read(handle, buffer.AsSpan(total), fileOffset + total); - if (read <= 0) + ThrowHelper.ThrowIfNegative(length); + + _inputHandle = inputHandle; + _start = start; + _length = length; + _bufferPool = bufferPool; + _buffer = bufferPool.Rent(BlockInputBufferSize); + } + + public override bool CanRead => true; + public override bool CanSeek => false; + public override bool CanWrite => false; + public override long Length => _length; + + public override long Position + { + get => _position; + set => throw new NotSupportedException(); + } + + public override void Flush() { } + + public override int Read(byte[] buffer, int offset, int count) => + Read(buffer.AsSpan(offset, count)); + + public override int Read(Span buffer) + { + var total = 0; + while (!buffer.IsEmpty && _position < _length) { - throw new EndOfStreamException( - "Unexpected end of pack stream while reading an LZMA2 block." - ); + if (!FillBuffer()) + { + break; + } + var bufferOffset = checked((int)(_position - _bufferStart)); + var count = Math.Min(_bufferLength - bufferOffset, buffer.Length); + _buffer!.AsSpan(bufferOffset, count).CopyTo(buffer); + _position += count; + total += count; + buffer = buffer.Slice(count); } - total += read; + return total; + } + + public override int ReadByte() + { + if (_position >= _length) + { + return -1; + } + + if (!FillBuffer()) + { + return -1; + } + var value = _buffer![checked((int)(_position - _bufferStart))]; + _position++; + return value; + } + + public override long Seek(long offset, SeekOrigin origin) => + throw new NotSupportedException(); + + public override void SetLength(long value) => throw new NotSupportedException(); + + public override void Write(byte[] buffer, int offset, int count) => + throw new NotSupportedException(); + + protected override void Dispose(bool disposing) + { + if (disposing && _buffer is not null) + { + _bufferPool.Return(_buffer, clearArray: true); + _buffer = null; + } + + base.Dispose(disposing); + } + + private bool FillBuffer() + { + if ( + _bufferStart >= 0 + && _position >= _bufferStart + && _position - _bufferStart < _bufferLength + ) + { + return true; + } + + var buffer = + _buffer ?? throw new ObjectDisposedException(nameof(RandomAccessBlockStream)); + var requested = (int)Math.Min(buffer.Length, _length - _position); + var total = 0; + while (total < requested) + { + var read = RandomAccess.Read( + _inputHandle, + buffer.AsSpan(total, requested - total), + checked(_start + _position + total) + ); + if (read <= 0) + { + break; + } + total += read; + } + + _bufferStart = _position; + _bufferLength = total; + return total > 0; } } #endif diff --git a/src/SharpCompress/Compressors/LZMA/LzmaStream.Async.cs b/src/SharpCompress/Compressors/LZMA/LzmaStream.Async.cs index c89fb923..1c314705 100644 --- a/src/SharpCompress/Compressors/LZMA/LzmaStream.Async.cs +++ b/src/SharpCompress/Compressors/LZMA/LzmaStream.Async.cs @@ -175,6 +175,11 @@ public partial class LzmaStream CancellationToken cancellationToken ) { + if (count > 0) + { + EnsureReadMode(ReadMode.Asynchronous); + } + if (_endReached) { return 0; @@ -289,6 +294,11 @@ public partial class LzmaStream CancellationToken cancellationToken = default ) { + if (!buffer.IsEmpty) + { + EnsureReadMode(ReadMode.Asynchronous); + } + if (_endReached) { return 0; diff --git a/src/SharpCompress/Compressors/LZMA/LzmaStream.cs b/src/SharpCompress/Compressors/LZMA/LzmaStream.cs index 7f296a15..832c0633 100644 --- a/src/SharpCompress/Compressors/LZMA/LzmaStream.cs +++ b/src/SharpCompress/Compressors/LZMA/LzmaStream.cs @@ -36,6 +36,14 @@ public partial class LzmaStream : Stream, IStreamStack, IAsyncDisposable private readonly Encoder? _encoder; private byte[]? _asyncHeaderBuffer; private bool _isDisposed; + private ReadMode _readMode; + + private enum ReadMode + { + None, + Synchronous, + Asynchronous, + } private LzmaStream( byte[] properties, @@ -249,6 +257,11 @@ public partial class LzmaStream : Stream, IStreamStack, IAsyncDisposable public override int Read(byte[] buffer, int offset, int count) { + if (count > 0) + { + EnsureReadMode(ReadMode.Synchronous); + } + if (_endReached) { return 0; @@ -344,6 +357,8 @@ public partial class LzmaStream : Stream, IStreamStack, IAsyncDisposable public override int ReadByte() { + EnsureReadMode(ReadMode.Synchronous); + if (_endReached) { return -1; @@ -423,6 +438,22 @@ public partial class LzmaStream : Stream, IStreamStack, IAsyncDisposable return value; } + private void EnsureReadMode(ReadMode readMode) + { + if (_readMode == ReadMode.None) + { + _readMode = readMode; + return; + } + + if (_readMode != readMode) + { + throw new InvalidOperationException( + "A LzmaStream cannot mix synchronous and asynchronous reads." + ); + } + } + private void DecodeChunkHeader() { var control = _inputStream!.ReadByte(); diff --git a/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs b/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs index d1a07532..dc3b1c93 100644 --- a/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs +++ b/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs @@ -452,39 +452,13 @@ public class SevenZipArchiveTests : ArchiveTests [Fact] public void SevenZipArchive_EnableParallelism_OptInEngagesParallelDecodePath() { - // Highly repetitive so it compresses well, and large enough that -- combined with the - // small dictionary below -- the encoder is forced to periodically reset the dictionary, - // producing multiple independently-decodable LZMA2 restart blocks (the only shape - // Lzma2ParallelDecoder can split across threads). - var payload = new byte[20_000_000]; - for (var i = 0; i < payload.Length; i++) - { - payload[i] = (byte)(i % 7); - } - - // A real (file-backed, seekable) archive is required: the parallel decode path only - // engages for a genuine FileStream, never for archives opened from in-memory streams. var archivePath = Path.Combine(SCRATCH_FILES_PATH, "parallel-decode-optin.7z"); - var writerOptions = new SevenZipWriterOptions(CompressionType.LZMA2) - { - LzmaProperties = new LzmaEncoderProperties( - eos: false, - dictionary: 1 << 20, - numFastBytes: 32 - ), - }; - using (var archiveStream = File.Create(archivePath)) - using (var writer = new SevenZipWriter(archiveStream, writerOptions)) - using (var source = new MemoryStream(payload)) - { - writer.Write("payload.bin", source, DateTime.UtcNow); - } + var payload = CreateParallelDecodeTestArchive(archivePath); Assert.Equal( payload, ReadSinglePayloadEntry( - archivePath, - ReaderOptions.ForFilePath, + () => SevenZipArchive.OpenArchive(archivePath, ReaderOptions.ForFilePath), out var sequentialFolderStreamType ) ); @@ -493,22 +467,80 @@ public class SevenZipArchiveTests : ArchiveTests Assert.Equal( payload, ReadSinglePayloadEntry( - archivePath, - ReaderOptions.ForFilePath.WithEnableParallelism(true), + () => + SevenZipArchive.OpenArchive( + archivePath, + ReaderOptions.ForFilePath.WithEnableParallelism(true) + ), out var parallelFolderStreamType ) ); Assert.Equal(typeof(FileStream), parallelFolderStreamType); } + [Fact] + public void SevenZipArchive_EnableParallelism_MultipartLzma2FallsBackToSequentialDecode() + { + var archivePath = Path.Combine(SCRATCH_FILES_PATH, "parallel-decode-multipart.7z"); + var payload = CreateParallelDecodeTestArchive(archivePath); + var archiveBytes = File.ReadAllBytes(archivePath); + const int splitOffset = 64; + Assert.True(archiveBytes.Length > splitOffset); + + var firstPartPath = Path.Combine(SCRATCH_FILES_PATH, "parallel-decode-multipart.001"); + var secondPartPath = Path.Combine(SCRATCH_FILES_PATH, "parallel-decode-multipart.002"); + File.WriteAllBytes(firstPartPath, archiveBytes[..splitOffset]); + File.WriteAllBytes(secondPartPath, archiveBytes[splitOffset..]); + var parts = new[] { new FileInfo(firstPartPath), new FileInfo(secondPartPath) }; + + Assert.Equal( + payload, + ReadSinglePayloadEntry( + () => + SevenZipArchive.OpenArchive( + parts, + ReaderOptions.ForFilePath.WithEnableParallelism(true) + ), + out var folderStreamType + ) + ); + Assert.NotEqual(typeof(FileStream), folderStreamType); + } + + private static byte[] CreateParallelDecodeTestArchive(string archivePath) + { + // Highly repetitive so it compresses well, and large enough that -- combined with the + // small dictionary below -- the encoder is forced to periodically reset the dictionary, + // producing multiple independently-decodable LZMA2 restart blocks. + var payload = new byte[20_000_000]; + for (var i = 0; i < payload.Length; i++) + { + payload[i] = (byte)(i % 7); + } + + using var archiveStream = File.Create(archivePath); + using var writer = new SevenZipWriter( + archiveStream, + new SevenZipWriterOptions(CompressionType.LZMA2) + { + LzmaProperties = new LzmaEncoderProperties( + eos: false, + dictionary: 1 << 20, + numFastBytes: 32 + ), + } + ); + using var source = new MemoryStream(payload); + writer.Write("payload.bin", source, DateTime.UtcNow); + return payload; + } + private static byte[] ReadSinglePayloadEntry( - string archivePath, - ReaderOptions readerOptions, + Func openArchive, out Type folderStreamType ) { - using var archive = (SevenZipArchive) - SevenZipArchive.OpenArchive(archivePath, readerOptions); + using var archive = Assert.IsType(openArchive()); using var reader = archive.ExtractAllEntries(); var sevenZipReader = Assert.IsType(reader); sevenZipReader.DiagnosticsEnabled = true; diff --git a/tests/SharpCompress.Test/Streams/LzmaStreamAsyncTests.cs b/tests/SharpCompress.Test/Streams/LzmaStreamAsyncTests.cs index cc41e831..8a467f73 100644 --- a/tests/SharpCompress.Test/Streams/LzmaStreamAsyncTests.cs +++ b/tests/SharpCompress.Test/Streams/LzmaStreamAsyncTests.cs @@ -37,6 +37,47 @@ public class LzmaStreamAsyncTests : TestBase Assert.Equal((byte)'X', buffer[0]); } + [Fact] + public async Task LzmaStream_SynchronousReadThenAsynchronousRead_Throws() + { + using var stream = CreateRawLzmaStream(); + var buffer = new byte[1]; + + Assert.Equal(1, stream.Read(buffer, 0, buffer.Length)); + + await Assert.ThrowsAsync(() => + stream.ReadAsync(buffer, 0, buffer.Length) + ); + } + +#if !LEGACY_DOTNET + [Fact] + public async Task LzmaStream_AsynchronousMemoryReadThenSynchronousRead_Throws() + { + using var stream = CreateRawLzmaStream(); + var buffer = new byte[1]; + + Assert.Equal(1, await stream.ReadAsync(buffer.AsMemory())); + + Assert.Throws(() => stream.Read(buffer, 0, buffer.Length)); + } +#endif + + [Fact] + public async Task LzmaStream_ZeroLengthReadsDoNotSelectReadMode() + { + using var stream = CreateRawLzmaStream(); + var empty = Array.Empty(); + + Assert.Equal(0, stream.Read(empty, 0, 0)); + Assert.Equal(0, await stream.ReadAsync(empty, 0, 0)); + Assert.Equal(1, stream.ReadByte()); + + await Assert.ThrowsAsync(() => + stream.ReadAsync(new byte[1], 0, 1) + ); + } + private static byte[] LzmaData { get; } = [ 0x5D, @@ -625,4 +666,14 @@ public class LzmaStreamAsyncTests : TestBase } } } + + private static LzmaStream CreateRawLzmaStream() + { + var input = new MemoryStream(LzmaData); + var properties = new byte[5]; + Buffer.BlockCopy(LzmaData, 0, properties, 0, properties.Length); + var outputSize = BitConverter.ToInt64(LzmaData, 5); + input.Position = 13; + return LzmaStream.Create(properties, input, input.Length - input.Position, outputSize); + } } diff --git a/tests/SharpCompress.Test/Streams/LzmaStreamTests.cs b/tests/SharpCompress.Test/Streams/LzmaStreamTests.cs index 5a0e77cf..887aaa94 100644 --- a/tests/SharpCompress.Test/Streams/LzmaStreamTests.cs +++ b/tests/SharpCompress.Test/Streams/LzmaStreamTests.cs @@ -866,6 +866,7 @@ public class LzmaStreamTests var inputPath = Path.GetTempFileName(); var outputPath = Path.GetTempFileName(); + var bufferPool = new TrackingArrayPool(); try { File.WriteAllBytes(inputPath, packBytes); @@ -894,12 +895,20 @@ public class LzmaStreamTests 0, blocks, outputFile.SafeFileHandle, - Environment.ProcessorCount + Environment.ProcessorCount, + bufferPool ); } var actual = File.ReadAllBytes(outputPath); Assert.Equal(expected, actual); + Assert.All( + bufferPool.RentRequests, + request => Assert.InRange(request, 1, Lzma2ParallelDecoder.BlockOutputBufferSize) + ); + Assert.Equal(bufferPool.RentRequests.Count, bufferPool.ReturnedLengths.Count); + Assert.Equal(0, bufferPool.OutstandingRentals); + Assert.All(bufferPool.ClearArrayRequests, Assert.True); } finally { @@ -907,5 +916,163 @@ public class LzmaStreamTests File.Delete(outputPath); } } + + [Fact] + public void Lzma2ParallelDecoder_DecodeBlocksParallel_ReturnsPooledBuffersOnFailure() + { + var packedData = new byte[] { 0x01, 0x00, 0x3F, 0xAA, 0xBB }; + var block = new Lzma2Block(0, packedData.Length, 0, 64); + var inputPath = Path.GetTempFileName(); + var outputPath = Path.GetTempFileName(); + var bufferPool = new TrackingArrayPool(); + + try + { + File.WriteAllBytes(inputPath, packedData); + + using ( + var inputFile = new FileStream( + inputPath, + FileMode.Open, + FileAccess.Read, + FileShare.Read + ) + ) + using ( + var outputFile = new FileStream( + outputPath, + FileMode.Open, + FileAccess.ReadWrite, + FileShare.None + ) + ) + { + outputFile.SetLength(block.UnpackLen); + Assert.ThrowsAny(() => + Lzma2ParallelDecoder.DecodeBlocksParallel( + inputFile.SafeFileHandle, + Lzma2Props, + 0, + [block], + outputFile.SafeFileHandle, + 1, + bufferPool + ) + ); + } + + Assert.Equal(bufferPool.RentRequests.Count, bufferPool.ReturnedLengths.Count); + Assert.Equal(0, bufferPool.OutstandingRentals); + Assert.All(bufferPool.ClearArrayRequests, Assert.True); + } + finally + { + File.Delete(inputPath); + File.Delete(outputPath); + } + } + + [Fact] + public void Lzma2ParallelDecoder_DecodeBlocksParallel_SupportsLongPackLengths() + { + var expected = RepeatingPayload(64, 7); + var packedData = UncompressedChunk(dictReset: true, expected); + var block = new Lzma2Block(0, (long)int.MaxValue + 1, 0, expected.Length); + var inputPath = Path.GetTempFileName(); + var outputPath = Path.GetTempFileName(); + var bufferPool = new TrackingArrayPool(); + + try + { + File.WriteAllBytes(inputPath, packedData); + + using ( + var inputFile = new FileStream( + inputPath, + FileMode.Open, + FileAccess.Read, + FileShare.Read + ) + ) + using ( + var outputFile = new FileStream( + outputPath, + FileMode.Open, + FileAccess.ReadWrite, + FileShare.None + ) + ) + { + outputFile.SetLength(block.UnpackLen); + Lzma2ParallelDecoder.DecodeBlocksParallel( + inputFile.SafeFileHandle, + Lzma2Props, + 0, + [block], + outputFile.SafeFileHandle, + 1, + bufferPool + ); + } + + Assert.Equal(expected, File.ReadAllBytes(outputPath)); + Assert.All( + bufferPool.RentRequests, + request => Assert.InRange(request, 1, Lzma2ParallelDecoder.BlockOutputBufferSize) + ); + } + finally + { + File.Delete(inputPath); + File.Delete(outputPath); + } + } + + private sealed class TrackingArrayPool : ArrayPool + { + private readonly object _lock = new(); + private readonly HashSet _rented = new(); + + public List RentRequests { get; } = new(); + public List ReturnedLengths { get; } = new(); + public List ClearArrayRequests { get; } = new(); + + public int OutstandingRentals + { + get + { + lock (_lock) + { + return _rented.Count; + } + } + } + + public override byte[] Rent(int minimumLength) + { + var buffer = new byte[minimumLength]; + lock (_lock) + { + RentRequests.Add(minimumLength); + _rented.Add(buffer); + } + return buffer; + } + + public override void Return(byte[] array, bool clearArray = false) + { + lock (_lock) + { + Assert.True(_rented.Remove(array), "A buffer must be returned only once."); + ReturnedLengths.Add(array.Length); + ClearArrayRequests.Add(clearArray); + } + + if (clearArray) + { + Array.Clear(array, 0, array.Length); + } + } + } #endif } From 369ab91522f81e14efa90ee368687f2015fc1788 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Mon, 3 Aug 2026 16:01:58 +0100 Subject: [PATCH 19/40] fix windows build --- tests/SharpCompress.Test/LargeArchiveTests.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/SharpCompress.Test/LargeArchiveTests.cs b/tests/SharpCompress.Test/LargeArchiveTests.cs index 761abda6..18b427e2 100644 --- a/tests/SharpCompress.Test/LargeArchiveTests.cs +++ b/tests/SharpCompress.Test/LargeArchiveTests.cs @@ -40,13 +40,24 @@ public class LargeArchiveTests : TestBase [InlineData("Large/Large.7z")] public async Task OpenAsyncArchive_ShouldStreamLargeEntry(string fixtureName) { +#if LEGACY_DOTNET + using var stream = new AsyncOnlyStream( + File.OpenRead(await GetMaterializedFixturePathAsync(fixtureName)) + ); +#else await using var stream = new AsyncOnlyStream( File.OpenRead(await GetMaterializedFixturePathAsync(fixtureName)) ); +#endif await using var archive = await ArchiveFactory.OpenAsyncArchive(stream); var entry = await GetSingleEntryAsync(archive); + +#if LEGACY_DOTNET + using var entryStream = await entry.OpenEntryStreamAsync(); +#else await using var entryStream = await entry.OpenEntryStreamAsync(); +#endif await VerifyContentAsync(entry.Key, entryStream); } @@ -72,14 +83,25 @@ public class LargeArchiveTests : TestBase [InlineData("Large/Large.tar.gz")] public async Task OpenAsyncReader_ShouldStreamLargeEntry(string fixtureName) { +#if LEGACY_DOTNET + using var stream = new AsyncOnlyStream( + File.OpenRead(await GetMaterializedFixturePathAsync(fixtureName)) + ); +#else await using var stream = new AsyncOnlyStream( File.OpenRead(await GetMaterializedFixturePathAsync(fixtureName)) ); +#endif await using var reader = await ReaderFactory.OpenAsyncReader(stream); Assert.True(await reader.MoveToNextEntryAsync()); Assert.False(reader.Entry.IsDirectory); + +#if LEGACY_DOTNET + using var entryStream = await reader.OpenEntryStreamAsync(); +#else await using var entryStream = await reader.OpenEntryStreamAsync(); +#endif await VerifyContentAsync(reader.Entry.Key, entryStream); Assert.False(await reader.MoveToNextEntryAsync()); } From 4a71ec898bb06e9536a0c219f722dfa8a6f5b35b Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Mon, 3 Aug 2026 16:38:27 +0100 Subject: [PATCH 20/40] fixes found by gh and sol --- src/SharpCompress/Factories/GZipFactory.cs | 19 ++++++++++--- src/SharpCompress/Factories/LzwFactory.cs | 18 +++++++++--- tests/SharpCompress.Test/LargeArchiveTests.cs | 16 +++++++++++ .../Tar/TarReaderAsyncTests.cs | 28 +++++++++++++++---- 4 files changed, 67 insertions(+), 14 deletions(-) diff --git a/src/SharpCompress/Factories/GZipFactory.cs b/src/SharpCompress/Factories/GZipFactory.cs index 4649f0fc..7dfdd313 100644 --- a/src/SharpCompress/Factories/GZipFactory.cs +++ b/src/SharpCompress/Factories/GZipFactory.cs @@ -185,14 +185,25 @@ public class GZipFactory } sharpCompressStream.Rewind(); - var tarReader = await new TarFactory() - .TryOpenReaderAsync(sharpCompressStream, options, cancellationToken) + using var testStream = SharpCompressStream.CreateNonDisposing( + await options + .Providers.CreateDecompressStreamAsync( + CompressionType.GZip, + SharpCompressStream.CreateNonDisposing(sharpCompressStream), + CompressionContext.FromStream(sharpCompressStream).WithReaderOptions(options), + cancellationToken + ) + .ConfigureAwait(false) + ); + var isTarArchive = await TarArchive + .IsTarFileAsync(testStream, cancellationToken) .ConfigureAwait(false); + sharpCompressStream.Rewind(); sharpCompressStream.StopRecording(); - if (tarReader is not null) + if (isTarArchive) { - return tarReader; + return new TarReader(sharpCompressStream, options, CompressionType.GZip); } return await OpenAsyncReader(sharpCompressStream, options, cancellationToken) diff --git a/src/SharpCompress/Factories/LzwFactory.cs b/src/SharpCompress/Factories/LzwFactory.cs index da475d8a..098ec002 100644 --- a/src/SharpCompress/Factories/LzwFactory.cs +++ b/src/SharpCompress/Factories/LzwFactory.cs @@ -100,14 +100,24 @@ public class LzwFactory : Factory, IReaderFactory } sharpCompressStream.Rewind(); - var tarReader = await new TarFactory() - .TryOpenReaderAsync(sharpCompressStream, options, cancellationToken) + using var testStream = SharpCompressStream.CreateNonDisposing( + await options + .Providers.CreateDecompressStreamAsync( + CompressionType.Lzw, + SharpCompressStream.CreateNonDisposing(sharpCompressStream), + cancellationToken + ) + .ConfigureAwait(false) + ); + var isTarArchive = await TarArchive + .IsTarFileAsync(testStream, cancellationToken) .ConfigureAwait(false); + sharpCompressStream.Rewind(); sharpCompressStream.StopRecording(); - if (tarReader is not null) + if (isTarArchive) { - return tarReader; + return new TarReader(sharpCompressStream, options, CompressionType.Lzw); } return await OpenAsyncReader(sharpCompressStream, options, cancellationToken) diff --git a/tests/SharpCompress.Test/LargeArchiveTests.cs b/tests/SharpCompress.Test/LargeArchiveTests.cs index 761abda6..09090f6a 100644 --- a/tests/SharpCompress.Test/LargeArchiveTests.cs +++ b/tests/SharpCompress.Test/LargeArchiveTests.cs @@ -84,6 +84,22 @@ public class LargeArchiveTests : TestBase Assert.False(await reader.MoveToNextEntryAsync()); } + [Fact] + public async Task OpenAsyncReader_WithGZipExtensionHint_ShouldStreamLargeTarEntry() + { + using var file = File.OpenRead(GetFixturePath("Large/Large.tar.gz")); + await using var stream = new AsyncOnlyStream(new ForwardOnlyStream(file)); + var options = new ReaderOptions { ExtensionHint = "gz" }; + + await using var reader = await ReaderFactory.OpenAsyncReader(stream, options); + + Assert.True(await reader.MoveToNextEntryAsync()); + Assert.False(reader.Entry.IsDirectory); + await using var entryStream = await reader.OpenEntryStreamAsync(); + await VerifyContentAsync(reader.Entry.Key, entryStream); + Assert.False(await reader.MoveToNextEntryAsync()); + } + private static string GetFixturePath(string fixtureName) => Path.Combine(TEST_ARCHIVES_PATH, fixtureName); diff --git a/tests/SharpCompress.Test/Tar/TarReaderAsyncTests.cs b/tests/SharpCompress.Test/Tar/TarReaderAsyncTests.cs index 90bce78e..c329d9d2 100644 --- a/tests/SharpCompress.Test/Tar/TarReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/Tar/TarReaderAsyncTests.cs @@ -47,22 +47,38 @@ public class TarReaderAsyncTests : ReaderTests await ReadAsync("Tar.tar.Z", CompressionType.Lzw); [Theory] - [InlineData("Tar.tar.gz", "gz", CompressionType.GZip)] - [InlineData("Tar.tar.Z", "z", CompressionType.Lzw)] + [InlineData("Tar.tar.gz", "gz", CompressionType.GZip, false)] + [InlineData("Tar.tar.gz", "gz", CompressionType.GZip, true)] + [InlineData("Tar.tar.Z", "z", CompressionType.Lzw, false)] + [InlineData("Tar.tar.Z", "z", CompressionType.Lzw, true)] public async ValueTask ReaderFactory_ExtensionHint_PreservesCompressedTarDetection_Async( string archiveName, string extensionHint, - CompressionType compressionType + CompressionType compressionType, + bool useForwardOnlyStream ) { - using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, archiveName)); + using var file = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, archiveName)); + Stream source = useForwardOnlyStream ? new ForwardOnlyStream(file) : file; + await using var stream = new AsyncOnlyStream(source); var options = ReaderOptions.ForExternalStream.WithExtensionHint(extensionHint); await using var reader = await ReaderFactory.OpenAsyncReader(stream, options); Assert.Equal(ArchiveType.Tar, reader.Type); - Assert.True(await reader.MoveToNextEntryAsync()); - Assert.Equal(compressionType, reader.Entry.CompressionType); + var entryCount = 0; + while (await reader.MoveToNextEntryAsync()) + { + entryCount++; + Assert.Equal(compressionType, reader.Entry.CompressionType); + if (!reader.Entry.IsDirectory) + { + await reader.WriteEntryToDirectoryAsync(SCRATCH_FILES_PATH); + } + } + + Assert.True(entryCount > 0); + VerifyFiles(); } [Fact] From 409ab426d20232763e2ce2584fef02ee8b74e8d9 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 4 Aug 2026 07:18:53 +0000 Subject: [PATCH 21/40] Initial plan From e578c8a9b8f271a3b071e6a8e6046605bf737e8e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 4 Aug 2026 07:21:40 +0000 Subject: [PATCH 22/40] Fix formatting: correct indentation of helper methods in TarFactory.cs Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com> --- src/SharpCompress/Factories/TarFactory.cs | 45 ++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/SharpCompress/Factories/TarFactory.cs b/src/SharpCompress/Factories/TarFactory.cs index 7c88b1d6..38dd7ab9 100644 --- a/src/SharpCompress/Factories/TarFactory.cs +++ b/src/SharpCompress/Factories/TarFactory.cs @@ -296,11 +296,53 @@ public class TarFactory #region IReaderFactory + /// + /// Checks whether is backed by a genuinely seekable stream. + /// Note: always reports for + /// non-passthrough instances (it simulates seeking via its ring buffer even over a + /// non-seekable underlying stream), so it must be unwrapped to check the real stream. + /// + private static bool IsGenuinelySeekable(Stream stream) + { + while (stream is SharpCompressStream scs) + { + stream = scs.BaseStream(); + } + return stream.CanSeek; + } + + /// + /// Creates a new wrapper around with + /// its own independent recording/rewind scope, isolated from any recording session the caller may + /// already have in progress on itself. + /// + /// + /// When the stream is genuinely seekable, a is used: + /// it delegates recording/rewinding to the underlying stream's native and + /// never allocates a rewind ring buffer. For non-seekable streams, a ring-buffered + /// is required to support rewinding during format detection. + /// + private static SharpCompressStream CreateNestedRecordingStream(Stream stream) + { + if (!IsGenuinelySeekable(stream)) + { + return new SharpCompressStream(stream); + } + + // Unwrap buffered SharpCompressStream wrappers so we use the real stream's native Seek. + while (stream is SharpCompressStream scs && !scs.IsPassthrough) + { + stream = scs.BaseStream(); + } + + return new SeekableSharpCompressStream(stream, leaveStreamOpen: false); + } + /// public IReader OpenReader(Stream stream, ReaderOptions? options) { options ??= ReaderOptions.ForExternalStream; - var sharpCompressStream = new SharpCompressStream(stream); + var sharpCompressStream = CreateNestedRecordingStream(stream); sharpCompressStream.StartRecording(TarWrapper.MaximumRewindBufferSize); foreach (var wrapper in TarWrapper.Wrappers) { @@ -315,6 +357,7 @@ public class TarFactory ); if (TarArchive.IsTarFile(decompressedStream)) { + sharpCompressStream.Rewind(); sharpCompressStream.StopRecording(); return new TarReader(sharpCompressStream, options, wrapper.CompressionType); } From 62d1b5dcc82d32dcc4254f0d1601767e8a04a08b Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 4 Aug 2026 09:11:26 +0100 Subject: [PATCH 23/40] Provide a better way to manage async disposable streams --- .../Archives/IArchiveEntryExtensions.cs | 9 ++--- src/SharpCompress/Common/EntryStream.Async.cs | 4 +-- src/SharpCompress/Common/EntryStream.cs | 2 +- src/SharpCompress/IO/AsyncDisposableStream.cs | 36 +++++++++++++++++++ src/SharpCompress/IO/AsyncDisposeScope.cs | 36 +++++++++++++++++++ .../IO/ConfiguredAsyncDisposeScope.cs | 19 ++++++++++ .../Polyfills/StreamExtensions.cs | 14 ++++++++ .../Readers/AbstractReader.Async.cs | 15 +------- .../Readers/IAsyncReaderExtensions.cs | 6 ---- .../BZip2/BZip2StreamAsyncTests.cs | 14 +------- .../GZip/GZipCrcExtractionTests.cs | 5 +-- tests/SharpCompress.Test/LargeArchiveTests.cs | 23 ++---------- .../Mocks/AsyncOnlyStream.cs | 3 +- .../Mocks/FlushOnDisposeStream.cs | 5 ++- .../Mocks/ForwardOnlyStream.cs | 7 ++-- tests/SharpCompress.Test/Mocks/TestStream.cs | 7 ++-- .../Mocks/ThrowOnFlushStream.cs | 3 +- .../Mocks/TruncatedStream.cs | 3 +- tests/SharpCompress.Test/ReaderTests.cs | 12 ++----- .../Tar/TarArchiveAsyncTests.cs | 11 ++---- .../Tar/TarReaderAsyncTests.cs | 5 --- .../Tar/TarWriterNonSeekableTests.cs | 7 ++-- .../SharpCompress.Test/Zip/Zip64AsyncTests.cs | 11 ------ .../Zip/ZipCrcExtractionTests.cs | 10 ++---- .../Zip/ZipReaderAsyncTests.cs | 8 ----- .../Zip/ZipWriterNonSeekableTests.cs | 7 ++-- 26 files changed, 141 insertions(+), 141 deletions(-) create mode 100644 src/SharpCompress/IO/AsyncDisposableStream.cs create mode 100644 src/SharpCompress/IO/AsyncDisposeScope.cs create mode 100644 src/SharpCompress/IO/ConfiguredAsyncDisposeScope.cs diff --git a/src/SharpCompress/Archives/IArchiveEntryExtensions.cs b/src/SharpCompress/Archives/IArchiveEntryExtensions.cs index 502a419e..c0e58656 100644 --- a/src/SharpCompress/Archives/IArchiveEntryExtensions.cs +++ b/src/SharpCompress/Archives/IArchiveEntryExtensions.cs @@ -108,15 +108,12 @@ public static class IArchiveEntryExtensions throw new ExtractionException("Entry is a file directory and cannot be extracted."); } -#if LEGACY_DOTNET - using var entryStream = await archiveEntry + var entryStream = await archiveEntry .OpenEntryStreamAsync(cancellationToken) .ConfigureAwait(false); -#else - await using var entryStream = await archiveEntry - .OpenEntryStreamAsync(cancellationToken) + await using var entryStreamScope = entryStream + .DisposeAsyncScope() .ConfigureAwait(false); -#endif var checkedStream = options is null ? entryStream : IEntryExtensions.WrapWithChecksumValidation(archiveEntry, entryStream, options); diff --git a/src/SharpCompress/Common/EntryStream.Async.cs b/src/SharpCompress/Common/EntryStream.Async.cs index a58afe70..b7112da7 100644 --- a/src/SharpCompress/Common/EntryStream.Async.cs +++ b/src/SharpCompress/Common/EntryStream.Async.cs @@ -17,7 +17,6 @@ public partial class EntryStream _completed = true; } -#if !LEGACY_DOTNET public override async ValueTask DisposeAsync() { if (_isDisposed) @@ -43,9 +42,8 @@ public partial class EntryStream } } await base.DisposeAsync().ConfigureAwait(false); - await _stream.DisposeAsync().ConfigureAwait(false); + await _stream.DisposeAsyncCompat().ConfigureAwait(false); } -#endif public override async Task ReadAsync( byte[] buffer, diff --git a/src/SharpCompress/Common/EntryStream.cs b/src/SharpCompress/Common/EntryStream.cs index b8ccf339..ed4e8fc6 100644 --- a/src/SharpCompress/Common/EntryStream.cs +++ b/src/SharpCompress/Common/EntryStream.cs @@ -8,7 +8,7 @@ using SharpCompress.Readers; namespace SharpCompress.Common; -public partial class EntryStream : Stream +public partial class EntryStream : AsyncDisposableStream { private readonly IReader _reader; private readonly Stream _stream; diff --git a/src/SharpCompress/IO/AsyncDisposableStream.cs b/src/SharpCompress/IO/AsyncDisposableStream.cs new file mode 100644 index 00000000..4c7fafbe --- /dev/null +++ b/src/SharpCompress/IO/AsyncDisposableStream.cs @@ -0,0 +1,36 @@ +using System; +using System.IO; +using System.Threading.Tasks; + +namespace SharpCompress.IO; + +/// +/// A that is guaranteed to be asynchronously disposable on every target framework. +/// +/// +/// +/// On .NET Framework 4.8 and .NET Standard 2.0, has no DisposeAsync. +/// Microsoft.Bcl.AsyncInterfaces supplies the interface on those +/// targets but cannot retrofit it onto the BCL's , and C# will not accept an +/// extension method for the pattern - await using requires a reachable instance +/// DisposeAsync. Deriving from this class instead of therefore makes a type +/// usable with await using uniformly, with no conditional compilation at the call site. +/// +/// +/// The fallback below is the same behaviour as the BCL's own default , +/// so a derived type may call await base.DisposeAsync() unconditionally on any target. +/// +/// +public abstract class AsyncDisposableStream : Stream +#if NO_STREAM_DISPOSEASYNC + , IAsyncDisposable +#endif +{ +#if NO_STREAM_DISPOSEASYNC + public virtual ValueTask DisposeAsync() + { + Dispose(); + return default; + } +#endif +} diff --git a/src/SharpCompress/IO/AsyncDisposeScope.cs b/src/SharpCompress/IO/AsyncDisposeScope.cs new file mode 100644 index 00000000..d8167a6c --- /dev/null +++ b/src/SharpCompress/IO/AsyncDisposeScope.cs @@ -0,0 +1,36 @@ +using System; +using System.Threading.Tasks; + +namespace SharpCompress.IO; + +/// +/// Makes any resource usable with await using, disposing it asynchronously when the runtime type +/// supports it and synchronously otherwise. +/// +/// +/// Needed for locals whose static type is (or another type that +/// only sometimes has DisposeAsync), where await using cannot bind directly on +/// .NET Framework 4.8 / .NET Standard 2.0. Unlike a compile-time guard, this picks the asynchronous path +/// based on the runtime type, so a stream that really is asynchronously disposable is disposed that way on +/// every target framework. Prefer deriving from where the type is ours. +/// +internal readonly struct AsyncDisposeScope(IDisposable? resource) : IAsyncDisposable +{ + public ValueTask DisposeAsync() + { + if (resource is IAsyncDisposable asyncDisposable) + { + return asyncDisposable.DisposeAsync(); + } + + resource?.Dispose(); + return default; + } + + /// + /// Mirrors ConfiguredAsyncDisposable so await using can specify context capture without + /// boxing this struct through . + /// + public ConfiguredAsyncDisposeScope ConfigureAwait(bool continueOnCapturedContext) => + new(resource, continueOnCapturedContext); +} \ No newline at end of file diff --git a/src/SharpCompress/IO/ConfiguredAsyncDisposeScope.cs b/src/SharpCompress/IO/ConfiguredAsyncDisposeScope.cs new file mode 100644 index 00000000..f9d631c9 --- /dev/null +++ b/src/SharpCompress/IO/ConfiguredAsyncDisposeScope.cs @@ -0,0 +1,19 @@ +using System; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; + +namespace SharpCompress.IO; + +internal readonly struct ConfiguredAsyncDisposeScope(IDisposable? resource, bool continueOnCapturedContext) +{ + public ConfiguredValueTaskAwaitable DisposeAsync() + { + if (resource is IAsyncDisposable asyncDisposable) + { + return asyncDisposable.DisposeAsync().ConfigureAwait(continueOnCapturedContext); + } + + resource?.Dispose(); + return default(ValueTask).ConfigureAwait(continueOnCapturedContext); + } +} diff --git a/src/SharpCompress/Polyfills/StreamExtensions.cs b/src/SharpCompress/Polyfills/StreamExtensions.cs index c6e66b82..d4e80630 100644 --- a/src/SharpCompress/Polyfills/StreamExtensions.cs +++ b/src/SharpCompress/Polyfills/StreamExtensions.cs @@ -25,6 +25,20 @@ public static class StreamExtensions public void Skip() => stream.CopyTo(Stream.Null); + /// + /// Returns a scope that disposes this stream when awaited, asynchronously where the runtime type + /// supports it. Lets await using be written against a -typed local on + /// every target framework. + /// + internal AsyncDisposeScope DisposeAsyncScope() => new(stream); + + /// + /// Disposes this stream, asynchronously where the runtime type supports it. Use where the static + /// type is , which has no DisposeAsync on .NET Framework 4.8 / + /// .NET Standard 2.0. + /// + internal ValueTask DisposeAsyncCompat() => new AsyncDisposeScope(stream).DisposeAsync(); + public async ValueTask SkipAsync(CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); diff --git a/src/SharpCompress/Readers/AbstractReader.Async.cs b/src/SharpCompress/Readers/AbstractReader.Async.cs index 8628c0c2..969c73e6 100644 --- a/src/SharpCompress/Readers/AbstractReader.Async.cs +++ b/src/SharpCompress/Readers/AbstractReader.Async.cs @@ -104,13 +104,8 @@ public abstract partial class AbstractReader } } //don't know the size so we have to try to decompress to skip -#if LEGACY_DOTNET - using var s = await OpenEntryStreamAsync(cancellationToken).ConfigureAwait(false); - await s.SkipEntryAsync(cancellationToken).ConfigureAwait(false); -#else await using var s = await OpenEntryStreamAsync(cancellationToken).ConfigureAwait(false); await s.SkipEntryAsync(cancellationToken).ConfigureAwait(false); -#endif } public async ValueTask WriteEntryToAsync( @@ -139,19 +134,11 @@ public abstract partial class AbstractReader private async ValueTask WriteAsync(Stream writeStream, CancellationToken cancellationToken) { -#if LEGACY_DOTNET - using Stream s = await OpenEntryStreamAsync(cancellationToken).ConfigureAwait(false); + await using var s = await OpenEntryStreamAsync(cancellationToken).ConfigureAwait(false); var sourceStream = WrapWithProgress(s, Entry); await sourceStream .CopyToAsync(writeStream, Options.BufferSize, cancellationToken) .ConfigureAwait(false); -#else - await using Stream s = await OpenEntryStreamAsync(cancellationToken).ConfigureAwait(false); - var sourceStream = WrapWithProgress(s, Entry); - await sourceStream - .CopyToAsync(writeStream, Options.BufferSize, cancellationToken) - .ConfigureAwait(false); -#endif } public async ValueTask OpenEntryStreamAsync( diff --git a/src/SharpCompress/Readers/IAsyncReaderExtensions.cs b/src/SharpCompress/Readers/IAsyncReaderExtensions.cs index 7515b257..5b4e667b 100644 --- a/src/SharpCompress/Readers/IAsyncReaderExtensions.cs +++ b/src/SharpCompress/Readers/IAsyncReaderExtensions.cs @@ -108,15 +108,9 @@ public static class IAsyncReaderExtensions CancellationToken cancellationToken ) { -#if LEGACY_DOTNET - using var entryStream = await reader - .OpenEntryStreamAsync(cancellationToken) - .ConfigureAwait(false); -#else await using var entryStream = await reader .OpenEntryStreamAsync(cancellationToken) .ConfigureAwait(false); -#endif var checkedStream = IEntryExtensions.WrapWithChecksumValidation( reader.Entry, entryStream, diff --git a/tests/SharpCompress.Test/BZip2/BZip2StreamAsyncTests.cs b/tests/SharpCompress.Test/BZip2/BZip2StreamAsyncTests.cs index d5b2ef78..fc7e649c 100644 --- a/tests/SharpCompress.Test/BZip2/BZip2StreamAsyncTests.cs +++ b/tests/SharpCompress.Test/BZip2/BZip2StreamAsyncTests.cs @@ -151,19 +151,8 @@ public class BZip2StreamAsyncTests Assert.True(compressed.Length > 0); // Decompress and verify -#if LEGACY_DOTNET + // MemoryStream has nothing to dispose asynchronously using (var readStream = new MemoryStream(compressed)) - { - using ( - var bzip2Stream = await BZip2Stream.CreateAsync( - new AsyncOnlyStream(readStream), - SharpCompress.Compressors.CompressionMode.Decompress, - false - ) - ) - { -#else - await using (var readStream = new MemoryStream(compressed)) { await using ( var bzip2Stream = await BZip2Stream.CreateAsync( @@ -173,7 +162,6 @@ public class BZip2StreamAsyncTests ) ) { -#endif var result = new StringBuilder(); var buffer = new byte[256]; int bytesRead; diff --git a/tests/SharpCompress.Test/GZip/GZipCrcExtractionTests.cs b/tests/SharpCompress.Test/GZip/GZipCrcExtractionTests.cs index 2e98c962..acabcb4b 100644 --- a/tests/SharpCompress.Test/GZip/GZipCrcExtractionTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipCrcExtractionTests.cs @@ -68,11 +68,8 @@ public class GZipCrcExtractionTests : TestBase [Fact] public async Task GZipArchive_WriteToFileAsync_Throws_On_Crc_Mismatch() { -#if LEGACY_DOTNET + // MemoryStream has nothing to dispose asynchronously using var stream = new MemoryStream(ReadCorruptedGZipTrailer(corruptCrc: true)); -#else - await using var stream = new MemoryStream(ReadCorruptedGZipTrailer(corruptCrc: true)); -#endif await using var archive = await GZipArchive.OpenAsyncArchive(stream); var entry = await archive.EntriesAsync.SingleAsync(); var destination = Path.Combine(SCRATCH_FILES_PATH, Guid.NewGuid().ToString()); diff --git a/tests/SharpCompress.Test/LargeArchiveTests.cs b/tests/SharpCompress.Test/LargeArchiveTests.cs index 18b427e2..50f1cfec 100644 --- a/tests/SharpCompress.Test/LargeArchiveTests.cs +++ b/tests/SharpCompress.Test/LargeArchiveTests.cs @@ -40,24 +40,15 @@ public class LargeArchiveTests : TestBase [InlineData("Large/Large.7z")] public async Task OpenAsyncArchive_ShouldStreamLargeEntry(string fixtureName) { -#if LEGACY_DOTNET - using var stream = new AsyncOnlyStream( - File.OpenRead(await GetMaterializedFixturePathAsync(fixtureName)) - ); -#else await using var stream = new AsyncOnlyStream( File.OpenRead(await GetMaterializedFixturePathAsync(fixtureName)) ); -#endif await using var archive = await ArchiveFactory.OpenAsyncArchive(stream); var entry = await GetSingleEntryAsync(archive); -#if LEGACY_DOTNET - using var entryStream = await entry.OpenEntryStreamAsync(); -#else - await using var entryStream = await entry.OpenEntryStreamAsync(); -#endif + var entryStream = await entry.OpenEntryStreamAsync(); + await using var entryStreamScope = entryStream.DisposeAsyncScope(); await VerifyContentAsync(entry.Key, entryStream); } @@ -83,25 +74,15 @@ public class LargeArchiveTests : TestBase [InlineData("Large/Large.tar.gz")] public async Task OpenAsyncReader_ShouldStreamLargeEntry(string fixtureName) { -#if LEGACY_DOTNET - using var stream = new AsyncOnlyStream( - File.OpenRead(await GetMaterializedFixturePathAsync(fixtureName)) - ); -#else await using var stream = new AsyncOnlyStream( File.OpenRead(await GetMaterializedFixturePathAsync(fixtureName)) ); -#endif await using var reader = await ReaderFactory.OpenAsyncReader(stream); Assert.True(await reader.MoveToNextEntryAsync()); Assert.False(reader.Entry.IsDirectory); -#if LEGACY_DOTNET - using var entryStream = await reader.OpenEntryStreamAsync(); -#else await using var entryStream = await reader.OpenEntryStreamAsync(); -#endif await VerifyContentAsync(reader.Entry.Key, entryStream); Assert.False(await reader.MoveToNextEntryAsync()); } diff --git a/tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs b/tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs index 232f60ee..9dec5e5a 100644 --- a/tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs +++ b/tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs @@ -2,10 +2,11 @@ using System; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.IO; namespace SharpCompress.Test.Mocks; -public class AsyncOnlyStream(Stream stream, bool disposeStream = true) : Stream +public class AsyncOnlyStream(Stream stream, bool disposeStream = true) : AsyncDisposableStream { private readonly Stream _stream = stream ?? throw new ArgumentNullException(nameof(stream)); diff --git a/tests/SharpCompress.Test/Mocks/FlushOnDisposeStream.cs b/tests/SharpCompress.Test/Mocks/FlushOnDisposeStream.cs index 63f20702..28dff313 100644 --- a/tests/SharpCompress.Test/Mocks/FlushOnDisposeStream.cs +++ b/tests/SharpCompress.Test/Mocks/FlushOnDisposeStream.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Threading.Tasks; +using SharpCompress.IO; namespace SharpCompress.Test.Mocks; @@ -8,7 +9,7 @@ namespace SharpCompress.Test.Mocks; // CryptoStream doesn't always trigger the Flush, so this class is used instead // See https://referencesource.microsoft.com/#mscorlib/system/security/cryptography/cryptostream.cs,141 -public class FlushOnDisposeStream(Stream innerStream) : Stream +public class FlushOnDisposeStream(Stream innerStream) : AsyncDisposableStream { public override bool CanRead => innerStream.CanRead; @@ -48,12 +49,10 @@ public class FlushOnDisposeStream(Stream innerStream) : Stream base.Dispose(disposing); } -#if !LEGACY_DOTNET public override async ValueTask DisposeAsync() { await innerStream.FlushAsync(); innerStream.Close(); await base.DisposeAsync(); } -#endif } diff --git a/tests/SharpCompress.Test/Mocks/ForwardOnlyStream.cs b/tests/SharpCompress.Test/Mocks/ForwardOnlyStream.cs index 32961ebc..f6b7ec8e 100644 --- a/tests/SharpCompress.Test/Mocks/ForwardOnlyStream.cs +++ b/tests/SharpCompress.Test/Mocks/ForwardOnlyStream.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.IO; namespace SharpCompress.Test.Mocks; @@ -9,7 +10,7 @@ namespace SharpCompress.Test.Mocks; /// A forward-only stream wrapper that delegates directly to the underlying stream /// without any buffering. Supports reading and writing but not seeking. /// -public class ForwardOnlyStream : Stream +public class ForwardOnlyStream : AsyncDisposableStream { private readonly Stream _stream; private bool _isDisposed; @@ -142,17 +143,15 @@ public class ForwardOnlyStream : Stream } } -#if !LEGACY_DOTNET public override async ValueTask DisposeAsync() { if (!_isDisposed) { - await _stream.DisposeAsync(); + await _stream.DisposeAsyncCompat(); _isDisposed = true; } await base.DisposeAsync(); } -#endif private void ThrowIfDisposed() { diff --git a/tests/SharpCompress.Test/Mocks/TestStream.cs b/tests/SharpCompress.Test/Mocks/TestStream.cs index d3ff48a8..5061afc3 100644 --- a/tests/SharpCompress.Test/Mocks/TestStream.cs +++ b/tests/SharpCompress.Test/Mocks/TestStream.cs @@ -2,10 +2,11 @@ using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.IO; namespace SharpCompress.Test.Mocks; -public class TestStream(Stream stream, bool read, bool write, bool seek) : Stream +public class TestStream(Stream stream, bool read, bool write, bool seek) : AsyncDisposableStream { public TestStream(Stream stream) : this(stream, stream.CanRead, stream.CanWrite, stream.CanSeek) { } @@ -50,14 +51,14 @@ public class TestStream(Stream stream, bool read, bool write, bool seek) : Strea Memory buffer, CancellationToken cancellationToken = default ) => stream.ReadAsync(buffer, cancellationToken); +#endif public override async ValueTask DisposeAsync() { await base.DisposeAsync(); - await stream.DisposeAsync(); + await stream.DisposeAsyncCompat(); IsDisposed = true; } -#endif public override long Seek(long offset, SeekOrigin origin) => stream.Seek(offset, origin); diff --git a/tests/SharpCompress.Test/Mocks/ThrowOnFlushStream.cs b/tests/SharpCompress.Test/Mocks/ThrowOnFlushStream.cs index 2cf1a84c..911762a5 100644 --- a/tests/SharpCompress.Test/Mocks/ThrowOnFlushStream.cs +++ b/tests/SharpCompress.Test/Mocks/ThrowOnFlushStream.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.IO; namespace SharpCompress.Test.Mocks; @@ -9,7 +10,7 @@ namespace SharpCompress.Test.Mocks; /// A stream wrapper that throws NotSupportedException on Flush() calls. /// This is used to test that archive iteration handles streams that don't support flushing. /// -public class ThrowOnFlushStream : Stream +public class ThrowOnFlushStream : AsyncDisposableStream { private readonly Stream inner; diff --git a/tests/SharpCompress.Test/Mocks/TruncatedStream.cs b/tests/SharpCompress.Test/Mocks/TruncatedStream.cs index 8699a430..55037f2e 100644 --- a/tests/SharpCompress.Test/Mocks/TruncatedStream.cs +++ b/tests/SharpCompress.Test/Mocks/TruncatedStream.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using SharpCompress.IO; namespace SharpCompress.Test.Mocks; @@ -7,7 +8,7 @@ namespace SharpCompress.Test.Mocks; /// A stream wrapper that truncates the underlying stream after reading a specified number of bytes. /// Used for testing error handling when streams end prematurely. /// -public class TruncatedStream : Stream +public class TruncatedStream : AsyncDisposableStream { private readonly Stream baseStream; private readonly long truncateAfterBytes; diff --git a/tests/SharpCompress.Test/ReaderTests.cs b/tests/SharpCompress.Test/ReaderTests.cs index 4c9024c9..64469219 100644 --- a/tests/SharpCompress.Test/ReaderTests.cs +++ b/tests/SharpCompress.Test/ReaderTests.cs @@ -159,18 +159,12 @@ public abstract class ReaderTests : TestBase { using var file = File.OpenRead(testArchive); -#if !LEGACY_DOTNET - await using var protectedStream = SharpCompressStream.CreateNonDisposing( + // SharpCompressStream is not yet an AsyncDisposableStream, so scope its disposal + var protectedStream = SharpCompressStream.CreateNonDisposing( new ForwardOnlyStream(file, options.BufferSize) ); + await using var protectedStreamScope = protectedStream.DisposeAsyncScope(); await using var testStream = new TestStream(protectedStream); -#else - - using var protectedStream = SharpCompressStream.CreateNonDisposing( - new ForwardOnlyStream(file, options.BufferSize) - ); - using var testStream = new TestStream(protectedStream); -#endif await using ( var reader = await ReaderFactory.OpenAsyncReader( new AsyncOnlyStream(testStream), diff --git a/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs b/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs index 3a1b95cc..dabc0fa2 100644 --- a/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs @@ -187,10 +187,6 @@ public class TarArchiveAsyncTests : ArchiveTests } } } -#if LEGACY_DOTNET - //add a delay because old .net sucks on DisposeAsync - await Task.Delay(TimeSpan.FromSeconds(1)); -#endif } [Fact] @@ -338,11 +334,8 @@ public class TarArchiveAsyncTests : ArchiveTests { ++numberOfEntries; -#if LEGACY_DOTNET - using var tarEntryStream = await entry.OpenEntryStreamAsync(); -#else - await using var tarEntryStream = await entry.OpenEntryStreamAsync(); -#endif + var tarEntryStream = await entry.OpenEntryStreamAsync(); + await using var tarEntryStreamScope = tarEntryStream.DisposeAsyncScope(); using var testFileStream = new MemoryStream(); await tarEntryStream.CopyToAsync(testFileStream); Assert.Equal(testBytes.Length, testFileStream.Length); diff --git a/tests/SharpCompress.Test/Tar/TarReaderAsyncTests.cs b/tests/SharpCompress.Test/Tar/TarReaderAsyncTests.cs index 90bce78e..9307b7d3 100644 --- a/tests/SharpCompress.Test/Tar/TarReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/Tar/TarReaderAsyncTests.cs @@ -337,13 +337,8 @@ public class TarReaderAsyncTests : ReaderTests Assert.True(await reader.MoveToNextEntryAsync()); Assert.Equal("inner.tar.gz", reader.Entry.Key); -#if !LEGACY_DOTNET await using var entryStream = await reader.OpenEntryStreamAsync(); await using var flushingStream = new FlushOnDisposeStream(entryStream); -#else - using var entryStream = await reader.OpenEntryStreamAsync(); - using var flushingStream = new FlushOnDisposeStream(entryStream); -#endif // Extract inner.tar.gz await using var innerReader = await ReaderFactory.OpenAsyncReader(flushingStream); diff --git a/tests/SharpCompress.Test/Tar/TarWriterNonSeekableTests.cs b/tests/SharpCompress.Test/Tar/TarWriterNonSeekableTests.cs index 454c20a6..218533db 100644 --- a/tests/SharpCompress.Test/Tar/TarWriterNonSeekableTests.cs +++ b/tests/SharpCompress.Test/Tar/TarWriterNonSeekableTests.cs @@ -89,11 +89,8 @@ public class TarWriterNonSeekableTests { var entry = fileEntries.Single(e => e.Key == name); using var extracted = new MemoryStream(); -#if LEGACY_DOTNET - using (var entryStream = await entry.OpenEntryStreamAsync()) -#else - await using (var entryStream = await entry.OpenEntryStreamAsync()) -#endif + var entryStream = await entry.OpenEntryStreamAsync(); + await using (entryStream.DisposeAsyncScope()) { await entryStream.CopyToAsync(extracted); } diff --git a/tests/SharpCompress.Test/Zip/Zip64AsyncTests.cs b/tests/SharpCompress.Test/Zip/Zip64AsyncTests.cs index 5124b6b0..626265cd 100644 --- a/tests/SharpCompress.Test/Zip/Zip64AsyncTests.cs +++ b/tests/SharpCompress.Test/Zip/Zip64AsyncTests.cs @@ -198,11 +198,7 @@ public class Zip64AsyncTests : WriterTests count++; lastKey = rd.Entry.Key; -#if LEGACY_DOTNET - using var entryStream = await rd.OpenEntryStreamAsync(); -#else await using var entryStream = await rd.OpenEntryStreamAsync(); -#endif if (rd.Entry.Key == "small") { using var ms = new MemoryStream(); @@ -337,17 +333,10 @@ public class Zip64AsyncTests : WriterTests ); while (await rd.MoveToNextEntryAsync()) { -#if LEGACY_DOTNET - using (var entryStream = await rd.OpenEntryStreamAsync()) - { - await entryStream.SkipEntryAsync(); - } -#else await using (var entryStream = await rd.OpenEntryStreamAsync()) { await entryStream.SkipEntryAsync(); } -#endif count++; if (prev != null) { diff --git a/tests/SharpCompress.Test/Zip/ZipCrcExtractionTests.cs b/tests/SharpCompress.Test/Zip/ZipCrcExtractionTests.cs index cd0e610c..4a61b150 100644 --- a/tests/SharpCompress.Test/Zip/ZipCrcExtractionTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipCrcExtractionTests.cs @@ -122,11 +122,8 @@ public class ZipCrcExtractionTests : ArchiveTests using var zipStream = CreateZipWithInvalidCrc(useDataDescriptor: false); using var archive = ZipArchive.OpenArchive(zipStream); var entry = archive.Entries.Single(e => !e.IsDirectory); -#if LEGACY_DOTNET + // MemoryStream has nothing to dispose asynchronously using var destination = new MemoryStream(); -#else - await using var destination = new MemoryStream(); -#endif var exception = await Assert.ThrowsAsync(async () => await entry.WriteToAsync(destination, new ExtractionOptions { CheckCrc = true }) @@ -141,11 +138,8 @@ public class ZipCrcExtractionTests : ArchiveTests using var zipStream = CreateZipWithInvalidCrc(useDataDescriptor: false); using var archive = ZipArchive.OpenArchive(zipStream); var entry = archive.Entries.Single(e => !e.IsDirectory); -#if LEGACY_DOTNET + // MemoryStream has nothing to dispose asynchronously using var destination = new MemoryStream(); -#else - await using var destination = new MemoryStream(); -#endif await entry.WriteToAsync(destination, new ExtractionOptions { CheckCrc = false }); diff --git a/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs b/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs index 4c7cd627..78f70b28 100644 --- a/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs @@ -315,11 +315,7 @@ public class ZipReaderAsyncTests : ReaderTests { if (!reader.Entry.IsDirectory) { -#if LEGACY_DOTNET - using var entryStream = await reader.OpenEntryStreamAsync(); -#else await using var entryStream = await reader.OpenEntryStreamAsync(); -#endif // Read some data var buffer = new byte[1024]; await entryStream.ReadAsync(buffer, 0, buffer.Length); @@ -342,11 +338,7 @@ public class ZipReaderAsyncTests : ReaderTests { if (!reader.Entry.IsDirectory) { -#if LEGACY_DOTNET - using var entryStream = await reader.OpenEntryStreamAsync(); -#else await using var entryStream = await reader.OpenEntryStreamAsync(); -#endif // Read some data var buffer = new byte[1024]; await entryStream.ReadAsync(buffer, 0, buffer.Length); diff --git a/tests/SharpCompress.Test/Zip/ZipWriterNonSeekableTests.cs b/tests/SharpCompress.Test/Zip/ZipWriterNonSeekableTests.cs index 4dd10b57..95cdcc56 100644 --- a/tests/SharpCompress.Test/Zip/ZipWriterNonSeekableTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipWriterNonSeekableTests.cs @@ -163,11 +163,8 @@ public class ZipWriterNonSeekableTests { var entry = archive.Entries.Single(e => e.Key == name); using var extracted = new MemoryStream(); -#if LEGACY_DOTNET - using (var entryStream = await entry.OpenEntryStreamAsync()) -#else - await using (var entryStream = await entry.OpenEntryStreamAsync()) -#endif + var entryStream = await entry.OpenEntryStreamAsync(); + await using (entryStream.DisposeAsyncScope()) { await entryStream.CopyToAsync(extracted); } From 693921ec730588fbf5940e02465465334aae6b7c Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 4 Aug 2026 09:12:00 +0100 Subject: [PATCH 24/40] fmt --- src/SharpCompress/IO/AsyncDisposeScope.cs | 2 +- src/SharpCompress/IO/ConfiguredAsyncDisposeScope.cs | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/SharpCompress/IO/AsyncDisposeScope.cs b/src/SharpCompress/IO/AsyncDisposeScope.cs index d8167a6c..9d4538e4 100644 --- a/src/SharpCompress/IO/AsyncDisposeScope.cs +++ b/src/SharpCompress/IO/AsyncDisposeScope.cs @@ -33,4 +33,4 @@ internal readonly struct AsyncDisposeScope(IDisposable? resource) : IAsyncDispos /// public ConfiguredAsyncDisposeScope ConfigureAwait(bool continueOnCapturedContext) => new(resource, continueOnCapturedContext); -} \ No newline at end of file +} diff --git a/src/SharpCompress/IO/ConfiguredAsyncDisposeScope.cs b/src/SharpCompress/IO/ConfiguredAsyncDisposeScope.cs index f9d631c9..9ee5e368 100644 --- a/src/SharpCompress/IO/ConfiguredAsyncDisposeScope.cs +++ b/src/SharpCompress/IO/ConfiguredAsyncDisposeScope.cs @@ -4,7 +4,10 @@ using System.Threading.Tasks; namespace SharpCompress.IO; -internal readonly struct ConfiguredAsyncDisposeScope(IDisposable? resource, bool continueOnCapturedContext) +internal readonly struct ConfiguredAsyncDisposeScope( + IDisposable? resource, + bool continueOnCapturedContext +) { public ConfiguredValueTaskAwaitable DisposeAsync() { From 20ea4a3e03a746a16919931e8ce447dda8393251 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 4 Aug 2026 09:27:48 +0100 Subject: [PATCH 25/40] Do need the csproj change --- src/SharpCompress/SharpCompress.csproj | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/SharpCompress/SharpCompress.csproj b/src/SharpCompress/SharpCompress.csproj index 991eccb9..c57cb722 100644 --- a/src/SharpCompress/SharpCompress.csproj +++ b/src/SharpCompress/SharpCompress.csproj @@ -31,6 +31,9 @@ $(DefineConstants);LEGACY_DOTNET + + $(DefineConstants);NO_STREAM_DISPOSEASYNC + true true From c619ec068740083ef59742780c36333e51593e0c Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 4 Aug 2026 09:49:11 +0100 Subject: [PATCH 26/40] change generated dispose to have a sync only option --- src/SharpCompress/Archives/IArchiveEntryExtensions.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/SharpCompress/Archives/IArchiveEntryExtensions.cs b/src/SharpCompress/Archives/IArchiveEntryExtensions.cs index 36912f9e..31b4596a 100644 --- a/src/SharpCompress/Archives/IArchiveEntryExtensions.cs +++ b/src/SharpCompress/Archives/IArchiveEntryExtensions.cs @@ -71,12 +71,16 @@ public static partial class IArchiveEntryExtensions throw new ExtractionException("Entry is a file directory and cannot be extracted."); } +#if SYNC_ONLY + using var entryStream = archiveEntry.OpenEntryStream(); +#else var entryStream = await archiveEntry .OpenEntryStreamAsync(cancellationToken) .ConfigureAwait(false); await using var entryStreamScope = entryStream .DisposeAsyncScope() .ConfigureAwait(false); +#endif var checkedStream = options is null ? entryStream : IEntryExtensions.WrapWithChecksumValidation(archiveEntry, entryStream, options); From 46e740b61385e2665fbdd402e7ee101a42310881 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 4 Aug 2026 10:16:09 +0100 Subject: [PATCH 27/40] Remove UseSyncOverAsyncDispose --- src/SharpCompress/Common/EntryStream.cs | 13 +-------- .../Common/Tar/TarReadOnlySubStream.cs | 13 +-------- .../Common/Zip/WinzipAesCryptoStream.cs | 27 +++++++++---------- src/SharpCompress/Utility.cs | 9 ------- 4 files changed, 15 insertions(+), 47 deletions(-) diff --git a/src/SharpCompress/Common/EntryStream.cs b/src/SharpCompress/Common/EntryStream.cs index d2e3f51c..bf835b93 100644 --- a/src/SharpCompress/Common/EntryStream.cs +++ b/src/SharpCompress/Common/EntryStream.cs @@ -30,18 +30,7 @@ public partial class EntryStream : AsyncDisposableStream _isDisposed = true; if (!(_completed || _reader.Cancelled)) { - if (Utility.UseSyncOverAsyncDispose()) - { -#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits -#pragma warning disable CA2012 - SkipEntryAsync().GetAwaiter().GetResult(); -#pragma warning restore CA2012 -#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits - } - else - { - SkipEntry(); - } + SkipEntry(); } //Need a safe standard approach to this - it's okay for compression to overreads. Handling needs to be standardised diff --git a/src/SharpCompress/Common/Tar/TarReadOnlySubStream.cs b/src/SharpCompress/Common/Tar/TarReadOnlySubStream.cs index 86594f63..c4dc0b72 100644 --- a/src/SharpCompress/Common/Tar/TarReadOnlySubStream.cs +++ b/src/SharpCompress/Common/Tar/TarReadOnlySubStream.cs @@ -29,18 +29,7 @@ internal class TarReadOnlySubStream : Stream _isDisposed = true; if (disposing) { - if (Utility.UseSyncOverAsyncDispose()) - { -#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits -#pragma warning disable CA2012 - AdvanceToNextHeaderAsync().GetAwaiter().GetResult(); -#pragma warning restore CA2012 -#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits - } - else - { - AdvanceToNextHeader(); - } + AdvanceToNextHeader(); } base.Dispose(disposing); } diff --git a/src/SharpCompress/Common/Zip/WinzipAesCryptoStream.cs b/src/SharpCompress/Common/Zip/WinzipAesCryptoStream.cs index f6343c02..8a4eda18 100644 --- a/src/SharpCompress/Common/Zip/WinzipAesCryptoStream.cs +++ b/src/SharpCompress/Common/Zip/WinzipAesCryptoStream.cs @@ -69,28 +69,27 @@ internal partial class WinzipAesCryptoStream : Stream _isDisposed = true; if (disposing) { - // Read out last 10 auth bytes - catch exceptions for async-only streams - if (Utility.UseSyncOverAsyncDispose()) + // Read out last 10 auth bytes +#if LEGACY_DOTNET + // Stream has no DisposeAsync on legacy targets, so async flows fall back to this + // sync Dispose while the underlying stream may be async-only. + var ten = ArrayPool.Shared.Rent(10); + try { - var ten = ArrayPool.Shared.Rent(10); - try - { #pragma warning disable VSTHRD002 // Avoid problematic synchronous waits #pragma warning disable CA2012 - _stream.ReadFullyAsync(ten, 0, 10).GetAwaiter().GetResult(); + _stream.ReadFullyAsync(ten, 0, 10).GetAwaiter().GetResult(); #pragma warning restore CA2012 #pragma warning restore VSTHRD002 // Avoid problematic synchronous waits - } - finally - { - ArrayPool.Shared.Return(ten); - } } - else + finally { - Span ten = stackalloc byte[10]; - _stream.ReadFully(ten); + ArrayPool.Shared.Return(ten); } +#else + Span ten = stackalloc byte[10]; + _stream.ReadFully(ten); +#endif _stream.Dispose(); } base.Dispose(disposing); diff --git a/src/SharpCompress/Utility.cs b/src/SharpCompress/Utility.cs index c03c3643..c5824d51 100644 --- a/src/SharpCompress/Utility.cs +++ b/src/SharpCompress/Utility.cs @@ -23,15 +23,6 @@ internal static partial class Utility ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; - public static bool UseSyncOverAsyncDispose() - { - var useSyncOverAsync = false; -#if LEGACY_DOTNET - useSyncOverAsync = true; -#endif - return useSyncOverAsync; - } - private static readonly HashSet invalidChars = new(Path.GetInvalidFileNameChars()); public static ReadOnlyCollection ToReadOnly(this IList items) => new(items); From 87f5616d1f94c4f1d57fad211794124e36e7f437 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 4 Aug 2026 11:45:13 +0100 Subject: [PATCH 28/40] Detect v Inspect is added. Detect is used only basic info but does not buffer as much as it can. --- docs/API.md | 46 +- docs/FORMATS.md | 2 +- docs/USAGE.md | 26 +- .../Archives/ArchiveAccessMode.cs | 25 + .../Archives/ArchiveDetection.cs | 43 ++ .../Archives/ArchiveEncryptionScope.cs | 25 + .../Archives/ArchiveFactory.Detection.cs | 370 ++++++---- .../ArchiveFactory.Information.Async.cs | 537 ++++++++++++++ .../Archives/ArchiveFactory.Information.cs | 682 ++++++++++++++++++ .../Archives/ArchiveInformation.cs | 179 +++-- .../Archives/ArchiveInformationLimitations.cs | 30 + .../Archives/ArchiveInformationStatus.cs | 17 + .../Archives/ZipArchiveInformation.cs | 15 + src/SharpCompress/Common/Rar/RarVolume.cs | 2 + src/SharpCompress/IO/ArchiveOffsetStream.cs | 87 +++ .../SharpCompress.Test/ArchiveFactoryTests.cs | 330 +++++++-- 16 files changed, 2125 insertions(+), 291 deletions(-) create mode 100644 src/SharpCompress/Archives/ArchiveAccessMode.cs create mode 100644 src/SharpCompress/Archives/ArchiveDetection.cs create mode 100644 src/SharpCompress/Archives/ArchiveEncryptionScope.cs create mode 100644 src/SharpCompress/Archives/ArchiveFactory.Information.Async.cs create mode 100644 src/SharpCompress/Archives/ArchiveFactory.Information.cs create mode 100644 src/SharpCompress/Archives/ArchiveInformationLimitations.cs create mode 100644 src/SharpCompress/Archives/ArchiveInformationStatus.cs create mode 100644 src/SharpCompress/Archives/ZipArchiveInformation.cs create mode 100644 src/SharpCompress/IO/ArchiveOffsetStream.cs diff --git a/docs/API.md b/docs/API.md index cda03364..1536c9c6 100644 --- a/docs/API.md +++ b/docs/API.md @@ -50,20 +50,26 @@ if (ArchiveFactory.IsArchive("archive.zip", out var archiveType)) Console.WriteLine($"Detected {archiveType}"); } -// Detect capabilities before choosing Archive API vs Reader API -var info = ArchiveFactory.GetArchiveInformation("archive.arc"); -if (info is not null) +// Detect the format and choose an API without enumerating entries. +var detection = ArchiveFactory.DetectArchive("archive.tar.xz"); +if (detection is not null) { - Console.WriteLine($"Type: {info.Type}"); - Console.WriteLine($"Supports random access: {info.SupportsRandomAccess}"); - Console.WriteLine($"ZIP data descriptor entries: {info.ZipDataDescriptorEntryCount}"); - Console.WriteLine($"Solid stream count: {info.SolidStreamCount}"); + Console.WriteLine($"Container: {detection.ContainerType}"); + Console.WriteLine($"Outer compression: {detection.OuterCompressionType}"); + Console.WriteLine($"Supported APIs: {detection.SupportedApis}"); } -var asyncInfo = await ArchiveFactory.GetArchiveInformationAsync( - "archive.zip", - cancellationToken -); +// InspectArchive parses complete archive metadata. It can be expensive for +// sequential formats such as TAR and compressed TAR. +var information = ArchiveFactory.InspectArchive("archive.zip"); +if (information is not null) +{ + Console.WriteLine($"Entries: {information.EntryCount}"); + Console.WriteLine($"Solid streams: {information.SolidStreamCount}"); + Console.WriteLine($"ZIP data descriptor entries: {information.Zip?.DataDescriptorEntryCount}"); +} + +var asyncInformation = await ArchiveFactory.InspectArchiveAsync("archive.zip", cancellationToken); // Multi-volume archives var parts = ArchiveFactory.GetFileParts("archive.part1.rar") @@ -75,7 +81,23 @@ using (var archive = ArchiveFactory.OpenArchive(parts)) } ``` -`ArchiveInformation.SupportsRandomAccess` is `true` when the detected format supports `IArchive` random access. It is `false` for reader-only formats such as Ace, Arc, Arj, and standalone LZW, where `ReaderFactory.OpenReader` should be used instead. Compressed tar wrappers such as `.tar.gz` and `.tar.xz` are also reader-only; `ArchiveFactory.GetArchiveInformation` returns `null` for them and `ArchiveFactory.OpenArchive` does not open them as the outer compression wrapper. Use `ReaderFactory.OpenReader` or `TarReader.OpenReader` for those files. Format-specific details are available through `ArchiveInformation.ZipDataDescriptorEntryCount` (ZIP) and `ArchiveInformation.SolidStreamCount` (solid-capable formats such as 7z and RAR). +`DetectArchive` identifies the logical container, its outer compression wrapper, and whether the Archive and Reader APIs are available without enumerating entries. Compressed TAR wrappers such as `.tar.gz` and `.tar.xz` are identified as `ArchiveType.Tar` with an outer compression type and `ArchiveAccessMode.Reader`. + +`InspectArchive` enumerates metadata and returns `ArchiveInformation`. It reports `Partial` status for missing volumes or encrypted headers without a password; malformed archives and incorrect passwords throw. ZIP-specific metadata is exposed through `ArchiveInformation.Zip`. + +The stream overloads of `DetectArchive` and `InspectArchive` preserve the supplied stream's position and leave it open, including when `ReaderOptions.LeaveStreamOpen` is `false`. + +#### Migrating from `GetArchiveInformation` + +| Removed API | Replacement | +| --- | --- | +| `GetArchiveInformation(...)` used only to identify a format | `DetectArchive(...)` | +| `GetArchiveInformation(...)` used to enumerate archive details | `InspectArchive(...)` | +| `GetArchiveInformationAsync(...)` | `DetectArchiveAsync(...)` or `InspectArchiveAsync(...)` | +| `Type` | `Detection.ContainerType` | +| `SupportsRandomAccess` | `Detection.SupportedApis.HasFlag(ArchiveAccessMode.Archive)` | +| `ZipDataDescriptorEntryCount` | `Zip.DataDescriptorEntryCount` | +| `SolidStreamCount` | `SolidStreamCount` | ### Creating Archives diff --git a/docs/FORMATS.md b/docs/FORMATS.md index 22a7080a..a9309c06 100644 --- a/docs/FORMATS.md +++ b/docs/FORMATS.md @@ -31,7 +31,7 @@ 4. The 7Zip format doesn't allow for reading as a forward-only stream, so 7Zip read support is only through the Archive API. Writing is supported through SevenZipWriter for non-solid archives with LZMA/LZMA2 and requires a seekable output stream. See [7Zip Format Notes](#7zip-format-notes) for details on async extraction behavior. 5. LZip has no support for extra data like the file name or timestamp. There is a default filename used when looking at the entry Key on the archive. -`ArchiveFactory.GetArchiveInformation(...).SupportsRandomAccess` is `true` when the detected format has an Archive API in this table. It is `false` for reader-only formats such as Ace, Arc, Arj, and standalone LZW. Compressed tar wrappers are supported by `ReaderFactory`/`TarReader`, not by `ArchiveFactory`/`TarArchive`; ArchiveFactory detection blocks them instead of opening the outer compression wrapper as a standalone archive. +`ArchiveFactory.DetectArchive(...)` reports which APIs in this table are available through `ArchiveDetection.SupportedApis`. Reader-only formats include Ace, Arc, Arj, and standalone LZW. Compressed TAR wrappers are detected as a TAR container with an outer compression type and support the Reader API, not the Archive API. Use `ArchiveFactory.InspectArchive(...)` when complete archive metadata is required. ### Zip Format Notes diff --git a/docs/USAGE.md b/docs/USAGE.md index 9666d43f..838cd16f 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -227,16 +227,16 @@ using (var archive = ArchiveFactory.OpenArchive("archive.zip")) } ``` -### Use ArchiveInformation to choose the right API +### Detect the format and choose the right API ```C# var archivePath = "archive.arc"; -var info = ArchiveFactory.GetArchiveInformation(archivePath); -if (info is null) +var detection = ArchiveFactory.DetectArchive(archivePath); +if (detection is null) { Console.WriteLine("Not a supported archive"); } -else if (info.SupportsRandomAccess) +else if (detection.SupportedApis.HasFlag(ArchiveAccessMode.Archive)) { using var archive = ArchiveFactory.OpenArchive(archivePath); archive.WriteToDirectory(@"D:\output"); @@ -248,7 +248,23 @@ else } ``` -`SupportsRandomAccess` is `false` for reader-only formats such as Ace, Arc, Arj, and standalone LZW. Use the Reader API for those formats. +`DetectArchive` does not enumerate entries. It reports both Archive and Reader API availability; reader-only formats include Ace, Arc, Arj, standalone LZW, and compressed TAR wrappers. + +### Inspect archive metadata + +```C# +var information = ArchiveFactory.InspectArchive(archivePath); +if (information is not null) +{ + Console.WriteLine($"Entries: {information.EntryCount}"); + Console.WriteLine($"Compressed bytes: {information.CompressedPayloadSize}"); + Console.WriteLine($"Solid streams: {information.SolidStreamCount}"); +} +``` + +`InspectArchive` parses archive metadata, which can require a complete sequential scan for TAR and compressed TAR files. It returns partial information for encrypted headers without a password or missing archive parts; inspect `Status` and `Limitations` before using nullable metadata values. + +The stream overload preserves the caller's current position and leaves the supplied stream open. ### Open multi-volume archives diff --git a/src/SharpCompress/Archives/ArchiveAccessMode.cs b/src/SharpCompress/Archives/ArchiveAccessMode.cs new file mode 100644 index 00000000..a86afdb2 --- /dev/null +++ b/src/SharpCompress/Archives/ArchiveAccessMode.cs @@ -0,0 +1,25 @@ +using System; + +namespace SharpCompress.Archives; + +/// +/// Specifies the APIs available for an archive format. +/// +[Flags] +public enum ArchiveAccessMode +{ + /// + /// No archive access API is available. + /// + None = 0, + + /// + /// The seekable API is available. + /// + Archive = 1, + + /// + /// The forward-only API is available. + /// + Reader = 2, +} diff --git a/src/SharpCompress/Archives/ArchiveDetection.cs b/src/SharpCompress/Archives/ArchiveDetection.cs new file mode 100644 index 00000000..fe961af8 --- /dev/null +++ b/src/SharpCompress/Archives/ArchiveDetection.cs @@ -0,0 +1,43 @@ +using SharpCompress.Common; + +namespace SharpCompress.Archives; + +/// +/// Identifies an archive format without enumerating its entries. +/// +public sealed class ArchiveDetection +{ + internal ArchiveDetection( + ArchiveType? containerType, + string formatName, + CompressionType? outerCompressionType, + ArchiveAccessMode supportedApis + ) + { + ContainerType = containerType; + FormatName = formatName; + OuterCompressionType = outerCompressionType; + SupportedApis = supportedApis; + } + + /// + /// Gets the logical archive container type, when it is a built-in SharpCompress type. + /// + public ArchiveType? ContainerType { get; } + + /// + /// Gets the detected format name. + /// + public string FormatName { get; } + + /// + /// Gets the compression wrapper around , if present. + /// For example, a tar.xz archive has a TAR container and XZ outer compression. + /// + public CompressionType? OuterCompressionType { get; } + + /// + /// Gets the APIs supported by the detected format. + /// + public ArchiveAccessMode SupportedApis { get; } +} diff --git a/src/SharpCompress/Archives/ArchiveEncryptionScope.cs b/src/SharpCompress/Archives/ArchiveEncryptionScope.cs new file mode 100644 index 00000000..5299e9bf --- /dev/null +++ b/src/SharpCompress/Archives/ArchiveEncryptionScope.cs @@ -0,0 +1,25 @@ +using System; + +namespace SharpCompress.Archives; + +/// +/// Identifies which archive data is encrypted. +/// +[Flags] +public enum ArchiveEncryptionScope +{ + /// + /// No encryption is present. + /// + None = 0, + + /// + /// One or more entry payloads are encrypted. + /// + EntryData = 1, + + /// + /// Archive headers are encrypted. + /// + Headers = 2, +} diff --git a/src/SharpCompress/Archives/ArchiveFactory.Detection.cs b/src/SharpCompress/Archives/ArchiveFactory.Detection.cs index 5674a462..caedc7fd 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.Detection.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.Detection.cs @@ -2,13 +2,8 @@ using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; -using SharpCompress.Archives.Rar; -using SharpCompress.Archives.SevenZip; using SharpCompress.Archives.Tar; -using SharpCompress.Archives.Zip; using SharpCompress.Common; -using SharpCompress.Common.Zip; -using SharpCompress.Common.Zip.Headers; using SharpCompress.Factories; using SharpCompress.IO; using SharpCompress.Providers; @@ -19,26 +14,24 @@ namespace SharpCompress.Archives; public static partial class ArchiveFactory { /// - /// Returns information about the archive at the given file path asynchronously, - /// or if the file is not a recognized archive. + /// Identifies the archive at the given file path without enumerating its entries. /// /// Path to the archive file. /// Cancellation token. - public static async ValueTask GetArchiveInformationAsync( + public static async ValueTask DetectArchiveAsync( string filePath, CancellationToken cancellationToken = default ) => - await GetArchiveInformationAsync(filePath, ReaderOptions.ForFilePath, cancellationToken) + await DetectArchiveAsync(filePath, ReaderOptions.ForFilePath, cancellationToken) .ConfigureAwait(false); /// - /// Returns information about the archive at the given file path asynchronously, - /// or if the file is not a recognized archive. + /// Identifies the archive at the given file path without enumerating its entries. /// /// Path to the archive file. /// Options controlling archive detection. /// Cancellation token. - public static async ValueTask GetArchiveInformationAsync( + public static async ValueTask DetectArchiveAsync( string filePath, ReaderOptions? readerOptions, CancellationToken cancellationToken = default @@ -46,7 +39,7 @@ public static partial class ArchiveFactory { filePath.NotNullOrEmpty(nameof(filePath)); using Stream stream = File.OpenRead(filePath); - return await GetArchiveInformationAsync( + return await DetectArchiveAsync( stream, readerOptions ?? ReaderOptions.ForFilePath, cancellationToken @@ -55,26 +48,24 @@ public static partial class ArchiveFactory } /// - /// Returns information about the archive in the given stream asynchronously, - /// or if the stream is not a recognized archive. + /// Identifies the archive in the given stream without enumerating its entries. /// /// A readable and seekable stream positioned at the start of the archive. /// Cancellation token. - public static async ValueTask GetArchiveInformationAsync( + public static async ValueTask DetectArchiveAsync( Stream stream, CancellationToken cancellationToken = default ) => - await GetArchiveInformationAsync(stream, ReaderOptions.ForExternalStream, cancellationToken) + await DetectArchiveAsync(stream, ReaderOptions.ForExternalStream, cancellationToken) .ConfigureAwait(false); /// - /// Returns information about the archive in the given stream asynchronously, - /// or if the stream is not a recognized archive. + /// Identifies the archive in the given stream without enumerating its entries. /// /// A readable and seekable stream positioned at the start of the archive. /// Options controlling archive detection. /// Cancellation token. - public static async ValueTask GetArchiveInformationAsync( + public static async ValueTask DetectArchiveAsync( Stream stream, ReaderOptions? readerOptions, CancellationToken cancellationToken = default @@ -83,19 +74,12 @@ public static partial class ArchiveFactory stream.RequireReadable(); stream.RequireSeekable(); - var factory = await TryFindFactoryAsync( + return await TryDetectArchiveAsync( stream, readerOptions ?? ReaderOptions.ForExternalStream, cancellationToken ) .ConfigureAwait(false); - return factory is null - ? null - : BuildArchiveInformation( - stream, - readerOptions ?? ReaderOptions.ForExternalStream, - factory - ); } internal static ValueTask FindFactoryAsync( @@ -187,87 +171,80 @@ public static partial class ArchiveFactory { var startPosition = stream.Position; - foreach (var factory in Factory.Factories) + try { - stream.Seek(startPosition, SeekOrigin.Begin); - var isArchive = await factory - .IsArchiveAsync(stream, readerOptions, cancellationToken) - .ConfigureAwait(false); - - if (isArchive) + foreach (var factory in Factory.Factories) { stream.Seek(startPosition, SeekOrigin.Begin); - if ( - await IsCompressedTarAsync(stream, factory, readerOptions, cancellationToken) - .ConfigureAwait(false) - ) + var isArchive = await factory + .IsArchiveAsync(stream, readerOptions, cancellationToken) + .ConfigureAwait(false); + + if (isArchive) { - continue; + stream.Seek(startPosition, SeekOrigin.Begin); + if ( + await IsCompressedTarAsync( + stream, + factory, + readerOptions, + cancellationToken + ) + .ConfigureAwait(false) + ) + { + continue; + } + + return factory; } - - stream.Seek(startPosition, SeekOrigin.Begin); - return factory; } - } - stream.Seek(startPosition, SeekOrigin.Begin); - return null; + return null; + } + finally + { + stream.Seek(startPosition, SeekOrigin.Begin); + } } /// - /// Returns information about the archive at the given file path, - /// or if the file is not a recognized archive. + /// Identifies the archive at the given file path without enumerating its entries. /// /// Path to the archive file. - public static ArchiveInformation? GetArchiveInformation(string filePath) => - GetArchiveInformation(filePath, ReaderOptions.ForFilePath); + public static ArchiveDetection? DetectArchive(string filePath) => + DetectArchive(filePath, ReaderOptions.ForFilePath); /// - /// Returns information about the archive at the given file path, - /// or if the file is not a recognized archive. + /// Identifies the archive at the given file path without enumerating its entries. /// /// Path to the archive file. /// Options controlling archive detection. - public static ArchiveInformation? GetArchiveInformation( - string filePath, - ReaderOptions? readerOptions - ) + public static ArchiveDetection? DetectArchive(string filePath, ReaderOptions? readerOptions) { filePath.NotNullOrEmpty(nameof(filePath)); using Stream stream = File.OpenRead(filePath); - return GetArchiveInformation(stream, readerOptions ?? ReaderOptions.ForFilePath); + return DetectArchive(stream, readerOptions ?? ReaderOptions.ForFilePath); } /// - /// Returns information about the archive in the given stream, - /// or if the stream is not a recognized archive. + /// Identifies the archive in the given stream without enumerating its entries. /// /// A readable and seekable stream positioned at the start of the archive. - public static ArchiveInformation? GetArchiveInformation(Stream stream) => - GetArchiveInformation(stream, ReaderOptions.ForExternalStream); + public static ArchiveDetection? DetectArchive(Stream stream) => + DetectArchive(stream, ReaderOptions.ForExternalStream); /// - /// Returns information about the archive in the given stream, - /// or if the stream is not a recognized archive. + /// Identifies the archive in the given stream without enumerating its entries. /// /// A readable and seekable stream positioned at the start of the archive. /// Options controlling archive detection. - public static ArchiveInformation? GetArchiveInformation( - Stream stream, - ReaderOptions? readerOptions - ) + public static ArchiveDetection? DetectArchive(Stream stream, ReaderOptions? readerOptions) { stream.RequireReadable(); stream.RequireSeekable(); - var factory = TryFindFactory(stream, readerOptions ?? ReaderOptions.ForExternalStream); - return factory is null - ? null - : BuildArchiveInformation( - stream, - readerOptions ?? ReaderOptions.ForExternalStream, - factory - ); + return TryDetectArchive(stream, readerOptions ?? ReaderOptions.ForExternalStream); } /// @@ -278,7 +255,7 @@ public static partial class ArchiveFactory /// /// This is the shared, seekable-stream detection core used by /// , , - /// and . + /// and . /// /// uses a separate code path /// based on rewindable buffering, which supports @@ -292,26 +269,31 @@ public static partial class ArchiveFactory { var startPosition = stream.Position; - foreach (var factory in Factory.Factories) + try { - stream.Seek(startPosition, SeekOrigin.Begin); - var isArchive = factory.IsArchive(stream, readerOptions); - - if (isArchive) + foreach (var factory in Factory.Factories) { stream.Seek(startPosition, SeekOrigin.Begin); - if (IsCompressedTar(stream, factory, readerOptions)) + var isArchive = factory.IsArchive(stream, readerOptions); + + if (isArchive) { - continue; + stream.Seek(startPosition, SeekOrigin.Begin); + if (IsCompressedTar(stream, factory, readerOptions)) + { + continue; + } + + return factory; } - - stream.Seek(startPosition, SeekOrigin.Begin); - return factory; } - } - stream.Seek(startPosition, SeekOrigin.Begin); - return null; + return null; + } + finally + { + stream.Seek(startPosition, SeekOrigin.Begin); + } } private static bool IsCompressedTar( @@ -425,114 +407,184 @@ public static partial class ArchiveFactory _ => null, }; - private static ArchiveInformation BuildArchiveInformation( - Stream stream, - ReaderOptions readerOptions, - IFactory factory - ) + private static ArchiveDetection? TryDetectArchive(Stream stream, ReaderOptions readerOptions) { - var info = new ArchiveInformation(factory.KnownArchiveType, factory is IArchiveFactory); var startPosition = stream.Position; try { - var probeReaderOptions = readerOptions with { LeaveStreamOpen = true }; - switch (factory) + foreach (var factory in Factory.Factories) { - case ZipFactory: - TryPopulateZipDetails(info, stream, probeReaderOptions); - break; - case SevenZipFactory: - TryPopulateSevenZipDetails(info, stream, probeReaderOptions); - break; - case RarFactory: - TryPopulateRarDetails(info, stream, probeReaderOptions); - break; + stream.Seek(startPosition, SeekOrigin.Begin); + if (!factory.IsArchive(stream, readerOptions)) + { + continue; + } + + if (GetCompressedTarType(factory) is { } compressionType) + { + stream.Seek(startPosition, SeekOrigin.Begin); + if (IsCompressedTar(stream, readerOptions, compressionType)) + { + return CreateCompressedTarDetection(compressionType); + } + } + + return CreateDetection(factory); } + + return + TryDetectCompressedTar(stream, readerOptions, startPosition) + is { } compressedTarType + ? CreateCompressedTarDetection(compressedTarType) + : null; } finally { stream.Seek(startPosition, SeekOrigin.Begin); } - - return info; } - private static void TryPopulateZipDetails( - ArchiveInformation info, + private static async ValueTask TryDetectArchiveAsync( Stream stream, - ReaderOptions readerOptions + ReaderOptions readerOptions, + CancellationToken cancellationToken ) { + var startPosition = stream.Position; + try { - info.ZipDataDescriptorEntryCount = GetZipDataDescriptorEntryCount( - stream, - readerOptions - ); + foreach (var factory in Factory.Factories) + { + stream.Seek(startPosition, SeekOrigin.Begin); + if ( + !await factory + .IsArchiveAsync(stream, readerOptions, cancellationToken) + .ConfigureAwait(false) + ) + { + continue; + } + + if (GetCompressedTarType(factory) is { } compressionType) + { + stream.Seek(startPosition, SeekOrigin.Begin); + if ( + await IsCompressedTarAsync( + stream, + readerOptions, + compressionType, + cancellationToken + ) + .ConfigureAwait(false) + ) + { + return CreateCompressedTarDetection(compressionType); + } + } + + return CreateDetection(factory); + } + + var compressedTarType = await TryDetectCompressedTarAsync( + stream, + readerOptions, + startPosition, + cancellationToken + ) + .ConfigureAwait(false); + return compressedTarType is { } value ? CreateCompressedTarDetection(value) : null; } - catch + finally { - // Keep archive detection resilient even when format-specific probing fails. + stream.Seek(startPosition, SeekOrigin.Begin); } } - private static void TryPopulateSevenZipDetails( - ArchiveInformation info, + private static CompressionType? TryDetectCompressedTar( Stream stream, - ReaderOptions readerOptions + ReaderOptions readerOptions, + long startPosition ) { - try + foreach (var wrapper in TarWrapper.Wrappers) { - info.SolidStreamCount = GetSevenZipSolidStreamCount(stream, readerOptions); - } - catch - { - // Keep archive detection resilient even when format-specific probing fails. + if (wrapper.CompressionType == CompressionType.None) + { + continue; + } + + stream.Seek(startPosition, SeekOrigin.Begin); + if (!wrapper.IsMatch(stream)) + { + continue; + } + + stream.Seek(startPosition, SeekOrigin.Begin); + if (IsCompressedTar(stream, readerOptions, wrapper.CompressionType)) + { + return wrapper.CompressionType; + } } + + return null; } - private static void TryPopulateRarDetails( - ArchiveInformation info, + private static async ValueTask TryDetectCompressedTarAsync( Stream stream, - ReaderOptions readerOptions + ReaderOptions readerOptions, + long startPosition, + CancellationToken cancellationToken ) { - try + foreach (var wrapper in TarWrapper.Wrappers) { - info.SolidStreamCount = GetRarSolidStreamCount(stream, readerOptions); + cancellationToken.ThrowIfCancellationRequested(); + if (wrapper.CompressionType == CompressionType.None) + { + continue; + } + + stream.Seek(startPosition, SeekOrigin.Begin); + if (!await wrapper.IsMatchAsync(stream, cancellationToken).ConfigureAwait(false)) + { + continue; + } + + stream.Seek(startPosition, SeekOrigin.Begin); + if ( + await IsCompressedTarAsync( + stream, + readerOptions, + wrapper.CompressionType, + cancellationToken + ) + .ConfigureAwait(false) + ) + { + return wrapper.CompressionType; + } } - catch + + return null; + } + + private static ArchiveDetection CreateDetection(IFactory factory) + { + var supportedApis = ArchiveAccessMode.None; + if (factory is IArchiveFactory) { - // Keep archive detection resilient even when format-specific probing fails. + supportedApis |= ArchiveAccessMode.Archive; } + if (factory is IReaderFactory) + { + supportedApis |= ArchiveAccessMode.Reader; + } + + return new ArchiveDetection(factory.KnownArchiveType, factory.Name, null, supportedApis); } - private static int GetZipDataDescriptorEntryCount(Stream stream, ReaderOptions readerOptions) - { - using var archive = ZipArchive.OpenArchive(stream, readerOptions); - return archive - .Entries.OfType() - .SelectMany(entry => entry.Parts.OfType()) - .Count(part => - FlagUtility.HasFlag(part.Header.Flags, HeaderFlags.UsePostDataDescriptor) - ); - } - - private static int GetSevenZipSolidStreamCount(Stream stream, ReaderOptions readerOptions) - { - using var archive = SevenZipArchive.OpenArchive(stream, readerOptions); - return archive - .Entries.OfType() - .Where(entry => !entry.IsDirectory && entry.FilePart.Folder is not null) - .GroupBy(entry => entry.FilePart.Folder) - .Count(group => group.Skip(1).Any()); - } - - private static int GetRarSolidStreamCount(Stream stream, ReaderOptions readerOptions) - { - using var archive = RarArchive.OpenArchive(stream, readerOptions); - return archive.IsSolid ? 1 : 0; - } + private static ArchiveDetection CreateCompressedTarDetection(CompressionType compressionType) => + new(ArchiveType.Tar, "Tar", compressionType, ArchiveAccessMode.Reader); } diff --git a/src/SharpCompress/Archives/ArchiveFactory.Information.Async.cs b/src/SharpCompress/Archives/ArchiveFactory.Information.Async.cs new file mode 100644 index 00000000..9c8076ef --- /dev/null +++ b/src/SharpCompress/Archives/ArchiveFactory.Information.Async.cs @@ -0,0 +1,537 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using SharpCompress.Archives.Rar; +using SharpCompress.Archives.SevenZip; +using SharpCompress.Common; +using SharpCompress.Common.Ace.Headers; +using SharpCompress.Common.Rar; +using SharpCompress.Common.Zip; +using SharpCompress.IO; +using SharpCompress.Readers; + +namespace SharpCompress.Archives; + +public static partial class ArchiveFactory +{ + /// + /// Asynchronously collects metadata for the archive at the given file path. + /// + /// Path to the archive file. + /// Cancellation token. + /// Archive metadata, or when the file is not a supported archive. + public static async ValueTask InspectArchiveAsync( + string filePath, + CancellationToken cancellationToken = default + ) => + await InspectArchiveAsync(filePath, ReaderOptions.ForFilePath, cancellationToken) + .ConfigureAwait(false); + + /// + /// Asynchronously collects metadata for the archive at the given file path. + /// + /// Path to the archive file. + /// Options controlling archive inspection. + /// Cancellation token. + /// Archive metadata, or when the file is not a supported archive. + public static async ValueTask InspectArchiveAsync( + string filePath, + ReaderOptions? readerOptions, + CancellationToken cancellationToken = default + ) + { + filePath.NotNullOrEmpty(nameof(filePath)); + var options = readerOptions ?? ReaderOptions.ForFilePath; + var detection = await DetectArchiveAsync(filePath, options, cancellationToken) + .ConfigureAwait(false); + if (detection is null) + { + return null; + } + if ((detection.SupportedApis & ArchiveAccessMode.Archive) != 0) + { + var fileInfos = GetArchiveFileParts(new FileInfo(filePath), options); + if (fileInfos.Length > 1) + { + return await InspectArchiveAsync(fileInfos, options, cancellationToken) + .ConfigureAwait(false); + } + } + + using Stream stream = File.OpenRead(filePath); + return await InspectArchiveAsync(stream, options, cancellationToken).ConfigureAwait(false); + } + + /// + /// Asynchronously collects metadata for the archive in the given stream. + /// + /// A readable and seekable stream positioned at the start of the archive. + /// Cancellation token. + /// Archive metadata, or when the stream is not a supported archive. + public static async ValueTask InspectArchiveAsync( + Stream stream, + CancellationToken cancellationToken = default + ) => + await InspectArchiveAsync(stream, ReaderOptions.ForExternalStream, cancellationToken) + .ConfigureAwait(false); + + /// + /// Asynchronously collects metadata for the archive in the given stream. + /// + /// A readable and seekable stream positioned at the start of the archive. + /// Options controlling archive inspection. + /// Cancellation token. + /// Archive metadata, or when the stream is not a supported archive. + /// The supplied stream remains open and is restored to its original position. + public static async ValueTask InspectArchiveAsync( + Stream stream, + ReaderOptions? readerOptions, + CancellationToken cancellationToken = default + ) + { + stream.RequireReadable(); + stream.RequireSeekable(); + cancellationToken.ThrowIfCancellationRequested(); + + var options = readerOptions ?? ReaderOptions.ForExternalStream; + var startPosition = stream.Position; + var physicalSize = GetPhysicalSize(stream, startPosition); + + try + { + using var archiveStream = new ArchiveOffsetStream(stream); + var inspectionOptions = options with { LeaveStreamOpen = true }; + var detection = await TryDetectArchiveAsync( + archiveStream, + inspectionOptions, + cancellationToken + ) + .ConfigureAwait(false); + if (detection is null) + { + return null; + } + + if ((detection.SupportedApis & ArchiveAccessMode.Archive) != 0) + { + await using var archive = await OpenAsyncArchive( + archiveStream, + inspectionOptions, + cancellationToken + ) + .ConfigureAwait(false); + return await InspectOpenedArchiveAsync( + archive, + detection, + physicalSize, + 1, + cancellationToken + ) + .ConfigureAwait(false); + } + + var aceHeader = await ReadAceHeaderAsync( + archiveStream, + detection, + inspectionOptions, + cancellationToken + ) + .ConfigureAwait(false); + await using var reader = await ReaderFactory + .OpenAsyncReader(archiveStream, inspectionOptions, cancellationToken) + .ConfigureAwait(false); + return await InspectOpenedReaderAsync( + reader, + detection, + physicalSize, + 1, + aceHeader, + cancellationToken + ) + .ConfigureAwait(false); + } + catch (CryptographicException) when (string.IsNullOrEmpty(options.Password)) + { + var detection = await RedetectArchiveAsync( + stream, + startPosition, + options, + cancellationToken + ) + .ConfigureAwait(false); + return detection is null + ? null + : CreatePartialInformation( + detection, + physicalSize, + 1, + ArchiveInformationLimitations.EncryptedHeaders + ); + } + catch (MultipartStreamRequiredException) + { + var detection = await RedetectArchiveAsync( + stream, + startPosition, + options, + cancellationToken + ) + .ConfigureAwait(false); + return detection is null + ? null + : CreatePartialInformation( + detection, + physicalSize, + 1, + ArchiveInformationLimitations.MissingVolumes + ); + } + finally + { + stream.Seek(startPosition, SeekOrigin.Begin); + } + } + + /// + /// Asynchronously collects metadata for an archive opened from multiple files. + /// + /// Archive source files in archive order. + /// Options controlling archive inspection. + /// Cancellation token. + /// Archive metadata, or when the files are not a supported archive. + public static async ValueTask InspectArchiveAsync( + IReadOnlyList fileInfos, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + fileInfos.NotNull(nameof(fileInfos)); + if (fileInfos.Count == 0) + { + throw new ArchiveOperationException("No files to inspect"); + } + if (fileInfos.Count == 1) + { + return await InspectArchiveAsync( + fileInfos[0].FullName, + readerOptions, + cancellationToken + ) + .ConfigureAwait(false); + } + + var options = readerOptions ?? ReaderOptions.ForFilePath; + var detection = await DetectArchiveAsync(fileInfos[0].FullName, options, cancellationToken) + .ConfigureAwait(false); + if (detection is null) + { + return null; + } + if ((detection.SupportedApis & ArchiveAccessMode.Archive) == 0) + { + throw new NotSupportedException( + "Inspecting multiple source files is supported only for formats with an Archive API." + ); + } + + var physicalSize = GetPhysicalSize(fileInfos); + try + { + await using var archive = await OpenAsyncArchive(fileInfos, options, cancellationToken) + .ConfigureAwait(false); + return await InspectOpenedArchiveAsync( + archive, + detection, + physicalSize, + fileInfos.Count, + cancellationToken + ) + .ConfigureAwait(false); + } + catch (CryptographicException) when (string.IsNullOrEmpty(options.Password)) + { + return CreatePartialInformation( + detection, + physicalSize, + fileInfos.Count, + ArchiveInformationLimitations.EncryptedHeaders + ); + } + } + + /// + /// Asynchronously collects metadata for an archive opened from multiple streams. + /// + /// Archive source streams in archive order. + /// Options controlling archive inspection. + /// Cancellation token. + /// Archive metadata, or when the streams are not a supported archive. + public static async ValueTask InspectArchiveAsync( + IReadOnlyList streams, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + streams.NotNull(nameof(streams)); + if (streams.Count == 0) + { + throw new ArchiveOperationException("No streams to inspect"); + } + if (streams.Count == 1) + { + return await InspectArchiveAsync(streams[0], readerOptions, cancellationToken) + .ConfigureAwait(false); + } + + var options = readerOptions ?? ReaderOptions.ForExternalStream; + var startPositions = streams.Select(stream => stream.Position).ToArray(); + var physicalSize = GetPhysicalSize(streams, startPositions); + + try + { + using var firstArchiveStream = new ArchiveOffsetStream(streams[0]); + var inspectionOptions = options with { LeaveStreamOpen = true }; + var detection = await TryDetectArchiveAsync( + firstArchiveStream, + inspectionOptions, + cancellationToken + ) + .ConfigureAwait(false); + if (detection is null) + { + return null; + } + if ((detection.SupportedApis & ArchiveAccessMode.Archive) == 0) + { + throw new NotSupportedException( + "Inspecting multiple source streams is supported only for formats with an Archive API." + ); + } + + var archiveStreams = new List { firstArchiveStream }; + archiveStreams.AddRange( + streams.Skip(1).Select(stream => new ArchiveOffsetStream(stream)) + ); + await using var archive = await OpenAsyncArchive( + archiveStreams, + inspectionOptions, + cancellationToken + ) + .ConfigureAwait(false); + return await InspectOpenedArchiveAsync( + archive, + detection, + physicalSize, + streams.Count, + cancellationToken + ) + .ConfigureAwait(false); + } + catch (CryptographicException) when (string.IsNullOrEmpty(options.Password)) + { + var detection = await RedetectArchiveAsync( + streams[0], + startPositions[0], + options, + cancellationToken + ) + .ConfigureAwait(false); + return detection is null + ? null + : CreatePartialInformation( + detection, + physicalSize, + streams.Count, + ArchiveInformationLimitations.EncryptedHeaders + ); + } + finally + { + for (var i = 0; i < streams.Count; i++) + { + streams[i].Seek(startPositions[i], SeekOrigin.Begin); + } + } + } + + private static async ValueTask InspectOpenedArchiveAsync( + IAsyncArchive archive, + ArchiveDetection detection, + long? physicalSize, + int physicalPartCount, + CancellationToken cancellationToken + ) + { + var entries = new List(); + await foreach ( + var entry in archive + .EntriesAsync.WithCancellation(cancellationToken) + .ConfigureAwait(false) + ) + { + cancellationToken.ThrowIfCancellationRequested(); + entries.Add(entry); + } + + var volumes = new List(); + await foreach ( + var volume in archive + .VolumesAsync.WithCancellation(cancellationToken) + .ConfigureAwait(false) + ) + { + cancellationToken.ThrowIfCancellationRequested(); + volumes.Add(volume); + } + + var entryArray = entries.ToArray(); + var volumeArray = volumes.ToArray(); + var zip = GetZipInformation(entryArray); + var (isSolid, solidStreamCount) = await GetSolidInformationAsync(archive, entryArray) + .ConfigureAwait(false); + var isComplete = await archive.IsCompleteAsync().ConfigureAwait(false); + var limitations = isComplete + ? ArchiveInformationLimitations.None + : ArchiveInformationLimitations.MissingVolumes; + if (archive.Type == ArchiveType.GZip) + { + limitations |= ArchiveInformationLimitations.UnavailableMetadata; + } + var hasEncryptedHeaders = volumeArray + .OfType() + .Any(volume => volume.IsHeaderEncrypted); + var hasEncryptedEntries = entryArray.Any(entry => entry.IsEncrypted); + + return new ArchiveInformation( + detection, + GetStatus(limitations), + limitations, + GetFormatVersion(volumeArray), + entryArray.LongLength, + zip?.DataDescriptorEntryCount ?? 0, + physicalSize, + isComplete ? GetCompressedPayloadSize(archive, entryArray) : null, + isComplete && archive.Type != ArchiveType.GZip + ? entryArray.Aggregate(0L, (total, entry) => total + entry.Size) + : null, + isSolid, + solidStreamCount, + GetEncryptionScope(hasEncryptedHeaders, hasEncryptedEntries), + hasEncryptedHeaders || hasEncryptedEntries, + GetIsMultiVolume(archive.Type, volumeArray), + physicalPartCount, + volumeArray.Length, + isComplete, + GetArchiveComment(volumeArray), + zip + ); + } + + private static async ValueTask InspectOpenedReaderAsync( + IAsyncReader reader, + ArchiveDetection detection, + long? physicalSize, + int physicalPartCount, + AceMainHeader? aceHeader, + CancellationToken cancellationToken + ) + { + var inspection = new ReaderInspection(detection); + while (await reader.MoveToNextEntryAsync(cancellationToken).ConfigureAwait(false)) + { + cancellationToken.ThrowIfCancellationRequested(); + inspection.Add(reader.Entry); + } + + var isSolid = aceHeader?.IsSolid ?? false; + var isMultiVolume = aceHeader?.IsMultiVolume ?? false; + var formatVersion = aceHeader is null ? null : $"ACE {aceHeader.AceVersion / 10.0:0.0}"; + return inspection.CreateInformation( + physicalSize, + physicalPartCount, + isSolid, + isSolid && inspection.EntryCount > 1 ? 1 : 0, + isMultiVolume, + formatVersion + ); + } + + private static async ValueTask ReadAceHeaderAsync( + Stream stream, + ArchiveDetection detection, + ReaderOptions options, + CancellationToken cancellationToken + ) + { + if (detection.ContainerType != ArchiveType.Ace) + { + return null; + } + + try + { + stream.Position = 0; + return await new AceMainHeader(options.ArchiveEncoding) + .ReadAsync(stream, cancellationToken) + .ConfigureAwait(false) as AceMainHeader; + } + finally + { + stream.Position = 0; + } + } + + private static async ValueTask<(bool IsSolid, long SolidStreamCount)> GetSolidInformationAsync( + IAsyncArchive archive, + IReadOnlyCollection entries + ) + { + if (archive.Type == ArchiveType.SevenZip) + { + var solidStreamCount = entries + .OfType() + .Where(entry => !entry.IsDirectory && entry.FilePart.Folder is not null) + .GroupBy(entry => entry.FilePart.Folder) + .LongCount(group => group.Skip(1).Any()); + return (solidStreamCount > 0, solidStreamCount); + } + + if (archive.Type == ArchiveType.Rar) + { + return ( + await archive.IsSolidAsync().ConfigureAwait(false), + CountRarSolidStreams(entries) + ); + } + + return (false, 0); + } + + private static long? GetCompressedPayloadSize( + IAsyncArchive archive, + IReadOnlyCollection entries + ) => + archive.Type switch + { + ArchiveType.GZip => null, + ArchiveType.SevenZip when archive is SevenZipArchive sevenZipArchive => + sevenZipArchive.TotalSize, + _ => entries.Aggregate(0L, (total, entry) => total + entry.CompressedSize), + }; + + private static async ValueTask RedetectArchiveAsync( + Stream stream, + long startPosition, + ReaderOptions options, + CancellationToken cancellationToken + ) + { + stream.Seek(startPosition, SeekOrigin.Begin); + using var archiveStream = new ArchiveOffsetStream(stream); + return await TryDetectArchiveAsync(archiveStream, options, cancellationToken) + .ConfigureAwait(false); + } +} diff --git a/src/SharpCompress/Archives/ArchiveFactory.Information.cs b/src/SharpCompress/Archives/ArchiveFactory.Information.cs new file mode 100644 index 00000000..1179a41a --- /dev/null +++ b/src/SharpCompress/Archives/ArchiveFactory.Information.cs @@ -0,0 +1,682 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using SharpCompress.Archives.Rar; +using SharpCompress.Archives.SevenZip; +using SharpCompress.Archives.Zip; +using SharpCompress.Common; +using SharpCompress.Common.Rar; +using SharpCompress.Common.Zip; +using SharpCompress.Common.Zip.Headers; +using SharpCompress.IO; +using SharpCompress.Readers; +using AceMainHeader = SharpCompress.Common.Ace.Headers.AceMainHeader; + +namespace SharpCompress.Archives; + +public static partial class ArchiveFactory +{ + /// + /// Collects metadata for the archive at the given file path. + /// + /// Path to the archive file. + /// Archive metadata, or when the file is not a supported archive. + public static ArchiveInformation? InspectArchive(string filePath) => + InspectArchive(filePath, ReaderOptions.ForFilePath); + + /// + /// Collects metadata for the archive at the given file path. + /// + /// Path to the archive file. + /// Options controlling archive inspection. + /// Archive metadata, or when the file is not a supported archive. + public static ArchiveInformation? InspectArchive(string filePath, ReaderOptions? readerOptions) + { + filePath.NotNullOrEmpty(nameof(filePath)); + var options = readerOptions ?? ReaderOptions.ForFilePath; + var detection = DetectArchive(filePath, options); + if (detection is null) + { + return null; + } + if ((detection.SupportedApis & ArchiveAccessMode.Archive) != 0) + { + var fileInfos = GetArchiveFileParts(new FileInfo(filePath), options); + if (fileInfos.Length > 1) + { + return InspectArchive(fileInfos, options); + } + } + + using Stream stream = File.OpenRead(filePath); + return InspectArchive(stream, options); + } + + /// + /// Collects metadata for the archive in the given stream. + /// + /// A readable and seekable stream positioned at the start of the archive. + /// Archive metadata, or when the stream is not a supported archive. + public static ArchiveInformation? InspectArchive(Stream stream) => + InspectArchive(stream, ReaderOptions.ForExternalStream); + + /// + /// Collects metadata for the archive in the given stream. + /// + /// A readable and seekable stream positioned at the start of the archive. + /// Options controlling archive inspection. + /// Archive metadata, or when the stream is not a supported archive. + /// The supplied stream remains open and is restored to its original position. + public static ArchiveInformation? InspectArchive(Stream stream, ReaderOptions? readerOptions) + { + stream.RequireReadable(); + stream.RequireSeekable(); + + var options = readerOptions ?? ReaderOptions.ForExternalStream; + var startPosition = stream.Position; + var physicalSize = GetPhysicalSize(stream, startPosition); + + try + { + using var archiveStream = new ArchiveOffsetStream(stream); + var inspectionOptions = options with { LeaveStreamOpen = true }; + var detection = TryDetectArchive(archiveStream, inspectionOptions); + if (detection is null) + { + return null; + } + + if ((detection.SupportedApis & ArchiveAccessMode.Archive) != 0) + { + using var archive = OpenArchive(archiveStream, inspectionOptions); + return InspectOpenedArchive(archive, detection, physicalSize, 1); + } + + var aceHeader = ReadAceHeader(archiveStream, detection, inspectionOptions); + using var reader = ReaderFactory.OpenReader(archiveStream, inspectionOptions); + return InspectOpenedReader(reader, detection, physicalSize, 1, aceHeader); + } + catch (CryptographicException) when (string.IsNullOrEmpty(options.Password)) + { + var detection = RedetectArchive(stream, startPosition, options); + return detection is null + ? null + : CreatePartialInformation( + detection, + physicalSize, + 1, + ArchiveInformationLimitations.EncryptedHeaders + ); + } + catch (MultipartStreamRequiredException) + { + var detection = RedetectArchive(stream, startPosition, options); + return detection is null + ? null + : CreatePartialInformation( + detection, + physicalSize, + 1, + ArchiveInformationLimitations.MissingVolumes + ); + } + finally + { + stream.Seek(startPosition, SeekOrigin.Begin); + } + } + + /// + /// Collects metadata for an archive opened from multiple files. + /// + /// Archive source files in archive order. + /// Options controlling archive inspection. + /// Archive metadata, or when the files are not a supported archive. + public static ArchiveInformation? InspectArchive( + IReadOnlyList fileInfos, + ReaderOptions? readerOptions = null + ) + { + fileInfos.NotNull(nameof(fileInfos)); + if (fileInfos.Count == 0) + { + throw new ArchiveOperationException("No files to inspect"); + } + if (fileInfos.Count == 1) + { + return InspectArchive(fileInfos[0].FullName, readerOptions); + } + + var options = readerOptions ?? ReaderOptions.ForFilePath; + var detection = DetectArchive(fileInfos[0].FullName, options); + if (detection is null) + { + return null; + } + if ((detection.SupportedApis & ArchiveAccessMode.Archive) == 0) + { + throw new NotSupportedException( + "Inspecting multiple source files is supported only for formats with an Archive API." + ); + } + + var physicalSize = GetPhysicalSize(fileInfos); + try + { + using var archive = OpenArchive(fileInfos, options); + return InspectOpenedArchive(archive, detection, physicalSize, fileInfos.Count); + } + catch (CryptographicException) when (string.IsNullOrEmpty(options.Password)) + { + return CreatePartialInformation( + detection, + physicalSize, + fileInfos.Count, + ArchiveInformationLimitations.EncryptedHeaders + ); + } + } + + /// + /// Collects metadata for an archive opened from multiple streams. + /// + /// Archive source streams in archive order. + /// Options controlling archive inspection. + /// Archive metadata, or when the streams are not a supported archive. + public static ArchiveInformation? InspectArchive( + IReadOnlyList streams, + ReaderOptions? readerOptions = null + ) + { + streams.NotNull(nameof(streams)); + if (streams.Count == 0) + { + throw new ArchiveOperationException("No streams to inspect"); + } + if (streams.Count == 1) + { + return InspectArchive(streams[0], readerOptions); + } + + var options = readerOptions ?? ReaderOptions.ForExternalStream; + var startPositions = streams.Select(stream => stream.Position).ToArray(); + var physicalSize = GetPhysicalSize(streams, startPositions); + + try + { + using var firstArchiveStream = new ArchiveOffsetStream(streams[0]); + var inspectionOptions = options with { LeaveStreamOpen = true }; + var detection = TryDetectArchive(firstArchiveStream, inspectionOptions); + if (detection is null) + { + return null; + } + if ((detection.SupportedApis & ArchiveAccessMode.Archive) == 0) + { + throw new NotSupportedException( + "Inspecting multiple source streams is supported only for formats with an Archive API." + ); + } + + var archiveStreams = new List { firstArchiveStream }; + archiveStreams.AddRange( + streams.Skip(1).Select(stream => new ArchiveOffsetStream(stream)) + ); + using var archive = OpenArchive(archiveStreams, inspectionOptions); + return InspectOpenedArchive(archive, detection, physicalSize, streams.Count); + } + catch (CryptographicException) when (string.IsNullOrEmpty(options.Password)) + { + var detection = RedetectArchive(streams[0], startPositions[0], options); + return detection is null + ? null + : CreatePartialInformation( + detection, + physicalSize, + streams.Count, + ArchiveInformationLimitations.EncryptedHeaders + ); + } + finally + { + for (var i = 0; i < streams.Count; i++) + { + streams[i].Seek(startPositions[i], SeekOrigin.Begin); + } + } + } + + private static ArchiveInformation InspectOpenedArchive( + IArchive archive, + ArchiveDetection detection, + long? physicalSize, + int physicalPartCount + ) + { + var entries = archive.Entries.ToArray(); + var volumes = archive.Volumes.ToArray(); + var zip = GetZipInformation(entries); + var (isSolid, solidStreamCount) = GetSolidInformation(archive, entries); + var isMultiVolume = GetIsMultiVolume(archive.Type, volumes); + var comment = GetArchiveComment(volumes); + var hasEncryptedHeaders = volumes + .OfType() + .Any(volume => volume.IsHeaderEncrypted); + var hasEncryptedEntries = entries.Any(entry => entry.IsEncrypted); + var isComplete = archive.IsComplete; + var limitations = isComplete + ? ArchiveInformationLimitations.None + : ArchiveInformationLimitations.MissingVolumes; + if (archive.Type == ArchiveType.GZip) + { + limitations |= ArchiveInformationLimitations.UnavailableMetadata; + } + + return new ArchiveInformation( + detection, + GetStatus(limitations), + limitations, + GetFormatVersion(volumes), + entries.LongLength, + zip?.DataDescriptorEntryCount ?? 0, + physicalSize, + isComplete ? GetCompressedPayloadSize(archive, entries) : null, + isComplete && archive.Type != ArchiveType.GZip + ? entries.Aggregate(0L, (total, entry) => total + entry.Size) + : null, + isSolid, + solidStreamCount, + GetEncryptionScope(hasEncryptedHeaders, hasEncryptedEntries), + hasEncryptedHeaders || hasEncryptedEntries, + isMultiVolume, + physicalPartCount, + volumes.Length, + isComplete, + comment, + zip + ); + } + + private static ArchiveInformation InspectOpenedReader( + IReader reader, + ArchiveDetection detection, + long? physicalSize, + int physicalPartCount, + AceMainHeader? aceHeader + ) + { + var inspection = new ReaderInspection(detection); + while (reader.MoveToNextEntry()) + { + inspection.Add(reader.Entry); + } + + var isSolid = aceHeader?.IsSolid ?? false; + var isMultiVolume = aceHeader?.IsMultiVolume ?? false; + var formatVersion = aceHeader is null ? null : $"ACE {aceHeader.AceVersion / 10.0:0.0}"; + return inspection.CreateInformation( + physicalSize, + physicalPartCount, + isSolid, + isSolid && inspection.EntryCount > 1 ? 1 : 0, + isMultiVolume, + formatVersion + ); + } + + private static AceMainHeader? ReadAceHeader( + Stream stream, + ArchiveDetection detection, + ReaderOptions options + ) + { + if (detection.ContainerType != ArchiveType.Ace) + { + return null; + } + + try + { + stream.Position = 0; + return new AceMainHeader(options.ArchiveEncoding).Read(stream) as AceMainHeader; + } + finally + { + stream.Position = 0; + } + } + + private static ArchiveInformation CreatePartialInformation( + ArchiveDetection detection, + long? physicalSize, + int physicalPartCount, + ArchiveInformationLimitations limitations + ) => + new( + detection, + ArchiveInformationStatus.Partial, + limitations, + null, + null, + null, + physicalSize, + null, + null, + null, + null, + limitations.HasFlag(ArchiveInformationLimitations.EncryptedHeaders) + ? ArchiveEncryptionScope.Headers + : null, + limitations.HasFlag(ArchiveInformationLimitations.EncryptedHeaders) ? true : null, + null, + physicalPartCount, + null, + null, + null, + null + ); + + private static ArchiveInformationStatus GetStatus(ArchiveInformationLimitations limitations) => + limitations == ArchiveInformationLimitations.None + ? ArchiveInformationStatus.Complete + : ArchiveInformationStatus.Partial; + + private static ZipArchiveInformation? GetZipInformation(IEnumerable entries) + { + var dataDescriptorEntryCount = entries + .OfType() + .LongCount(entry => + entry + .Parts.OfType() + .Any(part => + FlagUtility.HasFlag( + part.Header.Flags, + SharpCompress.Common.Zip.Headers.HeaderFlags.UsePostDataDescriptor + ) + ) + ); + return dataDescriptorEntryCount == 0 && !entries.OfType().Any() + ? null + : new ZipArchiveInformation(dataDescriptorEntryCount); + } + + private static (bool IsSolid, long SolidStreamCount) GetSolidInformation( + IArchive archive, + IReadOnlyCollection entries + ) + { + if (archive.Type == ArchiveType.SevenZip) + { + var solidStreamCount = entries + .OfType() + .Where(entry => !entry.IsDirectory && entry.FilePart.Folder is not null) + .GroupBy(entry => entry.FilePart.Folder) + .LongCount(group => group.Skip(1).Any()); + return (solidStreamCount > 0, solidStreamCount); + } + + if (archive.Type == ArchiveType.Rar) + { + var solidStreamCount = CountRarSolidStreams(entries); + return (archive.IsSolid, solidStreamCount); + } + + return (false, 0); + } + + private static long CountRarSolidStreams(IEnumerable entries) + { + var wasSolid = false; + long count = 0; + foreach (var entry in entries.Where(entry => !entry.IsDirectory)) + { + if (entry.IsSolid && !wasSolid) + { + count++; + } + wasSolid = entry.IsSolid; + } + return count; + } + + private static bool GetIsMultiVolume(ArchiveType type, IVolume[] volumes) => + type == ArchiveType.Rar + ? volumes.OfType().Any(volume => volume.IsMultiVolume) + : volumes.Length > 1; + + private static string? GetArchiveComment(IEnumerable volumes) => + volumes.OfType().LastOrDefault()?.Comment + ?? volumes + .OfType() + .Select(volume => volume.Comment) + .FirstOrDefault(comment => comment is not null); + + private static string? GetFormatVersion(IEnumerable volumes) + { + var rarVolume = volumes.OfType().FirstOrDefault(); + return rarVolume is null ? null : $"RAR {rarVolume.MinVersion}-{rarVolume.MaxVersion}"; + } + + private static ArchiveEncryptionScope GetEncryptionScope( + bool hasEncryptedHeaders, + bool hasEncryptedEntries + ) + { + var encryption = ArchiveEncryptionScope.None; + if (hasEncryptedHeaders) + { + encryption |= ArchiveEncryptionScope.Headers; + } + if (hasEncryptedEntries) + { + encryption |= ArchiveEncryptionScope.EntryData; + } + return encryption; + } + + private static long? GetCompressedPayloadSize( + IArchive archive, + IReadOnlyCollection entries + ) => + archive.Type switch + { + ArchiveType.GZip => null, + ArchiveType.SevenZip when archive is SevenZipArchive sevenZipArchive => + sevenZipArchive.TotalSize, + _ => entries.Aggregate(0L, (total, entry) => total + entry.CompressedSize), + }; + + private static long? GetPhysicalSize(Stream stream, long startPosition) + { + try + { + return stream.Length - startPosition; + } + catch (NotSupportedException) + { + return null; + } + } + + private static ArchiveDetection? RedetectArchive( + Stream stream, + long startPosition, + ReaderOptions options + ) + { + stream.Seek(startPosition, SeekOrigin.Begin); + using var archiveStream = new ArchiveOffsetStream(stream); + return TryDetectArchive(archiveStream, options); + } + + private static FileInfo[] GetArchiveFileParts(FileInfo firstPart, ReaderOptions options) + { + using Stream stream = firstPart.OpenRead(); + var factory = TryFindFactory(stream, options); + if (factory is null) + { + return [firstPart]; + } + + var parts = new List { firstPart }; + for (var index = 1; factory.GetFilePart(index, firstPart) is { } part; index++) + { + parts.Add(part); + } + return parts.ToArray(); + } + + private static long? GetPhysicalSize(IEnumerable fileInfos) + { + try + { + return fileInfos.Aggregate(0L, (total, fileInfo) => total + fileInfo.Length); + } + catch (IOException) + { + return null; + } + } + + private static long? GetPhysicalSize(IReadOnlyList streams, long[] startPositions) + { + try + { + long total = 0; + for (var i = 0; i < streams.Count; i++) + { + total += streams[i].Length - startPositions[i]; + } + return total; + } + catch (NotSupportedException) + { + return null; + } + } + + private sealed class ReaderInspection + { + private readonly ArchiveDetection detection; + private long? compressedPayloadSize = 0; + private long? uncompressedPayloadSize = 0; + private long dataDescriptorEntryCount; + private long entriesWithUnknownSizeCount; + private bool isEncrypted; + private ArchiveInformationLimitations limitations; + + public ReaderInspection(ArchiveDetection detection) + { + this.detection = detection; + if ( + detection.ContainerType == ArchiveType.Lzw + || ( + detection.ContainerType == ArchiveType.Tar + && detection.OuterCompressionType is not null + ) + ) + { + compressedPayloadSize = null; + limitations |= ArchiveInformationLimitations.UnavailableMetadata; + } + if (detection.ContainerType == ArchiveType.Lzw) + { + uncompressedPayloadSize = null; + } + } + + public long EntryCount { get; private set; } + + public void Add(IEntry entry) + { + EntryCount++; + isEncrypted |= entry.IsEncrypted; + + var usesDataDescriptor = UsesZipDataDescriptor(entry); + if (usesDataDescriptor) + { + dataDescriptorEntryCount++; + } + + if ( + detection.ContainerType == ArchiveType.Lzw + || usesDataDescriptor + || !TryGetSize(entry, out var size) + ) + { + entriesWithUnknownSizeCount++; + uncompressedPayloadSize = null; + limitations |= ArchiveInformationLimitations.UnavailableMetadata; + } + else if (uncompressedPayloadSize is { } totalSize) + { + uncompressedPayloadSize = totalSize + size; + } + + if (compressedPayloadSize is { } totalCompressedSize) + { + compressedPayloadSize = totalCompressedSize + entry.CompressedSize; + } + } + + public ArchiveInformation CreateInformation( + long? physicalSize, + int physicalPartCount, + bool isSolid, + long solidStreamCount, + bool isMultiVolume, + string? formatVersion + ) => + new( + detection, + GetStatus(limitations), + limitations, + formatVersion, + EntryCount, + entriesWithUnknownSizeCount, + physicalSize, + compressedPayloadSize, + uncompressedPayloadSize, + isSolid, + solidStreamCount, + isEncrypted ? ArchiveEncryptionScope.EntryData : ArchiveEncryptionScope.None, + isEncrypted, + isMultiVolume, + physicalPartCount, + 1, + true, + null, + detection.ContainerType == ArchiveType.Zip + ? new ZipArchiveInformation(dataDescriptorEntryCount) + : null + ); + + private static bool TryGetSize(IEntry entry, out long size) + { + try + { + size = entry.Size; + return size >= 0; + } + catch (NotImplementedException) + { + size = 0; + return false; + } + } + + private static bool UsesZipDataDescriptor(IEntry entry) => + entry is ZipEntry zipEntry + && zipEntry + .Parts.OfType() + .Any(part => + FlagUtility.HasFlag( + part.Header.Flags, + SharpCompress.Common.Zip.Headers.HeaderFlags.UsePostDataDescriptor + ) + ); + } +} diff --git a/src/SharpCompress/Archives/ArchiveInformation.cs b/src/SharpCompress/Archives/ArchiveInformation.cs index 81d4fc6d..68468771 100644 --- a/src/SharpCompress/Archives/ArchiveInformation.cs +++ b/src/SharpCompress/Archives/ArchiveInformation.cs @@ -1,50 +1,145 @@ -using SharpCompress.Common; - namespace SharpCompress.Archives; /// -/// Contains information about a detected archive, including its type and supported capabilities. +/// Contains metadata collected by fully inspecting an archive. /// -/// -/// Use or -/// -/// to obtain an instance of this record. -/// -public record ArchiveInformation +public sealed class ArchiveInformation { - /// - /// The type of archive detected, or when the format is not a registered well-known type. - /// - public ArchiveType? Type { get; set; } - - /// - /// when this archive format supports random access via the API, - /// meaning the full file listing can be retrieved without decompressing the entire archive. - /// when only the API is available, - /// which reads entries sequentially and can only report per-entry progress. - /// - public bool SupportsRandomAccess { get; set; } - - /// - /// For ZIP archives, the number of entries that use post-data descriptor trailers. - /// This value is for non-ZIP formats. - /// - public int? ZipDataDescriptorEntryCount { get; set; } - - /// - /// For solid-capable archive formats, the number of solid compressed streams present. - /// This value is for formats that do not support solid entries. - /// - public int? SolidStreamCount { get; set; } - - /// - /// Creates a new archive information instance. - /// - /// The detected archive type. - /// Whether the detected format supports random access. - public ArchiveInformation(ArchiveType? type, bool supportsRandomAccess) + internal ArchiveInformation( + ArchiveDetection detection, + ArchiveInformationStatus status, + ArchiveInformationLimitations limitations, + string? formatVersion, + long? entryCount, + long? entriesWithUnknownSizeCount, + long? physicalSize, + long? compressedPayloadSize, + long? uncompressedPayloadSize, + bool? isSolid, + long? solidStreamCount, + ArchiveEncryptionScope? encryption, + bool? isEncrypted, + bool? isMultiVolume, + int? physicalPartCount, + int? logicalVolumeCount, + bool? isComplete, + string? comment, + ZipArchiveInformation? zip + ) { - Type = type; - SupportsRandomAccess = supportsRandomAccess; + Detection = detection; + Status = status; + Limitations = limitations; + FormatVersion = formatVersion; + EntryCount = entryCount; + EntriesWithUnknownSizeCount = entriesWithUnknownSizeCount; + PhysicalSize = physicalSize; + CompressedPayloadSize = compressedPayloadSize; + UncompressedPayloadSize = uncompressedPayloadSize; + IsSolid = isSolid; + SolidStreamCount = solidStreamCount; + Encryption = encryption; + IsEncrypted = isEncrypted; + IsMultiVolume = isMultiVolume; + PhysicalPartCount = physicalPartCount; + LogicalVolumeCount = logicalVolumeCount; + IsComplete = isComplete; + Comment = comment; + Zip = zip; } + + /// + /// Gets the format and API capabilities identified before inspection. + /// + public ArchiveDetection Detection { get; } + + /// + /// Gets whether metadata collection completed. + /// + public ArchiveInformationStatus Status { get; } + + /// + /// Gets conditions that prevented complete metadata collection. + /// + public ArchiveInformationLimitations Limitations { get; } + + /// + /// Gets the format version when exposed by the archive format. + /// + public string? FormatVersion { get; } + + /// + /// Gets the number of entries, excluding format control records. + /// + public long? EntryCount { get; } + + /// + /// Gets the number of entries for which a forward-only reader cannot know the uncompressed size before reading entry data. + /// + public long? EntriesWithUnknownSizeCount { get; } + + /// + /// Gets the number of bytes in the supplied source parts, when known. + /// + public long? PhysicalSize { get; } + + /// + /// Gets the aggregate stored payload size, when the format exposes it reliably. + /// + public long? CompressedPayloadSize { get; } + + /// + /// Gets the aggregate uncompressed entry size, when the format exposes it reliably. + /// + public long? UncompressedPayloadSize { get; } + + /// + /// Gets whether entries share compression state. + /// + public bool? IsSolid { get; } + + /// + /// Gets the number of independent shared compression streams, when known. + /// + public long? SolidStreamCount { get; } + + /// + /// Gets the scopes protected by encryption, when known. + /// + public ArchiveEncryptionScope? Encryption { get; } + + /// + /// Gets whether archive headers or entry data are encrypted, when known. + /// + public bool? IsEncrypted { get; } + + /// + /// Gets whether the archive is part of a multi-volume set, when known. + /// + public bool? IsMultiVolume { get; } + + /// + /// Gets the number of source parts supplied for inspection. + /// + public int? PhysicalPartCount { get; } + + /// + /// Gets the number of logical volumes exposed by the archive, when known. + /// + public int? LogicalVolumeCount { get; } + + /// + /// Gets whether all discovered entries are complete, when known. + /// + public bool? IsComplete { get; } + + /// + /// Gets the archive comment, when supported by the format. + /// + public string? Comment { get; } + + /// + /// Gets ZIP-specific metadata when the detected container is ZIP. + /// + public ZipArchiveInformation? Zip { get; } } diff --git a/src/SharpCompress/Archives/ArchiveInformationLimitations.cs b/src/SharpCompress/Archives/ArchiveInformationLimitations.cs new file mode 100644 index 00000000..26f2cd3a --- /dev/null +++ b/src/SharpCompress/Archives/ArchiveInformationLimitations.cs @@ -0,0 +1,30 @@ +using System; + +namespace SharpCompress.Archives; + +/// +/// Identifies conditions that prevented complete archive metadata inspection. +/// +[Flags] +public enum ArchiveInformationLimitations +{ + /// + /// No known inspection limitations apply. + /// + None = 0, + + /// + /// Archive headers are encrypted and no password was supplied. + /// + EncryptedHeaders = 1, + + /// + /// One or more archive volumes are unavailable. + /// + MissingVolumes = 2, + + /// + /// The format does not expose a requested metadata value. + /// + UnavailableMetadata = 4, +} diff --git a/src/SharpCompress/Archives/ArchiveInformationStatus.cs b/src/SharpCompress/Archives/ArchiveInformationStatus.cs new file mode 100644 index 00000000..dabee58b --- /dev/null +++ b/src/SharpCompress/Archives/ArchiveInformationStatus.cs @@ -0,0 +1,17 @@ +namespace SharpCompress.Archives; + +/// +/// Describes whether archive metadata could be collected completely. +/// +public enum ArchiveInformationStatus +{ + /// + /// All metadata supported by the format was collected. + /// + Complete, + + /// + /// Some metadata could not be collected. See . + /// + Partial, +} diff --git a/src/SharpCompress/Archives/ZipArchiveInformation.cs b/src/SharpCompress/Archives/ZipArchiveInformation.cs new file mode 100644 index 00000000..c7160903 --- /dev/null +++ b/src/SharpCompress/Archives/ZipArchiveInformation.cs @@ -0,0 +1,15 @@ +namespace SharpCompress.Archives; + +/// +/// Contains ZIP-specific metadata. +/// +public sealed class ZipArchiveInformation +{ + internal ZipArchiveInformation(long dataDescriptorEntryCount) => + DataDescriptorEntryCount = dataDescriptorEntryCount; + + /// + /// Gets the number of entries whose local header defers its CRC and sizes to a data descriptor. + /// + public long DataDescriptorEntryCount { get; } +} diff --git a/src/SharpCompress/Common/Rar/RarVolume.cs b/src/SharpCompress/Common/Rar/RarVolume.cs index 1b7de043..f3913d2c 100644 --- a/src/SharpCompress/Common/Rar/RarVolume.cs +++ b/src/SharpCompress/Common/Rar/RarVolume.cs @@ -25,6 +25,8 @@ public abstract class RarVolume : Volume private ArchiveHeader? ArchiveHeader { get; set; } + internal bool IsHeaderEncrypted => _headerFactory.IsEncrypted; + private StreamingMode Mode => _headerFactory.StreamingMode; internal abstract IEnumerable ReadFileParts(); diff --git a/src/SharpCompress/IO/ArchiveOffsetStream.cs b/src/SharpCompress/IO/ArchiveOffsetStream.cs new file mode 100644 index 00000000..9c7bc03e --- /dev/null +++ b/src/SharpCompress/IO/ArchiveOffsetStream.cs @@ -0,0 +1,87 @@ +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace SharpCompress.IO; + +/// +/// Exposes the remainder of a seekable stream as an archive that begins at position zero. +/// +internal sealed class ArchiveOffsetStream : Stream +{ + private readonly Stream stream; + private readonly long origin; + + public ArchiveOffsetStream(Stream stream) + { + this.stream = stream; + origin = stream.Position; + } + + public override bool CanRead => stream.CanRead; + public override bool CanSeek => stream.CanSeek; + public override bool CanWrite => false; + public override long Length => stream.Length - origin; + + public override long Position + { + get => stream.Position - origin; + set => stream.Position = origin + value; + } + + public override void Flush() { } + + public override int Read(byte[] buffer, int offset, int count) => + stream.Read(buffer, offset, count); + +#if !LEGACY_DOTNET + public override int Read(Span buffer) => stream.Read(buffer); +#endif + + public override int ReadByte() => stream.ReadByte(); + + public override Task ReadAsync( + byte[] buffer, + int offset, + int count, + CancellationToken cancellationToken + ) => stream.ReadAsync(buffer, offset, count, cancellationToken); + +#if !NET48 && !NETSTANDARD2_0 + public override ValueTask ReadAsync( + Memory buffer, + CancellationToken cancellationToken = default + ) => stream.ReadAsync(buffer, cancellationToken); +#endif + + public override long Seek(long offset, SeekOrigin seekOrigin) + { + var position = seekOrigin switch + { + SeekOrigin.Begin => offset, + SeekOrigin.Current => Position + offset, + SeekOrigin.End => Length + offset, + _ => throw new ArgumentOutOfRangeException(nameof(seekOrigin)), + }; + + if (position < 0) + { + throw new IOException("Attempted to seek before the archive origin."); + } + + stream.Seek(origin + position, SeekOrigin.Begin); + return position; + } + + public override void SetLength(long value) => throw new NotSupportedException(); + + public override void Write(byte[] buffer, int offset, int count) => + throw new NotSupportedException(); + + protected override void Dispose(bool disposing) + { + // The caller owns the source stream. + base.Dispose(disposing); + } +} diff --git a/tests/SharpCompress.Test/ArchiveFactoryTests.cs b/tests/SharpCompress.Test/ArchiveFactoryTests.cs index 2c8aa42c..5ea9d3c0 100644 --- a/tests/SharpCompress.Test/ArchiveFactoryTests.cs +++ b/tests/SharpCompress.Test/ArchiveFactoryTests.cs @@ -247,38 +247,36 @@ public class ArchiveFactoryTests : TestBase [InlineData("7Zip.nonsolid.7z", ArchiveType.SevenZip, true)] [InlineData("Ace.store.ace", ArchiveType.Ace, false)] [InlineData("Arc.uncompressed.arc", ArchiveType.Arc, false)] - public void GetArchiveInformation_ReturnsExpectedInfo( + public void DetectArchive_ReturnsExpectedInfo( string archiveName, ArchiveType expectedType, bool expectedRandomAccess ) { - var info = ArchiveFactory.GetArchiveInformation( - Path.Combine(TEST_ARCHIVES_PATH, archiveName) - ); + var info = ArchiveFactory.DetectArchive(Path.Combine(TEST_ARCHIVES_PATH, archiveName)); Assert.NotNull(info); - Assert.Equal(expectedType, info.Type); - Assert.Equal(expectedRandomAccess, info.SupportsRandomAccess); + Assert.Equal(expectedType, info.ContainerType); + Assert.Equal(expectedRandomAccess, info.SupportedApis.HasFlag(ArchiveAccessMode.Archive)); } [Theory] [InlineData("7Zip.LZMA2.exe", ArchiveType.SevenZip, true)] [InlineData("Rar.jpeg.jpg", ArchiveType.Rar, true)] - public void GetArchiveInformation_WithReaderOptions_ReturnsExpectedInfo( + public void DetectArchive_WithReaderOptions_ReturnsExpectedInfo( string archiveName, ArchiveType expectedType, bool expectedRandomAccess ) { - var info = ArchiveFactory.GetArchiveInformation( + var info = ArchiveFactory.DetectArchive( GetTestArchivePath(archiveName), ReaderOptions.ForFilePath.WithLookForHeader(true) ); Assert.NotNull(info); - Assert.Equal(expectedType, info.Type); - Assert.Equal(expectedRandomAccess, info.SupportsRandomAccess); + Assert.Equal(expectedType, info.ContainerType); + Assert.Equal(expectedRandomAccess, info.SupportedApis.HasFlag(ArchiveAccessMode.Archive)); } [Theory] @@ -288,38 +286,38 @@ public class ArchiveFactoryTests : TestBase [InlineData("7Zip.nonsolid.7z", ArchiveType.SevenZip, true)] [InlineData("Ace.store.ace", ArchiveType.Ace, false)] [InlineData("Arc.uncompressed.arc", ArchiveType.Arc, false)] - public async ValueTask GetArchiveInformationAsync_ReturnsExpectedInfo( + public async ValueTask DetectArchiveAsync_ReturnsExpectedInfo( string archiveName, ArchiveType expectedType, bool expectedRandomAccess ) { - var info = await ArchiveFactory.GetArchiveInformationAsync( + var info = await ArchiveFactory.DetectArchiveAsync( Path.Combine(TEST_ARCHIVES_PATH, archiveName) ); Assert.NotNull(info); - Assert.Equal(expectedType, info.Type); - Assert.Equal(expectedRandomAccess, info.SupportsRandomAccess); + Assert.Equal(expectedType, info.ContainerType); + Assert.Equal(expectedRandomAccess, info.SupportedApis.HasFlag(ArchiveAccessMode.Archive)); } [Theory] [InlineData("7Zip.LZMA2.exe", ArchiveType.SevenZip, true)] [InlineData("Rar.jpeg.jpg", ArchiveType.Rar, true)] - public async ValueTask GetArchiveInformationAsync_WithReaderOptions_ReturnsExpectedInfo( + public async ValueTask DetectArchiveAsync_WithReaderOptions_ReturnsExpectedInfo( string archiveName, ArchiveType expectedType, bool expectedRandomAccess ) { - var info = await ArchiveFactory.GetArchiveInformationAsync( + var info = await ArchiveFactory.DetectArchiveAsync( GetTestArchivePath(archiveName), ReaderOptions.ForFilePath.WithLookForHeader(true) ); Assert.NotNull(info); - Assert.Equal(expectedType, info.Type); - Assert.Equal(expectedRandomAccess, info.SupportsRandomAccess); + Assert.Equal(expectedType, info.ContainerType); + Assert.Equal(expectedRandomAccess, info.SupportedApis.HasFlag(ArchiveAccessMode.Archive)); } [Theory] @@ -466,17 +464,17 @@ public class ArchiveFactoryTests : TestBase [InlineData("SharpCompress.AES.zip", ArchiveType.Zip, true)] [InlineData("SharpCompress.Encrypted.zip", ArchiveType.Zip, true)] [InlineData("SharpCompress.Encrypted2.zip", ArchiveType.Zip, true)] - public void GetArchiveInformation_DetectsSingleFileTestArchives( + public void DetectArchive_DetectsSingleFileTestArchives( string archiveName, ArchiveType expectedType, bool expectedSeekable ) { - var info = ArchiveFactory.GetArchiveInformation(GetTestArchivePath(archiveName)); + var info = ArchiveFactory.DetectArchive(GetTestArchivePath(archiveName)); Assert.NotNull(info); - Assert.Equal(expectedType, info.Type); - Assert.Equal(expectedSeekable, info.SupportsRandomAccess); + Assert.Equal(expectedType, info.ContainerType); + Assert.Equal(expectedSeekable, info.SupportedApis.HasFlag(ArchiveAccessMode.Archive)); } [Theory] @@ -623,35 +621,35 @@ public class ArchiveFactoryTests : TestBase [InlineData("SharpCompress.AES.zip", ArchiveType.Zip, true)] [InlineData("SharpCompress.Encrypted.zip", ArchiveType.Zip, true)] [InlineData("SharpCompress.Encrypted2.zip", ArchiveType.Zip, true)] - public async ValueTask GetArchiveInformationAsync_DetectsSingleFileTestArchives( + public async ValueTask DetectArchiveAsync_DetectsSingleFileTestArchives( string archiveName, ArchiveType expectedType, bool expectedSeekable ) { - var info = await ArchiveFactory.GetArchiveInformationAsync(GetTestArchivePath(archiveName)); + var info = await ArchiveFactory.DetectArchiveAsync(GetTestArchivePath(archiveName)); Assert.NotNull(info); - Assert.Equal(expectedType, info.Type); - Assert.Equal(expectedSeekable, info.SupportsRandomAccess); + Assert.Equal(expectedType, info.ContainerType); + Assert.Equal(expectedSeekable, info.SupportedApis.HasFlag(ArchiveAccessMode.Archive)); } [Fact] - public void GetArchiveInformation_ReturnsNull_ForNonArchive() + public void DetectArchive_ReturnsNull_ForNonArchive() { using var stream = new MemoryStream(Encoding.ASCII.GetBytes("not an archive")); - var info = ArchiveFactory.GetArchiveInformation(stream); + var info = ArchiveFactory.DetectArchive(stream); Assert.Null(info); } [Fact] - public async ValueTask GetArchiveInformationAsync_ReturnsNull_ForNonArchive() + public async ValueTask DetectArchiveAsync_ReturnsNull_ForNonArchive() { using var stream = new MemoryStream(Encoding.ASCII.GetBytes("not an archive")); - var info = await ArchiveFactory.GetArchiveInformationAsync(stream); + var info = await ArchiveFactory.DetectArchiveAsync(stream); Assert.Null(info); } @@ -697,13 +695,16 @@ public class ArchiveFactoryTests : TestBase [InlineData("Tar.tar.xz")] [InlineData("Tar.tar.zst")] [InlineData("Tar.tar.Z")] - public void GetArchiveInformation_ReturnsNull_ForCompressedTar(string archiveName) + public void DetectArchive_RecognizesCompressedTar(string archiveName) { using var stream = File.OpenRead(GetTestArchivePath(archiveName)); - var info = ArchiveFactory.GetArchiveInformation(stream); + var info = ArchiveFactory.DetectArchive(stream); - Assert.Null(info); + Assert.NotNull(info); + Assert.Equal(ArchiveType.Tar, info.ContainerType); + Assert.NotNull(info.OuterCompressionType); + Assert.Equal(ArchiveAccessMode.Reader, info.SupportedApis); } [Theory] @@ -713,21 +714,37 @@ public class ArchiveFactoryTests : TestBase [InlineData("Tar.tar.xz")] [InlineData("Tar.tar.zst")] [InlineData("Tar.tar.Z")] - public async ValueTask GetArchiveInformationAsync_ReturnsNull_ForCompressedTar( - string archiveName - ) + public async ValueTask DetectArchiveAsync_RecognizesCompressedTar(string archiveName) { using var stream = File.OpenRead(GetTestArchivePath(archiveName)); - var info = await ArchiveFactory.GetArchiveInformationAsync(stream); + var info = await ArchiveFactory.DetectArchiveAsync(stream); - Assert.Null(info); + Assert.NotNull(info); + Assert.Equal(ArchiveType.Tar, info.ContainerType); + Assert.NotNull(info.OuterCompressionType); + Assert.Equal(ArchiveAccessMode.Reader, info.SupportedApis); } [Theory] [InlineData("Zip.deflate.zip", ArchiveType.Zip)] [InlineData("Tar.noEmptyDirs.tar", ArchiveType.Tar)] - public void GetArchiveInformation_Stream_PreservesPosition( + public void DetectArchive_Stream_PreservesPosition(string archiveName, ArchiveType expectedType) + { + using var stream = CreatePrefixedArchiveStream(archiveName, 13); + var startPosition = stream.Position; + + var info = ArchiveFactory.DetectArchive(stream); + + Assert.NotNull(info); + Assert.Equal(expectedType, info.ContainerType); + Assert.Equal(startPosition, stream.Position); + } + + [Theory] + [InlineData("Zip.deflate.zip", ArchiveType.Zip)] + [InlineData("Tar.noEmptyDirs.tar", ArchiveType.Tar)] + public async ValueTask DetectArchiveAsync_Stream_PreservesPosition( string archiveName, ArchiveType expectedType ) @@ -735,17 +752,76 @@ public class ArchiveFactoryTests : TestBase using var stream = CreatePrefixedArchiveStream(archiveName, 13); var startPosition = stream.Position; - var info = ArchiveFactory.GetArchiveInformation(stream); + var info = await ArchiveFactory.DetectArchiveAsync(stream); Assert.NotNull(info); - Assert.Equal(expectedType, info.Type); + Assert.Equal(expectedType, info.ContainerType); Assert.Equal(startPosition, stream.Position); } + [Theory] + [InlineData("Zip.none.datadescriptors.zip", 2)] + [InlineData("Zip.deflate.zip", 0)] + public void InspectArchive_ZipDataDescriptorEntryCount( + string archiveName, + long expectedDataDescriptorEntryCount + ) + { + using var stream = File.OpenRead(GetTestArchivePath(archiveName)); + + var info = ArchiveFactory.InspectArchive(stream); + + Assert.NotNull(info); + Assert.Equal(ArchiveType.Zip, info.Detection.ContainerType); + Assert.NotNull(info.Zip); + Assert.Equal(expectedDataDescriptorEntryCount, info.Zip.DataDescriptorEntryCount); + Assert.Equal(expectedDataDescriptorEntryCount, info.EntriesWithUnknownSizeCount); + } + + [Theory] + [InlineData("7Zip.solid.7z", true, 1)] + [InlineData("7Zip.nonsolid.7z", false, 0)] + [InlineData("Rar.rar", false, 0)] + [InlineData("Rar.solid.rar", true, 1)] + public void InspectArchive_SolidStreamCount( + string archiveName, + bool expectedSolid, + long expectedSolidStreamCount + ) + { + using var stream = File.OpenRead(GetTestArchivePath(archiveName)); + + var info = ArchiveFactory.InspectArchive(stream); + + Assert.NotNull(info); + Assert.Equal(expectedSolid, info.IsSolid); + Assert.Equal(expectedSolidStreamCount, info.SolidStreamCount); + } + + [Theory] + [InlineData("Zip.none.datadescriptors.zip", ArchiveType.Zip)] + [InlineData("7Zip.solid.7z", ArchiveType.SevenZip)] + public async ValueTask InspectArchiveAsync_ReturnsMetadata( + string archiveName, + ArchiveType expectedType + ) + { + using var stream = File.OpenRead(GetTestArchivePath(archiveName)); + + var info = await ArchiveFactory.InspectArchiveAsync(stream); + + Assert.NotNull(info); + Assert.Equal(expectedType, info.Detection.ContainerType); + Assert.Equal(ArchiveInformationStatus.Complete, info.Status); + Assert.NotNull(info.EntryCount); + } + [Theory] [InlineData("Zip.deflate.zip", ArchiveType.Zip)] [InlineData("Tar.noEmptyDirs.tar", ArchiveType.Tar)] - public async ValueTask GetArchiveInformationAsync_Stream_PreservesPosition( + [InlineData("Rar.rar", ArchiveType.Rar)] + [InlineData("7Zip.nonsolid.7z", ArchiveType.SevenZip)] + public void InspectArchive_Stream_PreservesPosition( string archiveName, ArchiveType expectedType ) @@ -753,62 +829,172 @@ public class ArchiveFactoryTests : TestBase using var stream = CreatePrefixedArchiveStream(archiveName, 13); var startPosition = stream.Position; - var info = await ArchiveFactory.GetArchiveInformationAsync(stream); + var info = ArchiveFactory.InspectArchive(stream); Assert.NotNull(info); - Assert.Equal(expectedType, info.Type); + Assert.Equal(expectedType, info.Detection.ContainerType); Assert.Equal(startPosition, stream.Position); } [Theory] - [InlineData("Zip.none.datadescriptors.zip", true)] - [InlineData("Zip.deflate.zip", false)] - public void GetArchiveInformation_ZipDataDescriptorEntryCount( + [InlineData("Zip.deflate.zip", ArchiveType.Zip)] + [InlineData("Tar.noEmptyDirs.tar", ArchiveType.Tar)] + [InlineData("Rar.rar", ArchiveType.Rar)] + [InlineData("7Zip.nonsolid.7z", ArchiveType.SevenZip)] + public async ValueTask InspectArchiveAsync_Stream_PreservesPosition( string archiveName, - bool expectsDataDescriptorTrailers + ArchiveType expectedType ) { - using var stream = File.OpenRead(GetTestArchivePath(archiveName)); + using var stream = CreatePrefixedArchiveStream(archiveName, 13); + var startPosition = stream.Position; - var info = ArchiveFactory.GetArchiveInformation(stream); + var info = await ArchiveFactory.InspectArchiveAsync(stream); Assert.NotNull(info); - Assert.Equal(ArchiveType.Zip, info.Type); - Assert.NotNull(info.ZipDataDescriptorEntryCount); - Assert.Equal(expectsDataDescriptorTrailers, info.ZipDataDescriptorEntryCount > 0); + Assert.Equal(expectedType, info.Detection.ContainerType); + Assert.Equal(startPosition, stream.Position); + } + + [Fact] + public void InspectArchive_CompressedTar_ReportsContainerAndWrapper() + { + var info = ArchiveFactory.InspectArchive(GetTestArchivePath("Tar.tar.xz")); + + Assert.NotNull(info); + Assert.Equal(ArchiveType.Tar, info.Detection.ContainerType); + Assert.Equal(CompressionType.Xz, info.Detection.OuterCompressionType); + Assert.Equal(ArchiveInformationStatus.Partial, info.Status); + Assert.Null(info.CompressedPayloadSize); + Assert.NotNull(info.EntryCount); } [Theory] - [InlineData("7Zip.solid.7z", true)] - [InlineData("7Zip.nonsolid.7z", false)] - [InlineData("Rar.rar", false)] - public void GetArchiveInformation_SolidStreamCount(string archiveName, bool expectsSolidStreams) + [InlineData("Ace.method1-solid.ace", true)] + [InlineData("Ace.method1.ace", false)] + public void InspectArchive_Ace_ReportsSolidMetadata(string archiveName, bool expectedSolid) { - using var stream = File.OpenRead(GetTestArchivePath(archiveName)); - - var info = ArchiveFactory.GetArchiveInformation(stream); + var info = ArchiveFactory.InspectArchive(GetTestArchivePath(archiveName)); Assert.NotNull(info); - Assert.NotNull(info.SolidStreamCount); - Assert.Equal(expectsSolidStreams, info.SolidStreamCount > 0); + Assert.Equal(expectedSolid, info.IsSolid); + Assert.Equal(expectedSolid ? 1 : 0, info.SolidStreamCount); + } + + [Fact] + public void InspectArchive_Arc_ReportsUnavailableUncompressedSizes() + { + var info = ArchiveFactory.InspectArchive(GetTestArchivePath("Arc.uncompressed.arc")); + + Assert.NotNull(info); + Assert.Equal(ArchiveInformationStatus.Partial, info.Status); + Assert.True(info.Limitations.HasFlag(ArchiveInformationLimitations.UnavailableMetadata)); + Assert.Null(info.UncompressedPayloadSize); + Assert.True(info.EntriesWithUnknownSizeCount > 0); } [Theory] - [InlineData("Zip.none.datadescriptors.zip", true)] - [InlineData("7Zip.solid.7z", true)] - public async ValueTask GetArchiveInformationAsync_IncludesFormatSpecificDetails( + [InlineData("Arj.store.arj", ArchiveType.Arj, ArchiveInformationStatus.Complete)] + [InlineData("large_test.txt.Z", ArchiveType.Lzw, ArchiveInformationStatus.Partial)] + public void InspectArchive_ReaderOnlyFormats_ReturnsMetadata( string archiveName, - bool expectsAnyFormatSpecificDetails + ArchiveType expectedType, + ArchiveInformationStatus expectedStatus ) { - using var stream = File.OpenRead(GetTestArchivePath(archiveName)); - - var info = await ArchiveFactory.GetArchiveInformationAsync(stream); + var info = ArchiveFactory.InspectArchive(GetTestArchivePath(archiveName)); Assert.NotNull(info); - var hasZipDetail = info.ZipDataDescriptorEntryCount > 0; - var hasSolidDetail = info.SolidStreamCount > 0; - Assert.Equal(expectsAnyFormatSpecificDetails, hasZipDetail || hasSolidDetail); + Assert.Equal(expectedType, info.Detection.ContainerType); + Assert.Equal(expectedStatus, info.Status); + Assert.NotNull(info.EntryCount); + } + + [Fact] + public void InspectArchive_EncryptedHeaders_ReturnsPartialInformation() + { + var info = ArchiveFactory.InspectArchive( + GetTestArchivePath("Rar.encrypted_filesAndHeader.rar") + ); + + Assert.NotNull(info); + Assert.Equal(ArchiveInformationStatus.Partial, info.Status); + Assert.True(info.Limitations.HasFlag(ArchiveInformationLimitations.EncryptedHeaders)); + Assert.Equal(ArchiveEncryptionScope.Headers, info.Encryption); + Assert.True(info.IsEncrypted); + } + + [Fact] + public void InspectArchive_EncryptedHeadersWithPassword_ReportsBothEncryptionScopes() + { + var info = ArchiveFactory.InspectArchive( + GetTestArchivePath("Rar.encrypted_filesAndHeader.rar"), + ReaderOptions.ForFilePath.WithPassword("test") + ); + + Assert.NotNull(info); + Assert.Equal(ArchiveInformationStatus.Complete, info.Status); + Assert.Equal( + ArchiveEncryptionScope.Headers | ArchiveEncryptionScope.EntryData, + info.Encryption + ); + Assert.True(info.IsEncrypted); + } + + [Fact] + public void InspectArchive_IncompleteRarStream_ReturnsPartialInformation() + { + using var stream = File.OpenRead(GetTestArchivePath("Rar.multi.solid.part01.rar")); + + var info = ArchiveFactory.InspectArchive(stream); + + Assert.NotNull(info); + Assert.Equal(ArchiveInformationStatus.Partial, info.Status); + Assert.True(info.Limitations.HasFlag(ArchiveInformationLimitations.MissingVolumes)); + Assert.Null(info.CompressedPayloadSize); + Assert.Null(info.UncompressedPayloadSize); + } + + [Fact] + public async ValueTask InspectArchiveAsync_CompressedTar_ReturnsMetadata() + { + var info = await ArchiveFactory.InspectArchiveAsync(GetTestArchivePath("Tar.tar.xz")); + + Assert.NotNull(info); + Assert.Equal(ArchiveType.Tar, info.Detection.ContainerType); + Assert.Equal(CompressionType.Xz, info.Detection.OuterCompressionType); + Assert.NotNull(info.EntryCount); + } + + [Fact] + public void InspectArchive_MultiVolumeRar_ReturnsVolumeMetadata() + { + FileInfo[] parts = + [ + new(GetTestArchivePath("Rar.multi.solid.part01.rar")), + new(GetTestArchivePath("Rar.multi.solid.part02.rar")), + new(GetTestArchivePath("Rar.multi.solid.part03.rar")), + new(GetTestArchivePath("Rar.multi.solid.part04.rar")), + new(GetTestArchivePath("Rar.multi.solid.part05.rar")), + new(GetTestArchivePath("Rar.multi.solid.part06.rar")), + ]; + + var info = ArchiveFactory.InspectArchive(parts); + + Assert.NotNull(info); + Assert.Equal(ArchiveType.Rar, info.Detection.ContainerType); + Assert.Equal(parts.Length, info.PhysicalPartCount); + Assert.True(info.IsMultiVolume); + } + + [Fact] + public void InspectArchive_MultiVolumeRarPath_DiscoversAllParts() + { + var info = ArchiveFactory.InspectArchive(GetTestArchivePath("Rar.multi.solid.part01.rar")); + + Assert.NotNull(info); + Assert.Equal(6, info.PhysicalPartCount); + Assert.True(info.IsMultiVolume); } private MemoryStream CreatePrefixedArchiveStream(string archiveName, int prefixLength) From e4cc79f252598fc59ae3f790886747ef03002193 Mon Sep 17 00:00:00 2001 From: Julian Xhokaxhiu Date: Tue, 4 Aug 2026 19:56:17 +0200 Subject: [PATCH 29/40] Rar: Fix CPU performance regression --- src/SharpCompress/Compressors/Rar/RarCRC.cs | 39 +++++---------------- 1 file changed, 8 insertions(+), 31 deletions(-) diff --git a/src/SharpCompress/Compressors/Rar/RarCRC.cs b/src/SharpCompress/Compressors/Rar/RarCRC.cs index 7e6fc6cc..ee122d21 100644 --- a/src/SharpCompress/Compressors/Rar/RarCRC.cs +++ b/src/SharpCompress/Compressors/Rar/RarCRC.cs @@ -1,10 +1,16 @@ using System; +using SharpCompress.Crypto; namespace SharpCompress.Compressors.Rar; internal static class RarCRC { - private static readonly uint[] crcTab; + // Reuse Crc32Stream's cached slice-by-16 table (same polynomial) instead of a separate + // byte-at-a-time table, so bulk CRC checks during Rar extraction get the same throughput + // as Zip/GZip/7Zip/LZip CRC32 validation instead of a much slower one-byte-per-iteration loop. + private static readonly uint[] crcTab = Crc32Stream.InitializeTable( + Crc32Stream.DEFAULT_POLYNOMIAL + ); public static uint CheckCrc(uint startCrc, byte b) => (crcTab[((int)startCrc ^ b) & 0xff] ^ (startCrc >> 8)); @@ -12,35 +18,6 @@ internal static class RarCRC public static uint CheckCrc(uint startCrc, ReadOnlySpan data, int offset, int count) { var size = Math.Min(data.Length - offset, count); - - for (var i = 0; i < size; i++) - { - startCrc = (crcTab[((int)startCrc ^ data[offset + i]) & 0xff] ^ (startCrc >> 8)); - } - return (startCrc); - } - - static RarCRC() - { - { - crcTab = new uint[256]; - for (uint i = 0; i < 256; i++) - { - var c = i; - for (var j = 0; j < 8; j++) - { - if ((c & 1) != 0) - { - c >>= 1; - c ^= 0xEDB88320; - } - else - { - c >>= 1; - } - } - crcTab[i] = c; - } - } + return Crc32Stream.CalculateCrc(crcTab, startCrc, data.Slice(offset, size)); } } From 513b6bec45cb2470ee161f235cecd4350f61dad1 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Wed, 5 Aug 2026 13:44:19 +0100 Subject: [PATCH 30/40] fixes SharpCompress TAR extraction overwrites files outside the extraction root when a SymbolicLinkHandler enables symlink chaining --- docs/API.md | 2 + .../Common/DirectoryManagement.cs | 113 +++++++ src/SharpCompress/Common/ExtractionOptions.cs | 2 + .../Common/IEntryExtensions.Async.cs | 29 +- src/SharpCompress/Common/IEntryExtensions.cs | 35 +- .../Common/Options/IExtractionOptions.cs | 2 + .../Security/SymbolicLinkExtractionTests.cs | 304 ++++++++++++++++++ 7 files changed, 475 insertions(+), 12 deletions(-) create mode 100644 tests/SharpCompress.Test/Security/SymbolicLinkExtractionTests.cs diff --git a/docs/API.md b/docs/API.md index 80143e83..7cc13b7d 100644 --- a/docs/API.md +++ b/docs/API.md @@ -463,6 +463,8 @@ using (var archive = ZipArchive.OpenArchive("file.zip")) `CheckCrc` validates archive-level payload checksums when the format stores reliable metadata, such as ZIP CRC32 values. Formats without payload checksums skip this validation. Decompressor integrity checks that are required to decode a stream may still fail even when `CheckCrc` is disabled. +When using `SymbolicLinkHandler`, directory extraction rejects link targets outside the extraction root and never follows symbolic links or reparse points while extracting later entries. The handler itself remains trusted application code. + ### Options matrix ```text diff --git a/src/SharpCompress/Common/DirectoryManagement.cs b/src/SharpCompress/Common/DirectoryManagement.cs index 4bd4585d..0bf1a013 100644 --- a/src/SharpCompress/Common/DirectoryManagement.cs +++ b/src/SharpCompress/Common/DirectoryManagement.cs @@ -1,3 +1,4 @@ +using System; using System.IO; namespace SharpCompress.Common; @@ -8,6 +9,10 @@ internal static class DirectoryManagement "Entry is trying to create a directory outside of the destination directory."; internal const string WriteFileOutsideDestinationMessage = "Entry is trying to write a file outside of the destination directory."; + internal const string LinkTargetOutsideDestinationMessage = + "Entry is trying to create a symbolic link outside of the destination directory."; + internal const string ReparsePointInDestinationMessage = + "Entry is trying to extract through a symbolic link or reparse point."; internal static string GetFullDestinationDirectoryPath(string destinationDirectory) { @@ -58,6 +63,114 @@ internal static class DirectoryManagement throw new ExtractionException(exceptionMessage); } + internal static void EnsureNoReparsePointInDestinationDirectory( + string destinationPath, + string fullDestinationDirectoryPath + ) + { + var destinationDirectoryPath = TrimTrailingDirectorySeparators( + fullDestinationDirectoryPath + ); + EnsurePathIsNotReparsePoint(destinationDirectoryPath); + + if (string.Equals(destinationPath, destinationDirectoryPath, Utility.PathComparison)) + { + return; + } + + var relativeDestinationPath = destinationPath.Substring( + fullDestinationDirectoryPath.Length + ); + var path = destinationDirectoryPath; + + foreach ( + var pathPart in relativeDestinationPath.Split( + new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }, + StringSplitOptions.RemoveEmptyEntries + ) + ) + { + path = Path.Combine(path, pathPart); + + if (!PathExistsAndIsNotReparsePoint(path)) + { + return; + } + } + } + + internal static void CreateDirectory( + string destinationPath, + string fullDestinationDirectoryPath + ) + { + EnsureNoReparsePointInDestinationDirectory(destinationPath, fullDestinationDirectoryPath); + + if (!Directory.Exists(destinationPath)) + { + Directory.CreateDirectory(destinationPath); + } + + EnsureNoReparsePointInDestinationDirectory(destinationPath, fullDestinationDirectoryPath); + } + + internal static void EnsureLinkTargetInDestinationDirectory( + string destinationFileName, + string linkTarget, + string fullDestinationDirectoryPath + ) + { + var destinationDirectory = Path.GetDirectoryName(destinationFileName) + .NotNull("Destination directory is null"); + var fullLinkTargetPath = Path.GetFullPath(Path.Combine(destinationDirectory, linkTarget)); + + EnsurePathInDestinationDirectory( + fullLinkTargetPath, + fullDestinationDirectoryPath, + LinkTargetOutsideDestinationMessage + ); + } + + internal static void EnsurePathIsNotReparsePoint(string path) + { + PathExistsAndIsNotReparsePoint(path); + } + + private static bool PathExistsAndIsNotReparsePoint(string path) + { + try + { + if ((File.GetAttributes(path) & FileAttributes.ReparsePoint) != 0) + { + throw new ExtractionException(ReparsePointInDestinationMessage); + } + + return true; + } + catch (FileNotFoundException) + { + return false; + } + catch (DirectoryNotFoundException) + { + return false; + } + catch (UnauthorizedAccessException exception) + { + throw new ExtractionException( + "Unable to verify the extraction path for symbolic links or reparse points.", + exception + ); + } + catch (IOException exception) + { + throw new ExtractionException( + "Unable to verify the extraction path for symbolic links or reparse points.", + exception + ); + } + } + private static bool IsDirectorySeparator(char value) => value == Path.DirectorySeparatorChar || value == Path.AltDirectorySeparatorChar; diff --git a/src/SharpCompress/Common/ExtractionOptions.cs b/src/SharpCompress/Common/ExtractionOptions.cs index 04b33416..ce58a529 100644 --- a/src/SharpCompress/Common/ExtractionOptions.cs +++ b/src/SharpCompress/Common/ExtractionOptions.cs @@ -60,6 +60,8 @@ public sealed record ExtractionOptions : IExtractionOptions /// /// Breaking change: Changed from field to property in version 0.40.0. /// If no handler is provided, symbolic links are silently skipped during extraction. + /// Directory extraction rejects link targets outside the destination directory and does not + /// follow symbolic links or reparse points in later entry paths. /// public Action? SymbolicLinkHandler { get; set; } diff --git a/src/SharpCompress/Common/IEntryExtensions.Async.cs b/src/SharpCompress/Common/IEntryExtensions.Async.cs index 4ef697e0..eafddfcf 100644 --- a/src/SharpCompress/Common/IEntryExtensions.Async.cs +++ b/src/SharpCompress/Common/IEntryExtensions.Async.cs @@ -38,6 +38,11 @@ internal static partial class IEntryExtensions CancellationToken cancellationToken = default ) { + if (entry.LinkTarget is not null && options.SymbolicLinkHandler is null) + { + return; + } + var destinationFileName = GetEntryDestinationFileName( entry, fullDestinationDirectoryPath, @@ -54,6 +59,20 @@ internal static partial class IEntryExtensions DirectoryManagement.WriteFileOutsideDestinationMessage ); + DirectoryManagement.EnsureNoReparsePointInDestinationDirectory( + destinationFileName, + fullDestinationDirectoryPath + ); + + if (entry.LinkTarget is not null) + { + DirectoryManagement.EnsureLinkTargetInDestinationDirectory( + destinationFileName, + entry.LinkTarget, + fullDestinationDirectoryPath + ); + } + if (writeAsync != null) { await writeAsync(destinationFileName, cancellationToken).ConfigureAwait(false); @@ -69,10 +88,10 @@ internal static partial class IEntryExtensions DirectoryManagement.CreateDirectoryOutsideDestinationMessage ); - if (!Directory.Exists(destinationFileName)) - { - Directory.CreateDirectory(destinationFileName); - } + DirectoryManagement.CreateDirectory( + destinationFileName, + fullDestinationDirectoryPath + ); } } @@ -90,6 +109,8 @@ internal static partial class IEntryExtensions } else { + DirectoryManagement.EnsurePathIsNotReparsePoint(destinationFileName); + var fm = FileMode.Create; if (!options.Overwrite) diff --git a/src/SharpCompress/Common/IEntryExtensions.cs b/src/SharpCompress/Common/IEntryExtensions.cs index 492ad85d..d7e6f5cb 100644 --- a/src/SharpCompress/Common/IEntryExtensions.cs +++ b/src/SharpCompress/Common/IEntryExtensions.cs @@ -45,6 +45,11 @@ internal static partial class IEntryExtensions Action? write ) { + if (entry.LinkTarget is not null && options.SymbolicLinkHandler is null) + { + return; + } + var destinationFileName = GetEntryDestinationFileName( entry, fullDestinationDirectoryPath, @@ -60,6 +65,21 @@ internal static partial class IEntryExtensions fullDestinationDirectoryPath, DirectoryManagement.WriteFileOutsideDestinationMessage ); + + DirectoryManagement.EnsureNoReparsePointInDestinationDirectory( + destinationFileName, + fullDestinationDirectoryPath + ); + + if (entry.LinkTarget is not null) + { + DirectoryManagement.EnsureLinkTargetInDestinationDirectory( + destinationFileName, + entry.LinkTarget, + fullDestinationDirectoryPath + ); + } + write?.Invoke(destinationFileName); } else if (options.ExtractFullPath) @@ -72,10 +92,10 @@ internal static partial class IEntryExtensions DirectoryManagement.CreateDirectoryOutsideDestinationMessage ); - if (!Directory.Exists(destinationFileName)) - { - Directory.CreateDirectory(destinationFileName); - } + DirectoryManagement.CreateDirectory( + destinationFileName, + fullDestinationDirectoryPath + ); } } @@ -102,10 +122,7 @@ internal static partial class IEntryExtensions : DirectoryManagement.WriteFileOutsideDestinationMessage ); - if (!Directory.Exists(destdir)) - { - Directory.CreateDirectory(destdir); - } + DirectoryManagement.CreateDirectory(destdir, fullDestinationDirectoryPath); return Path.Combine(destdir, file); } @@ -126,6 +143,8 @@ internal static partial class IEntryExtensions } else { + DirectoryManagement.EnsurePathIsNotReparsePoint(destinationFileName); + var fm = FileMode.Create; if (!options.Overwrite) diff --git a/src/SharpCompress/Common/Options/IExtractionOptions.cs b/src/SharpCompress/Common/Options/IExtractionOptions.cs index 767bfb93..a6379f91 100644 --- a/src/SharpCompress/Common/Options/IExtractionOptions.cs +++ b/src/SharpCompress/Common/Options/IExtractionOptions.cs @@ -39,6 +39,8 @@ public interface IExtractionOptions /// Delegate for writing symbolic links to disk. /// The first parameter is the source path (where the symlink is created). /// The second parameter is the target path (what the symlink refers to). + /// Directory extraction rejects link targets outside the destination directory and does not + /// follow symbolic links or reparse points in later entry paths. /// Action? SymbolicLinkHandler { get; set; } } diff --git a/tests/SharpCompress.Test/Security/SymbolicLinkExtractionTests.cs b/tests/SharpCompress.Test/Security/SymbolicLinkExtractionTests.cs new file mode 100644 index 00000000..96ae170c --- /dev/null +++ b/tests/SharpCompress.Test/Security/SymbolicLinkExtractionTests.cs @@ -0,0 +1,304 @@ +#if NET8_0_OR_GREATER +using System; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SharpCompress.Archives; +using SharpCompress.Common; +using SharpCompress.Readers; +using Xunit; + +namespace SharpCompress.Test.Security; + +public class SymbolicLinkExtractionTests : TestBase +{ + private const int TarBlockSize = 512; + + [Theory] + [InlineData("ReaderAll")] + [InlineData("ReaderEntry")] + [InlineData("Archive")] + [InlineData("ArchiveEntry")] + [InlineData("AsyncReaderAll")] + [InlineData("AsyncReaderEntry")] + [InlineData("AsyncArchive")] + [InlineData("AsyncArchiveEntry")] + public async Task SymbolicLinkTargetOutsideDestination_ShouldThrowBeforeInvokingHandler( + string api + ) + { + var destinationDirectory = GetScratchPath("extract"); + var outsideDirectory = GetScratchPath("outside"); + var archivePath = GetScratch2Path("symbolic-link-outside.tar"); + Directory.CreateDirectory(destinationDirectory); + Directory.CreateDirectory(outsideDirectory); + BuildTar(archivePath, "../outside"); + + var handlerCalls = 0; + var options = new ExtractionOptions { SymbolicLinkHandler = (_, _) => handlerCalls++ }; + + var exception = await ExtractAsync(api, archivePath, destinationDirectory, options); + + var extractionException = Assert.IsType(exception); + Assert.Contains("symbolic link outside", extractionException.Message); + Assert.Equal(0, handlerCalls); + Assert.False(File.Exists(Path.Combine(outsideDirectory, "secret.txt"))); + } + + [Theory] + [InlineData("ReaderAll")] + [InlineData("Archive")] + [InlineData("AsyncReaderAll")] + [InlineData("AsyncArchive")] + public async Task EntriesBeneathSymbolicLink_ShouldNotBeExtracted(string api) + { + var destinationDirectory = GetScratchPath("extract"); + var targetDirectory = Path.Combine(destinationDirectory, "target"); + var archivePath = GetScratch2Path("symbolic-link-inside.tar"); + Directory.CreateDirectory(destinationDirectory); + Directory.CreateDirectory(targetDirectory); + BuildTar(archivePath, "target"); + + var handlerCalls = 0; + var options = new ExtractionOptions + { + SymbolicLinkHandler = (linkPath, linkTarget) => + { + handlerCalls++; + Directory.CreateSymbolicLink(linkPath, linkTarget); + }, + }; + + var exception = await ExtractAsync(api, archivePath, destinationDirectory, options); + + var extractionException = Assert.IsType(exception); + Assert.Contains("symbolic link or reparse point", extractionException.Message); + Assert.Equal(1, handlerCalls); + Assert.False(File.Exists(Path.Combine(targetDirectory, "secret.txt"))); + } + + private static void BuildTar(string path, string linkTarget) + { + using var stream = File.Create(path); + WriteTarEntry(stream, "link", (byte)'2', linkTarget, Array.Empty()); + WriteTarEntry(stream, "link/secret.txt", (byte)'0', null, Encoding.UTF8.GetBytes("secret")); + stream.Write(new byte[TarBlockSize * 2]); + } + + private static void WriteTarEntry( + Stream stream, + string name, + byte entryType, + string? linkTarget, + byte[] data + ) + { + var header = new byte[TarBlockSize]; + WriteString(header, 0, 100, name); + WriteOctal(header, 100, 8, 0b110_100_100); + WriteOctal(header, 108, 8, 0); + WriteOctal(header, 116, 8, 0); + WriteOctal(header, 124, 12, data.Length); + WriteOctal(header, 136, 12, 0); + Array.Fill(header, (byte)' ', 148, 8); + header[156] = entryType; + WriteString(header, 157, 100, linkTarget ?? string.Empty); + WriteString(header, 257, 6, "ustar"); + WriteString(header, 263, 2, "00"); + + var checksum = header.Sum(value => value); + WriteString(header, 148, 6, Convert.ToString(checksum, 8).PadLeft(6, '0')); + header[154] = 0; + header[155] = (byte)' '; + + stream.Write(header); + stream.Write(data); + + var padding = (TarBlockSize - (data.Length % TarBlockSize)) % TarBlockSize; + if (padding > 0) + { + stream.Write(new byte[padding]); + } + } + + private static void WriteString(byte[] buffer, int offset, int length, string value) + { + var bytes = Encoding.ASCII.GetBytes(value); + Array.Copy(bytes, 0, buffer, offset, Math.Min(bytes.Length, length)); + } + + private static void WriteOctal(byte[] buffer, int offset, int length, long value) + { + WriteString( + buffer, + offset, + length - 1, + Convert.ToString(value, 8).PadLeft(length - 1, '0') + ); + buffer[offset + length - 1] = 0; + } + + private static Task ExtractAsync( + string api, + string archivePath, + string destinationDirectory, + ExtractionOptions options + ) => + api switch + { + "ReaderAll" => Task.FromResult( + RecordException(() => + ExtractWithReaderAll(archivePath, destinationDirectory, options) + ) + ), + "ReaderEntry" => Task.FromResult( + RecordException(() => + ExtractWithReaderEntry(archivePath, destinationDirectory, options) + ) + ), + "Archive" => Task.FromResult( + RecordException(() => + ExtractWithArchive(archivePath, destinationDirectory, options) + ) + ), + "ArchiveEntry" => Task.FromResult( + RecordException(() => + ExtractWithArchiveEntry(archivePath, destinationDirectory, options) + ) + ), + "AsyncReaderAll" => RecordExceptionAsync(() => + ExtractWithAsyncReaderAllAsync(archivePath, destinationDirectory, options) + ), + "AsyncReaderEntry" => RecordExceptionAsync(() => + ExtractWithAsyncReaderEntryAsync(archivePath, destinationDirectory, options) + ), + "AsyncArchive" => RecordExceptionAsync(() => + ExtractWithAsyncArchiveAsync(archivePath, destinationDirectory, options) + ), + "AsyncArchiveEntry" => RecordExceptionAsync(() => + ExtractWithAsyncArchiveEntryAsync(archivePath, destinationDirectory, options) + ), + _ => throw new ArgumentOutOfRangeException(nameof(api), api, null), + }; + + private static Exception? RecordException(Action action) + { + try + { + action(); + return null; + } + catch (Exception exception) + { + return exception; + } + } + + private static async Task RecordExceptionAsync(Func action) + { + try + { + await action(); + return null; + } + catch (Exception exception) + { + return exception; + } + } + + private static void ExtractWithReaderAll( + string archivePath, + string destinationDirectory, + ExtractionOptions options + ) + { + using var stream = File.OpenRead(archivePath); + using var reader = ReaderFactory.OpenReader(stream); + reader.WriteAllToDirectory(destinationDirectory, options); + } + + private static void ExtractWithReaderEntry( + string archivePath, + string destinationDirectory, + ExtractionOptions options + ) + { + using var stream = File.OpenRead(archivePath); + using var reader = ReaderFactory.OpenReader(stream); + Assert.True(reader.MoveToNextEntry()); + reader.WriteEntryToDirectory(destinationDirectory, options); + } + + private static void ExtractWithArchive( + string archivePath, + string destinationDirectory, + ExtractionOptions options + ) + { + using var archive = ArchiveFactory.OpenArchive(archivePath); + archive.WriteToDirectory(destinationDirectory, options); + } + + private static void ExtractWithArchiveEntry( + string archivePath, + string destinationDirectory, + ExtractionOptions options + ) + { + using var archive = ArchiveFactory.OpenArchive(archivePath); + archive.Entries.First().WriteToDirectory(destinationDirectory, options); + } + + private static async Task ExtractWithAsyncReaderAllAsync( + string archivePath, + string destinationDirectory, + ExtractionOptions options + ) + { + using var stream = File.OpenRead(archivePath); + await using var reader = await ReaderFactory.OpenAsyncReader(stream); + await reader.WriteAllToDirectoryAsync(destinationDirectory, options); + } + + private static async Task ExtractWithAsyncReaderEntryAsync( + string archivePath, + string destinationDirectory, + ExtractionOptions options + ) + { + using var stream = File.OpenRead(archivePath); + await using var reader = await ReaderFactory.OpenAsyncReader(stream); + Assert.True(await reader.MoveToNextEntryAsync()); + await reader.WriteEntryToDirectoryAsync(destinationDirectory, options); + } + + private static async Task ExtractWithAsyncArchiveAsync( + string archivePath, + string destinationDirectory, + ExtractionOptions options + ) + { + await using var archive = await ArchiveFactory.OpenAsyncArchive(archivePath); + await archive.WriteToDirectoryAsync(destinationDirectory, options); + } + + private static async Task ExtractWithAsyncArchiveEntryAsync( + string archivePath, + string destinationDirectory, + ExtractionOptions options + ) + { + await using var archive = await ArchiveFactory.OpenAsyncArchive(archivePath); + + await foreach (var entry in archive.EntriesAsync) + { + await entry.WriteToDirectoryAsync(destinationDirectory, options); + return; + } + + throw new InvalidOperationException("Archive did not contain an entry."); + } +} +#endif From b97cce5ef1d684f8f7beae7bcbc5b54ee2727a96 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Wed, 5 Aug 2026 14:04:41 +0100 Subject: [PATCH 31/40] review fixes --- src/SharpCompress/Common/DirectoryManagement.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/SharpCompress/Common/DirectoryManagement.cs b/src/SharpCompress/Common/DirectoryManagement.cs index 0bf1a013..d9a53f95 100644 --- a/src/SharpCompress/Common/DirectoryManagement.cs +++ b/src/SharpCompress/Common/DirectoryManagement.cs @@ -10,7 +10,7 @@ internal static class DirectoryManagement internal const string WriteFileOutsideDestinationMessage = "Entry is trying to write a file outside of the destination directory."; internal const string LinkTargetOutsideDestinationMessage = - "Entry is trying to create a symbolic link outside of the destination directory."; + "Entry is trying to create a symbolic link whose target is outside of the destination directory."; internal const string ReparsePointInDestinationMessage = "Entry is trying to extract through a symbolic link or reparse point."; @@ -133,6 +133,11 @@ internal static class DirectoryManagement internal static void EnsurePathIsNotReparsePoint(string path) { + var destinationDirectory = Path.GetDirectoryName(path); + if (destinationDirectory is not null) + { + PathExistsAndIsNotReparsePoint(destinationDirectory); + } PathExistsAndIsNotReparsePoint(path); } From a25ef129a4e93dc2ed577cb95fb5a807fa71731f Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Wed, 5 Aug 2026 14:33:19 +0100 Subject: [PATCH 32/40] Fix sym link tests --- .../Security/SymbolicLinkExtractionTests.cs | 40 ++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/tests/SharpCompress.Test/Security/SymbolicLinkExtractionTests.cs b/tests/SharpCompress.Test/Security/SymbolicLinkExtractionTests.cs index 96ae170c..905add96 100644 --- a/tests/SharpCompress.Test/Security/SymbolicLinkExtractionTests.cs +++ b/tests/SharpCompress.Test/Security/SymbolicLinkExtractionTests.cs @@ -1,5 +1,6 @@ #if NET8_0_OR_GREATER using System; +using System.Diagnostics; using System.IO; using System.Linq; using System.Text; @@ -41,7 +42,7 @@ public class SymbolicLinkExtractionTests : TestBase var exception = await ExtractAsync(api, archivePath, destinationDirectory, options); var extractionException = Assert.IsType(exception); - Assert.Contains("symbolic link outside", extractionException.Message); + Assert.Contains("symbolic link whose target is outside", extractionException.Message); Assert.Equal(0, handlerCalls); Assert.False(File.Exists(Path.Combine(outsideDirectory, "secret.txt"))); } @@ -66,18 +67,47 @@ public class SymbolicLinkExtractionTests : TestBase SymbolicLinkHandler = (linkPath, linkTarget) => { handlerCalls++; - Directory.CreateSymbolicLink(linkPath, linkTarget); + CreateReparsePoint(linkPath, linkTarget); }, }; - var exception = await ExtractAsync(api, archivePath, destinationDirectory, options); + var extractionException = (await ExtractAsync(api, archivePath, destinationDirectory, options)).NotNull(); - var extractionException = Assert.IsType(exception); - Assert.Contains("symbolic link or reparse point", extractionException.Message); + Assert.Contains("symbolic link or reparse point", extractionException.ToString()); Assert.Equal(1, handlerCalls); Assert.False(File.Exists(Path.Combine(targetDirectory, "secret.txt"))); } + private static void CreateReparsePoint(string linkPath, string linkTarget) + { + if (OperatingSystem.IsWindows()) + { + // Directory junctions are reparse points that need no elevation, unlike symbolic links. + // Junction targets must be absolute. + var absoluteTarget = Path.GetFullPath( + Path.Combine(Path.GetDirectoryName(linkPath).NotNull(), linkTarget) + ); + var startInfo = new ProcessStartInfo("cmd.exe") + { + UseShellExecute = false, + CreateNoWindow = true, + }; + startInfo.ArgumentList.Add("/c"); + startInfo.ArgumentList.Add("mklink"); + startInfo.ArgumentList.Add("/j"); + startInfo.ArgumentList.Add(linkPath); + startInfo.ArgumentList.Add(absoluteTarget); + + using var process = Process.Start(startInfo).NotNull(); + process.WaitForExit(); + Assert.Equal(0, process.ExitCode); + } + else + { + Directory.CreateSymbolicLink(linkPath, linkTarget); + } + } + private static void BuildTar(string path, string linkTarget) { using var stream = File.Create(path); From c083c6efd843a844b0c8f7878787360e815be781 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Wed, 5 Aug 2026 15:59:16 +0100 Subject: [PATCH 33/40] fmt --- .../Security/SymbolicLinkExtractionTests.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/SharpCompress.Test/Security/SymbolicLinkExtractionTests.cs b/tests/SharpCompress.Test/Security/SymbolicLinkExtractionTests.cs index 905add96..a84c3267 100644 --- a/tests/SharpCompress.Test/Security/SymbolicLinkExtractionTests.cs +++ b/tests/SharpCompress.Test/Security/SymbolicLinkExtractionTests.cs @@ -71,7 +71,9 @@ public class SymbolicLinkExtractionTests : TestBase }, }; - var extractionException = (await ExtractAsync(api, archivePath, destinationDirectory, options)).NotNull(); + var extractionException = ( + await ExtractAsync(api, archivePath, destinationDirectory, options) + ).NotNull(); Assert.Contains("symbolic link or reparse point", extractionException.ToString()); Assert.Equal(1, handlerCalls); From ea7d97fcee9878d3449d16d3088786cecd749bac Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Wed, 5 Aug 2026 16:20:08 +0100 Subject: [PATCH 34/40] better zip streaming info --- docs/API.md | 4 +- .../ArchiveFactory.Information.Async.cs | 46 ++++++++++++- .../Archives/ArchiveFactory.Information.cs | 67 ++++++++++++------- .../Archives/ZipArchiveInformation.cs | 9 +-- .../Common/Zip/SeekableZipFilePart.Async.cs | 5 +- .../Common/Zip/SeekableZipFilePart.cs | 13 +++- .../Zip/SeekableZipHeaderFactory.Async.cs | 32 ++++++--- .../Common/Zip/SeekableZipHeaderFactory.cs | 26 ++++++- .../Common/Zip/ZipHeaderFactory.Async.cs | 10 ++- .../Common/Zip/ZipHeaderFactory.cs | 10 ++- .../SharpCompress.Test/ArchiveFactoryTests.cs | 29 ++++++-- 11 files changed, 192 insertions(+), 59 deletions(-) diff --git a/docs/API.md b/docs/API.md index 1536c9c6..29ecb2a2 100644 --- a/docs/API.md +++ b/docs/API.md @@ -66,7 +66,7 @@ if (information is not null) { Console.WriteLine($"Entries: {information.EntryCount}"); Console.WriteLine($"Solid streams: {information.SolidStreamCount}"); - Console.WriteLine($"ZIP data descriptor entries: {information.Zip?.DataDescriptorEntryCount}"); + Console.WriteLine($"Some ZIP entry sizes arrive after their data: {information.Zip?.HasEntriesWithDeferredSizes}"); } var asyncInformation = await ArchiveFactory.InspectArchiveAsync("archive.zip", cancellationToken); @@ -96,7 +96,7 @@ The stream overloads of `DetectArchive` and `InspectArchive` preserve the suppli | `GetArchiveInformationAsync(...)` | `DetectArchiveAsync(...)` or `InspectArchiveAsync(...)` | | `Type` | `Detection.ContainerType` | | `SupportsRandomAccess` | `Detection.SupportedApis.HasFlag(ArchiveAccessMode.Archive)` | -| `ZipDataDescriptorEntryCount` | `Zip.DataDescriptorEntryCount` | +| `ZipDataDescriptorEntryCount` | `Zip.HasEntriesWithDeferredSizes` | | `SolidStreamCount` | `SolidStreamCount` | ### Creating Archives diff --git a/src/SharpCompress/Archives/ArchiveFactory.Information.Async.cs b/src/SharpCompress/Archives/ArchiveFactory.Information.Async.cs index 9c8076ef..c4fd4fa2 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.Information.Async.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.Information.Async.cs @@ -6,6 +6,7 @@ using System.Threading; using System.Threading.Tasks; using SharpCompress.Archives.Rar; using SharpCompress.Archives.SevenZip; +using SharpCompress.Archives.Zip; using SharpCompress.Common; using SharpCompress.Common.Ace.Headers; using SharpCompress.Common.Rar; @@ -389,7 +390,12 @@ public static partial class ArchiveFactory var entryArray = entries.ToArray(); var volumeArray = volumes.ToArray(); - var zip = GetZipInformation(entryArray); + var (zipInformation, deferredSizeEntryCount) = await GetZipInformationAsync( + archive.Type == ArchiveType.Zip, + entryArray, + cancellationToken + ) + .ConfigureAwait(false); var (isSolid, solidStreamCount) = await GetSolidInformationAsync(archive, entryArray) .ConfigureAwait(false); var isComplete = await archive.IsCompleteAsync().ConfigureAwait(false); @@ -411,7 +417,7 @@ public static partial class ArchiveFactory limitations, GetFormatVersion(volumeArray), entryArray.LongLength, - zip?.DataDescriptorEntryCount ?? 0, + deferredSizeEntryCount, physicalSize, isComplete ? GetCompressedPayloadSize(archive, entryArray) : null, isComplete && archive.Type != ArchiveType.GZip @@ -426,7 +432,7 @@ public static partial class ArchiveFactory volumeArray.Length, isComplete, GetArchiveComment(volumeArray), - zip + zipInformation ); } @@ -522,6 +528,40 @@ public static partial class ArchiveFactory _ => entries.Aggregate(0L, (total, entry) => total + entry.CompressedSize), }; + private static async ValueTask<( + ZipArchiveInformation? Information, + long DeferredSizeEntryCount + )> GetZipInformationAsync( + bool isZipArchive, + IEnumerable entries, + CancellationToken cancellationToken + ) + { + if (!isZipArchive) + { + return (null, 0); + } + + long deferredSizeEntryCount = 0; + foreach (var entry in entries.OfType()) + { + cancellationToken.ThrowIfCancellationRequested(); + var filePart = entry.Parts.OfType().Single(); + if (!filePart.HasDeferredSizes) + { + continue; + } + + var localHeader = await filePart.GetRawLocalHeaderAsync().ConfigureAwait(false); + if (localHeader.CompressedSize == 0 && localHeader.UncompressedSize == 0) + { + deferredSizeEntryCount++; + } + } + + return (new ZipArchiveInformation(deferredSizeEntryCount > 0), deferredSizeEntryCount); + } + private static async ValueTask RedetectArchiveAsync( Stream stream, long startPosition, diff --git a/src/SharpCompress/Archives/ArchiveFactory.Information.cs b/src/SharpCompress/Archives/ArchiveFactory.Information.cs index 1179a41a..264e55c9 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.Information.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.Information.cs @@ -258,7 +258,10 @@ public static partial class ArchiveFactory { var entries = archive.Entries.ToArray(); var volumes = archive.Volumes.ToArray(); - var zip = GetZipInformation(entries); + var (zipInformation, deferredSizeEntryCount) = GetZipInformation( + archive.Type == ArchiveType.Zip, + entries + ); var (isSolid, solidStreamCount) = GetSolidInformation(archive, entries); var isMultiVolume = GetIsMultiVolume(archive.Type, volumes); var comment = GetArchiveComment(volumes); @@ -281,7 +284,7 @@ public static partial class ArchiveFactory limitations, GetFormatVersion(volumes), entries.LongLength, - zip?.DataDescriptorEntryCount ?? 0, + deferredSizeEntryCount, physicalSize, isComplete ? GetCompressedPayloadSize(archive, entries) : null, isComplete && archive.Type != ArchiveType.GZip @@ -296,7 +299,7 @@ public static partial class ArchiveFactory volumes.Length, isComplete, comment, - zip + zipInformation ); } @@ -384,23 +387,33 @@ public static partial class ArchiveFactory ? ArchiveInformationStatus.Complete : ArchiveInformationStatus.Partial; - private static ZipArchiveInformation? GetZipInformation(IEnumerable entries) + private static ( + ZipArchiveInformation? Information, + long DeferredSizeEntryCount + ) GetZipInformation(bool isZipArchive, IEnumerable entries) { - var dataDescriptorEntryCount = entries - .OfType() - .LongCount(entry => - entry - .Parts.OfType() - .Any(part => - FlagUtility.HasFlag( - part.Header.Flags, - SharpCompress.Common.Zip.Headers.HeaderFlags.UsePostDataDescriptor - ) - ) - ); - return dataDescriptorEntryCount == 0 && !entries.OfType().Any() - ? null - : new ZipArchiveInformation(dataDescriptorEntryCount); + if (!isZipArchive) + { + return (null, 0); + } + + long deferredSizeEntryCount = 0; + foreach (var entry in entries.OfType()) + { + var filePart = entry.Parts.OfType().Single(); + if (!filePart.HasDeferredSizes) + { + continue; + } + + var localHeader = filePart.GetRawLocalHeader(); + if (localHeader.CompressedSize == 0 && localHeader.UncompressedSize == 0) + { + deferredSizeEntryCount++; + } + } + + return (new ZipArchiveInformation(deferredSizeEntryCount > 0), deferredSizeEntryCount); } private static (bool IsSolid, long SolidStreamCount) GetSolidInformation( @@ -563,7 +576,7 @@ public static partial class ArchiveFactory private readonly ArchiveDetection detection; private long? compressedPayloadSize = 0; private long? uncompressedPayloadSize = 0; - private long dataDescriptorEntryCount; + private long deferredSizeEntryCount; private long entriesWithUnknownSizeCount; private bool isEncrypted; private ArchiveInformationLimitations limitations; @@ -595,15 +608,15 @@ public static partial class ArchiveFactory EntryCount++; isEncrypted |= entry.IsEncrypted; - var usesDataDescriptor = UsesZipDataDescriptor(entry); - if (usesDataDescriptor) + var hasDeferredSizes = HasDeferredSizes(entry); + if (hasDeferredSizes) { - dataDescriptorEntryCount++; + deferredSizeEntryCount++; } if ( detection.ContainerType == ArchiveType.Lzw - || usesDataDescriptor + || hasDeferredSizes || !TryGetSize(entry, out var size) ) { @@ -650,7 +663,7 @@ public static partial class ArchiveFactory true, null, detection.ContainerType == ArchiveType.Zip - ? new ZipArchiveInformation(dataDescriptorEntryCount) + ? new ZipArchiveInformation(deferredSizeEntryCount > 0) : null ); @@ -668,7 +681,7 @@ public static partial class ArchiveFactory } } - private static bool UsesZipDataDescriptor(IEntry entry) => + private static bool HasDeferredSizes(IEntry entry) => entry is ZipEntry zipEntry && zipEntry .Parts.OfType() @@ -677,6 +690,8 @@ public static partial class ArchiveFactory part.Header.Flags, SharpCompress.Common.Zip.Headers.HeaderFlags.UsePostDataDescriptor ) + && part.Header.CompressedSize == 0 + && part.Header.UncompressedSize == 0 ); } } diff --git a/src/SharpCompress/Archives/ZipArchiveInformation.cs b/src/SharpCompress/Archives/ZipArchiveInformation.cs index c7160903..80bc76cd 100644 --- a/src/SharpCompress/Archives/ZipArchiveInformation.cs +++ b/src/SharpCompress/Archives/ZipArchiveInformation.cs @@ -5,11 +5,12 @@ namespace SharpCompress.Archives; /// public sealed class ZipArchiveInformation { - internal ZipArchiveInformation(long dataDescriptorEntryCount) => - DataDescriptorEntryCount = dataDescriptorEntryCount; + internal ZipArchiveInformation(bool hasEntriesWithDeferredSizes) => + HasEntriesWithDeferredSizes = hasEntriesWithDeferredSizes; /// - /// Gets the number of entries whose local header defers its CRC and sizes to a data descriptor. + /// Gets whether any entry sizes are available only after reading the entry data. + /// When , a forward-only reader can initially report zero sizes for those entries. /// - public long DataDescriptorEntryCount { get; } + public bool HasEntriesWithDeferredSizes { get; } } diff --git a/src/SharpCompress/Common/Zip/SeekableZipFilePart.Async.cs b/src/SharpCompress/Common/Zip/SeekableZipFilePart.Async.cs index da9ea359..f4ff6799 100644 --- a/src/SharpCompress/Common/Zip/SeekableZipFilePart.Async.cs +++ b/src/SharpCompress/Common/Zip/SeekableZipFilePart.Async.cs @@ -23,6 +23,9 @@ internal partial class SeekableZipFilePart [Zomp.SyncMethodGenerator.CreateSyncVersion] private async ValueTask LoadLocalHeaderAsync(CancellationToken cancellationToken = default) => Header = await _headerFactory - .GetLocalHeaderAsync(BaseStream, (DirectoryEntryHeader)Header) + .GetLocalHeaderAsync(BaseStream, _directoryEntryHeader) .ConfigureAwait(false); + + internal ValueTask GetRawLocalHeaderAsync() => + _headerFactory.GetRawLocalHeaderAsync(BaseStream, _directoryEntryHeader); } diff --git a/src/SharpCompress/Common/Zip/SeekableZipFilePart.cs b/src/SharpCompress/Common/Zip/SeekableZipFilePart.cs index bed94da3..166c55b8 100644 --- a/src/SharpCompress/Common/Zip/SeekableZipFilePart.cs +++ b/src/SharpCompress/Common/Zip/SeekableZipFilePart.cs @@ -8,6 +8,7 @@ internal partial class SeekableZipFilePart : ZipFilePart { private bool _isLocalHeaderLoaded; private readonly SeekableZipHeaderFactory _headerFactory; + private readonly DirectoryEntryHeader _directoryEntryHeader; internal SeekableZipFilePart( SeekableZipHeaderFactory headerFactory, @@ -15,7 +16,17 @@ internal partial class SeekableZipFilePart : ZipFilePart Stream stream, CompressionProviderRegistry compressionProviders ) - : base(header, stream, compressionProviders) => _headerFactory = headerFactory; + : base(header, stream, compressionProviders) + { + _headerFactory = headerFactory; + _directoryEntryHeader = header; + } + + internal LocalEntryHeader GetRawLocalHeader() => + _headerFactory.GetRawLocalHeader(BaseStream, _directoryEntryHeader); + + internal bool HasDeferredSizes => + FlagUtility.HasFlag(_directoryEntryHeader.Flags, HeaderFlags.UsePostDataDescriptor); protected override Stream CreateBaseStream() { diff --git a/src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.Async.cs b/src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.Async.cs index 11e7eec4..e9065f80 100644 --- a/src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.Async.cs +++ b/src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.Async.cs @@ -146,17 +146,27 @@ internal sealed partial class SeekableZipHeaderFactory throw new ArchiveOperationException(); } - // populate fields only known from the DirectoryEntryHeader - localEntryHeader.HasData = directoryEntryHeader.HasData; - localEntryHeader.ExternalFileAttributes = directoryEntryHeader.ExternalFileAttributes; - localEntryHeader.Comment = directoryEntryHeader.Comment; - - if (FlagUtility.HasFlag(localEntryHeader.Flags, HeaderFlags.UsePostDataDescriptor)) - { - localEntryHeader.Crc = directoryEntryHeader.Crc; - localEntryHeader.CompressedSize = directoryEntryHeader.CompressedSize; - localEntryHeader.UncompressedSize = directoryEntryHeader.UncompressedSize; - } + PopulateDirectoryEntryMetadata(localEntryHeader, directoryEntryHeader); return localEntryHeader; } + + internal async ValueTask GetRawLocalHeaderAsync( + Stream stream, + DirectoryEntryHeader directoryEntryHeader + ) + { + stream.Seek(directoryEntryHeader.RelativeOffsetOfEntryHeader, SeekOrigin.Begin); +#if NET8_0_OR_GREATER + await using var reader = new AsyncBinaryReader(stream, leaveOpen: true); +#else + using var reader = new AsyncBinaryReader(stream, leaveOpen: true); +#endif + var signature = await reader.ReadUInt32Async().ConfigureAwait(false); + if (signature != ENTRY_HEADER_BYTES) + { + throw new ArchiveOperationException(); + } + + return await ReadLocalHeader(reader).ConfigureAwait(false); + } } diff --git a/src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.cs b/src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.cs index 7a88a480..c20163b9 100644 --- a/src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.cs +++ b/src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.cs @@ -151,6 +151,31 @@ internal sealed partial class SeekableZipHeaderFactory : ZipHeaderFactory throw new ArchiveOperationException(); } + PopulateDirectoryEntryMetadata(localEntryHeader, directoryEntryHeader); + return localEntryHeader; + } + + internal LocalEntryHeader GetRawLocalHeader( + Stream stream, + DirectoryEntryHeader directoryEntryHeader + ) + { + stream.Seek(directoryEntryHeader.RelativeOffsetOfEntryHeader, SeekOrigin.Begin); + var reader = new BinaryReader(stream); + var signature = reader.ReadUInt32(); + if (signature != ENTRY_HEADER_BYTES) + { + throw new ArchiveOperationException(); + } + + return ReadLocalHeader(reader); + } + + private static void PopulateDirectoryEntryMetadata( + LocalEntryHeader localEntryHeader, + DirectoryEntryHeader directoryEntryHeader + ) + { // populate fields only known from the DirectoryEntryHeader localEntryHeader.HasData = directoryEntryHeader.HasData; localEntryHeader.ExternalFileAttributes = directoryEntryHeader.ExternalFileAttributes; @@ -163,6 +188,5 @@ internal sealed partial class SeekableZipHeaderFactory : ZipHeaderFactory localEntryHeader.CompressedSize = directoryEntryHeader.CompressedSize; localEntryHeader.UncompressedSize = directoryEntryHeader.UncompressedSize; } - return localEntryHeader; } } diff --git a/src/SharpCompress/Common/Zip/ZipHeaderFactory.Async.cs b/src/SharpCompress/Common/Zip/ZipHeaderFactory.Async.cs index bc5944b4..bb420a59 100644 --- a/src/SharpCompress/Common/Zip/ZipHeaderFactory.Async.cs +++ b/src/SharpCompress/Common/Zip/ZipHeaderFactory.Async.cs @@ -20,8 +20,7 @@ internal partial class ZipHeaderFactory { case ENTRY_HEADER_BYTES: { - var entryHeader = new LocalEntryHeader(_archiveEncoding); - await entryHeader.Read(reader).ConfigureAwait(false); + var entryHeader = await ReadLocalHeader(reader).ConfigureAwait(false); await LoadHeaderAsync(entryHeader, reader.BaseStream).ConfigureAwait(false); _lastEntryHeader = entryHeader; @@ -87,6 +86,13 @@ internal partial class ZipHeaderFactory } } + protected async ValueTask ReadLocalHeader(AsyncBinaryReader reader) + { + var entryHeader = new LocalEntryHeader(_archiveEncoding); + await entryHeader.Read(reader).ConfigureAwait(false); + return entryHeader; + } + /// /// Loads encryption metadata and stream positioning for a header using async reads where needed. /// diff --git a/src/SharpCompress/Common/Zip/ZipHeaderFactory.cs b/src/SharpCompress/Common/Zip/ZipHeaderFactory.cs index e1ebb8c1..f98620f9 100644 --- a/src/SharpCompress/Common/Zip/ZipHeaderFactory.cs +++ b/src/SharpCompress/Common/Zip/ZipHeaderFactory.cs @@ -42,8 +42,7 @@ internal partial class ZipHeaderFactory { case ENTRY_HEADER_BYTES: { - var entryHeader = new LocalEntryHeader(_archiveEncoding); - entryHeader.Read(reader); + var entryHeader = ReadLocalHeader(reader); LoadHeader(entryHeader, reader.BaseStream); _lastEntryHeader = entryHeader; @@ -109,6 +108,13 @@ internal partial class ZipHeaderFactory } } + protected LocalEntryHeader ReadLocalHeader(BinaryReader reader) + { + var entryHeader = new LocalEntryHeader(_archiveEncoding); + entryHeader.Read(reader); + return entryHeader; + } + internal static bool IsHeader(uint headerBytes) { switch (headerBytes) diff --git a/tests/SharpCompress.Test/ArchiveFactoryTests.cs b/tests/SharpCompress.Test/ArchiveFactoryTests.cs index 5ea9d3c0..2de784f9 100644 --- a/tests/SharpCompress.Test/ArchiveFactoryTests.cs +++ b/tests/SharpCompress.Test/ArchiveFactoryTests.cs @@ -760,11 +760,13 @@ public class ArchiveFactoryTests : TestBase } [Theory] - [InlineData("Zip.none.datadescriptors.zip", 2)] - [InlineData("Zip.deflate.zip", 0)] - public void InspectArchive_ZipDataDescriptorEntryCount( + [InlineData("Zip.none.datadescriptors.zip", true, 2)] + [InlineData("Zip.deflate.dd.zip", true, 3)] + [InlineData("Zip.deflate.zip", false, 0)] + public void InspectArchive_ZipReportsEntriesWithDeferredSizes( string archiveName, - long expectedDataDescriptorEntryCount + bool expectedHasEntriesWithDeferredSizes, + long expectedUnknownSizeEntryCount ) { using var stream = File.OpenRead(GetTestArchivePath(archiveName)); @@ -774,8 +776,23 @@ public class ArchiveFactoryTests : TestBase Assert.NotNull(info); Assert.Equal(ArchiveType.Zip, info.Detection.ContainerType); Assert.NotNull(info.Zip); - Assert.Equal(expectedDataDescriptorEntryCount, info.Zip.DataDescriptorEntryCount); - Assert.Equal(expectedDataDescriptorEntryCount, info.EntriesWithUnknownSizeCount); + Assert.Equal(expectedHasEntriesWithDeferredSizes, info.Zip.HasEntriesWithDeferredSizes); + Assert.Equal(expectedUnknownSizeEntryCount, info.EntriesWithUnknownSizeCount); + } + + [Theory] + [InlineData("Zip.none.datadescriptors.zip", true)] + [InlineData("Zip.deflate.zip", false)] + public async ValueTask InspectArchiveAsync_ZipReportsEntriesWithDeferredSizes( + string archiveName, + bool expectedHasEntriesWithDeferredSizes + ) + { + var info = await ArchiveFactory.InspectArchiveAsync(GetTestArchivePath(archiveName)); + + Assert.NotNull(info); + Assert.NotNull(info.Zip); + Assert.Equal(expectedHasEntriesWithDeferredSizes, info.Zip.HasEntriesWithDeferredSizes); } [Theory] From 6f497d73bf5d2e01d7d3a8e0d2dd4971dad209d2 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Thu, 6 Aug 2026 08:14:48 +0100 Subject: [PATCH 35/40] use sync generator --- .../ArchiveFactory.Information.Async.cs | 3 ++ .../Archives/ArchiveFactory.Information.cs | 29 ------------------- .../Common/Zip/SeekableZipFilePart.Async.cs | 7 +++-- .../Common/Zip/SeekableZipFilePart.cs | 3 -- 4 files changed, 8 insertions(+), 34 deletions(-) diff --git a/src/SharpCompress/Archives/ArchiveFactory.Information.Async.cs b/src/SharpCompress/Archives/ArchiveFactory.Information.Async.cs index c4fd4fa2..cb5b763a 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.Information.Async.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.Information.Async.cs @@ -528,6 +528,7 @@ public static partial class ArchiveFactory _ => entries.Aggregate(0L, (total, entry) => total + entry.CompressedSize), }; + [Zomp.SyncMethodGenerator.CreateSyncVersion] private static async ValueTask<( ZipArchiveInformation? Information, long DeferredSizeEntryCount @@ -545,7 +546,9 @@ public static partial class ArchiveFactory long deferredSizeEntryCount = 0; foreach (var entry in entries.OfType()) { +#if !SYNC_ONLY cancellationToken.ThrowIfCancellationRequested(); +#endif var filePart = entry.Parts.OfType().Single(); if (!filePart.HasDeferredSizes) { diff --git a/src/SharpCompress/Archives/ArchiveFactory.Information.cs b/src/SharpCompress/Archives/ArchiveFactory.Information.cs index 264e55c9..01a06ed5 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.Information.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.Information.cs @@ -387,35 +387,6 @@ public static partial class ArchiveFactory ? ArchiveInformationStatus.Complete : ArchiveInformationStatus.Partial; - private static ( - ZipArchiveInformation? Information, - long DeferredSizeEntryCount - ) GetZipInformation(bool isZipArchive, IEnumerable entries) - { - if (!isZipArchive) - { - return (null, 0); - } - - long deferredSizeEntryCount = 0; - foreach (var entry in entries.OfType()) - { - var filePart = entry.Parts.OfType().Single(); - if (!filePart.HasDeferredSizes) - { - continue; - } - - var localHeader = filePart.GetRawLocalHeader(); - if (localHeader.CompressedSize == 0 && localHeader.UncompressedSize == 0) - { - deferredSizeEntryCount++; - } - } - - return (new ZipArchiveInformation(deferredSizeEntryCount > 0), deferredSizeEntryCount); - } - private static (bool IsSolid, long SolidStreamCount) GetSolidInformation( IArchive archive, IReadOnlyCollection entries diff --git a/src/SharpCompress/Common/Zip/SeekableZipFilePart.Async.cs b/src/SharpCompress/Common/Zip/SeekableZipFilePart.Async.cs index f4ff6799..c81c11ed 100644 --- a/src/SharpCompress/Common/Zip/SeekableZipFilePart.Async.cs +++ b/src/SharpCompress/Common/Zip/SeekableZipFilePart.Async.cs @@ -26,6 +26,9 @@ internal partial class SeekableZipFilePart .GetLocalHeaderAsync(BaseStream, _directoryEntryHeader) .ConfigureAwait(false); - internal ValueTask GetRawLocalHeaderAsync() => - _headerFactory.GetRawLocalHeaderAsync(BaseStream, _directoryEntryHeader); + [Zomp.SyncMethodGenerator.CreateSyncVersion] + internal async ValueTask GetRawLocalHeaderAsync() => + await _headerFactory + .GetRawLocalHeaderAsync(BaseStream, _directoryEntryHeader) + .ConfigureAwait(false); } diff --git a/src/SharpCompress/Common/Zip/SeekableZipFilePart.cs b/src/SharpCompress/Common/Zip/SeekableZipFilePart.cs index 166c55b8..baab3218 100644 --- a/src/SharpCompress/Common/Zip/SeekableZipFilePart.cs +++ b/src/SharpCompress/Common/Zip/SeekableZipFilePart.cs @@ -22,9 +22,6 @@ internal partial class SeekableZipFilePart : ZipFilePart _directoryEntryHeader = header; } - internal LocalEntryHeader GetRawLocalHeader() => - _headerFactory.GetRawLocalHeader(BaseStream, _directoryEntryHeader); - internal bool HasDeferredSizes => FlagUtility.HasFlag(_directoryEntryHeader.Flags, HeaderFlags.UsePostDataDescriptor); From 839df5f5d92ce03e21aaa4f2b0aaa83e9b45f746 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Thu, 6 Aug 2026 09:13:22 +0100 Subject: [PATCH 36/40] moved namespaces --- docs/API.md | 2 ++ docs/FORMATS.md | 2 +- docs/USAGE.md | 4 ++++ src/SharpCompress/Archives/ArchiveFactory.Detection.cs | 1 + .../Archives/ArchiveFactory.Information.Async.cs | 1 + src/SharpCompress/Archives/ArchiveFactory.Information.cs | 1 + .../{Archives => Detection}/ArchiveAccessMode.cs | 2 +- src/SharpCompress/{Archives => Detection}/ArchiveDetection.cs | 2 +- .../{Archives => Detection}/ArchiveEncryptionScope.cs | 2 +- .../{Archives => Detection}/ArchiveInformation.cs | 2 +- .../{Archives => Detection}/ArchiveInformationLimitations.cs | 2 +- .../{Archives => Detection}/ArchiveInformationStatus.cs | 2 +- .../{Archives => Detection}/ZipArchiveInformation.cs | 2 +- tests/SharpCompress.Test/ArchiveFactoryTests.cs | 1 + 14 files changed, 18 insertions(+), 8 deletions(-) rename src/SharpCompress/{Archives => Detection}/ArchiveAccessMode.cs (93%) rename src/SharpCompress/{Archives => Detection}/ArchiveDetection.cs (97%) rename src/SharpCompress/{Archives => Detection}/ArchiveEncryptionScope.cs (92%) rename src/SharpCompress/{Archives => Detection}/ArchiveInformation.cs (99%) rename src/SharpCompress/{Archives => Detection}/ArchiveInformationLimitations.cs (94%) rename src/SharpCompress/{Archives => Detection}/ArchiveInformationStatus.cs (91%) rename src/SharpCompress/{Archives => Detection}/ZipArchiveInformation.cs (93%) diff --git a/docs/API.md b/docs/API.md index 1412c789..6fd2fafa 100644 --- a/docs/API.md +++ b/docs/API.md @@ -85,6 +85,8 @@ using (var archive = ArchiveFactory.OpenArchive(parts)) `InspectArchive` enumerates metadata and returns `ArchiveInformation`. It reports `Partial` status for missing volumes or encrypted headers without a password; malformed archives and incorrect passwords throw. ZIP-specific metadata is exposed through `ArchiveInformation.Zip`. +Detection and inspection result types are in the `SharpCompress.Detection` namespace. + The stream overloads of `DetectArchive` and `InspectArchive` preserve the supplied stream's position and leave it open, including when `ReaderOptions.LeaveStreamOpen` is `false`. #### Migrating from `GetArchiveInformation` diff --git a/docs/FORMATS.md b/docs/FORMATS.md index a9309c06..3a2cde50 100644 --- a/docs/FORMATS.md +++ b/docs/FORMATS.md @@ -31,7 +31,7 @@ 4. The 7Zip format doesn't allow for reading as a forward-only stream, so 7Zip read support is only through the Archive API. Writing is supported through SevenZipWriter for non-solid archives with LZMA/LZMA2 and requires a seekable output stream. See [7Zip Format Notes](#7zip-format-notes) for details on async extraction behavior. 5. LZip has no support for extra data like the file name or timestamp. There is a default filename used when looking at the entry Key on the archive. -`ArchiveFactory.DetectArchive(...)` reports which APIs in this table are available through `ArchiveDetection.SupportedApis`. Reader-only formats include Ace, Arc, Arj, and standalone LZW. Compressed TAR wrappers are detected as a TAR container with an outer compression type and support the Reader API, not the Archive API. Use `ArchiveFactory.InspectArchive(...)` when complete archive metadata is required. +`ArchiveFactory.DetectArchive(...)` reports which APIs in this table are available through `SharpCompress.Detection.ArchiveDetection.SupportedApis`. Reader-only formats include Ace, Arc, Arj, and standalone LZW. Compressed TAR wrappers are detected as a TAR container with an outer compression type and support the Reader API, not the Archive API. Use `ArchiveFactory.InspectArchive(...)` when complete archive metadata is required. ### Zip Format Notes diff --git a/docs/USAGE.md b/docs/USAGE.md index 838cd16f..91e6331a 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -230,6 +230,8 @@ using (var archive = ArchiveFactory.OpenArchive("archive.zip")) ### Detect the format and choose the right API ```C# +using SharpCompress.Detection; + var archivePath = "archive.arc"; var detection = ArchiveFactory.DetectArchive(archivePath); if (detection is null) @@ -264,6 +266,8 @@ if (information is not null) `InspectArchive` parses archive metadata, which can require a complete sequential scan for TAR and compressed TAR files. It returns partial information for encrypted headers without a password or missing archive parts; inspect `Status` and `Limitations` before using nullable metadata values. +Detection and inspection result types are in the `SharpCompress.Detection` namespace. + The stream overload preserves the caller's current position and leaves the supplied stream open. ### Open multi-volume archives diff --git a/src/SharpCompress/Archives/ArchiveFactory.Detection.cs b/src/SharpCompress/Archives/ArchiveFactory.Detection.cs index caedc7fd..88168d92 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.Detection.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.Detection.cs @@ -4,6 +4,7 @@ using System.Threading; using System.Threading.Tasks; using SharpCompress.Archives.Tar; using SharpCompress.Common; +using SharpCompress.Detection; using SharpCompress.Factories; using SharpCompress.IO; using SharpCompress.Providers; diff --git a/src/SharpCompress/Archives/ArchiveFactory.Information.Async.cs b/src/SharpCompress/Archives/ArchiveFactory.Information.Async.cs index cb5b763a..1f26bd3e 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.Information.Async.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.Information.Async.cs @@ -11,6 +11,7 @@ using SharpCompress.Common; using SharpCompress.Common.Ace.Headers; using SharpCompress.Common.Rar; using SharpCompress.Common.Zip; +using SharpCompress.Detection; using SharpCompress.IO; using SharpCompress.Readers; diff --git a/src/SharpCompress/Archives/ArchiveFactory.Information.cs b/src/SharpCompress/Archives/ArchiveFactory.Information.cs index 01a06ed5..b89774ef 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.Information.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.Information.cs @@ -11,6 +11,7 @@ using SharpCompress.Common; using SharpCompress.Common.Rar; using SharpCompress.Common.Zip; using SharpCompress.Common.Zip.Headers; +using SharpCompress.Detection; using SharpCompress.IO; using SharpCompress.Readers; using AceMainHeader = SharpCompress.Common.Ace.Headers.AceMainHeader; diff --git a/src/SharpCompress/Archives/ArchiveAccessMode.cs b/src/SharpCompress/Detection/ArchiveAccessMode.cs similarity index 93% rename from src/SharpCompress/Archives/ArchiveAccessMode.cs rename to src/SharpCompress/Detection/ArchiveAccessMode.cs index a86afdb2..8826f2d8 100644 --- a/src/SharpCompress/Archives/ArchiveAccessMode.cs +++ b/src/SharpCompress/Detection/ArchiveAccessMode.cs @@ -1,6 +1,6 @@ using System; -namespace SharpCompress.Archives; +namespace SharpCompress.Detection; /// /// Specifies the APIs available for an archive format. diff --git a/src/SharpCompress/Archives/ArchiveDetection.cs b/src/SharpCompress/Detection/ArchiveDetection.cs similarity index 97% rename from src/SharpCompress/Archives/ArchiveDetection.cs rename to src/SharpCompress/Detection/ArchiveDetection.cs index fe961af8..c01cf4b5 100644 --- a/src/SharpCompress/Archives/ArchiveDetection.cs +++ b/src/SharpCompress/Detection/ArchiveDetection.cs @@ -1,6 +1,6 @@ using SharpCompress.Common; -namespace SharpCompress.Archives; +namespace SharpCompress.Detection; /// /// Identifies an archive format without enumerating its entries. diff --git a/src/SharpCompress/Archives/ArchiveEncryptionScope.cs b/src/SharpCompress/Detection/ArchiveEncryptionScope.cs similarity index 92% rename from src/SharpCompress/Archives/ArchiveEncryptionScope.cs rename to src/SharpCompress/Detection/ArchiveEncryptionScope.cs index 5299e9bf..2e3afe8c 100644 --- a/src/SharpCompress/Archives/ArchiveEncryptionScope.cs +++ b/src/SharpCompress/Detection/ArchiveEncryptionScope.cs @@ -1,6 +1,6 @@ using System; -namespace SharpCompress.Archives; +namespace SharpCompress.Detection; /// /// Identifies which archive data is encrypted. diff --git a/src/SharpCompress/Archives/ArchiveInformation.cs b/src/SharpCompress/Detection/ArchiveInformation.cs similarity index 99% rename from src/SharpCompress/Archives/ArchiveInformation.cs rename to src/SharpCompress/Detection/ArchiveInformation.cs index 68468771..fac49816 100644 --- a/src/SharpCompress/Archives/ArchiveInformation.cs +++ b/src/SharpCompress/Detection/ArchiveInformation.cs @@ -1,4 +1,4 @@ -namespace SharpCompress.Archives; +namespace SharpCompress.Detection; /// /// Contains metadata collected by fully inspecting an archive. diff --git a/src/SharpCompress/Archives/ArchiveInformationLimitations.cs b/src/SharpCompress/Detection/ArchiveInformationLimitations.cs similarity index 94% rename from src/SharpCompress/Archives/ArchiveInformationLimitations.cs rename to src/SharpCompress/Detection/ArchiveInformationLimitations.cs index 26f2cd3a..592f6ed6 100644 --- a/src/SharpCompress/Archives/ArchiveInformationLimitations.cs +++ b/src/SharpCompress/Detection/ArchiveInformationLimitations.cs @@ -1,6 +1,6 @@ using System; -namespace SharpCompress.Archives; +namespace SharpCompress.Detection; /// /// Identifies conditions that prevented complete archive metadata inspection. diff --git a/src/SharpCompress/Archives/ArchiveInformationStatus.cs b/src/SharpCompress/Detection/ArchiveInformationStatus.cs similarity index 91% rename from src/SharpCompress/Archives/ArchiveInformationStatus.cs rename to src/SharpCompress/Detection/ArchiveInformationStatus.cs index dabee58b..5d37f534 100644 --- a/src/SharpCompress/Archives/ArchiveInformationStatus.cs +++ b/src/SharpCompress/Detection/ArchiveInformationStatus.cs @@ -1,4 +1,4 @@ -namespace SharpCompress.Archives; +namespace SharpCompress.Detection; /// /// Describes whether archive metadata could be collected completely. diff --git a/src/SharpCompress/Archives/ZipArchiveInformation.cs b/src/SharpCompress/Detection/ZipArchiveInformation.cs similarity index 93% rename from src/SharpCompress/Archives/ZipArchiveInformation.cs rename to src/SharpCompress/Detection/ZipArchiveInformation.cs index 80bc76cd..63e46e1b 100644 --- a/src/SharpCompress/Archives/ZipArchiveInformation.cs +++ b/src/SharpCompress/Detection/ZipArchiveInformation.cs @@ -1,4 +1,4 @@ -namespace SharpCompress.Archives; +namespace SharpCompress.Detection; /// /// Contains ZIP-specific metadata. diff --git a/tests/SharpCompress.Test/ArchiveFactoryTests.cs b/tests/SharpCompress.Test/ArchiveFactoryTests.cs index 2de784f9..85fbca99 100644 --- a/tests/SharpCompress.Test/ArchiveFactoryTests.cs +++ b/tests/SharpCompress.Test/ArchiveFactoryTests.cs @@ -4,6 +4,7 @@ using System.Text; using System.Threading.Tasks; using SharpCompress.Archives; using SharpCompress.Common; +using SharpCompress.Detection; using SharpCompress.Factories; using SharpCompress.Readers; using SharpCompress.Test.Mocks; From 5908d9a7e653a993909fb436701c461e6ba49cd7 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Thu, 6 Aug 2026 15:17:32 +0100 Subject: [PATCH 37/40] move detection and use more sync method --- .../ArchiveFactory.Detection.Async.cs | 369 ++++++++++++ .../Archives/ArchiveFactory.Detection.cs | 541 ------------------ .../ArchiveFactory.Information.Async.cs | 43 +- .../Archives/ArchiveFactory.Information.cs | 266 --------- src/SharpCompress/Archives/ArchiveFactory.cs | 31 - 5 files changed, 406 insertions(+), 844 deletions(-) create mode 100644 src/SharpCompress/Archives/ArchiveFactory.Detection.Async.cs diff --git a/src/SharpCompress/Archives/ArchiveFactory.Detection.Async.cs b/src/SharpCompress/Archives/ArchiveFactory.Detection.Async.cs new file mode 100644 index 00000000..04cc7886 --- /dev/null +++ b/src/SharpCompress/Archives/ArchiveFactory.Detection.Async.cs @@ -0,0 +1,369 @@ +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using SharpCompress.Archives.Tar; +using SharpCompress.Common; +using SharpCompress.Detection; +using SharpCompress.Factories; +using SharpCompress.IO; +using SharpCompress.Providers; +using SharpCompress.Readers; + +namespace SharpCompress.Archives; + +public static partial class ArchiveFactory +{ + /// + /// Identifies the archive at the given file path without enumerating its entries. + /// + /// Path to the archive file. + /// Cancellation token. + [Zomp.SyncMethodGenerator.CreateSyncVersion] + public static async ValueTask DetectArchiveAsync( + string filePath, + CancellationToken cancellationToken = default + ) => + await DetectArchiveAsync(filePath, ReaderOptions.ForFilePath, cancellationToken) + .ConfigureAwait(false); + + /// + /// Identifies the archive at the given file path without enumerating its entries. + /// + /// Path to the archive file. + /// Options controlling archive detection. + /// Cancellation token. + [Zomp.SyncMethodGenerator.CreateSyncVersion] + public static async ValueTask DetectArchiveAsync( + string filePath, + ReaderOptions? readerOptions, + CancellationToken cancellationToken = default + ) + { + filePath.NotNullOrEmpty(nameof(filePath)); + using Stream stream = File.OpenRead(filePath); + return await DetectArchiveAsync( + stream, + readerOptions ?? ReaderOptions.ForFilePath, + cancellationToken + ) + .ConfigureAwait(false); + } + + /// + /// Identifies the archive in the given stream without enumerating its entries. + /// + /// A readable and seekable stream positioned at the start of the archive. + /// Cancellation token. + [Zomp.SyncMethodGenerator.CreateSyncVersion] + public static async ValueTask DetectArchiveAsync( + Stream stream, + CancellationToken cancellationToken = default + ) => + await DetectArchiveAsync(stream, ReaderOptions.ForExternalStream, cancellationToken) + .ConfigureAwait(false); + + /// + /// Identifies the archive in the given stream without enumerating its entries. + /// + /// A readable and seekable stream positioned at the start of the archive. + /// Options controlling archive detection. + /// Cancellation token. + [Zomp.SyncMethodGenerator.CreateSyncVersion] + public static async ValueTask DetectArchiveAsync( + Stream stream, + ReaderOptions? readerOptions, + CancellationToken cancellationToken = default + ) + { + stream.RequireReadable(); + stream.RequireSeekable(); + + return await TryDetectArchiveAsync( + stream, + readerOptions ?? ReaderOptions.ForExternalStream, + cancellationToken + ) + .ConfigureAwait(false); + } + + internal static ValueTask FindFactoryAsync( + string filePath, + CancellationToken cancellationToken = default + ) + where T : IFactory + { + filePath.NotNullOrEmpty(nameof(filePath)); + return FindFactoryAsync(new FileInfo(filePath), cancellationToken); + } + + internal static async ValueTask FindFactoryAsync( + FileInfo fileInfo, + CancellationToken cancellationToken = default + ) + where T : IFactory + { + fileInfo.NotNull(nameof(fileInfo)); + using Stream stream = fileInfo.OpenRead(); + return await FindFactoryAsync(stream, cancellationToken).ConfigureAwait(false); + } + + [Zomp.SyncMethodGenerator.CreateSyncVersion] + private static async ValueTask FindFactoryAsync( + FileInfo fileInfo, + ReaderOptions readerOptions, + CancellationToken cancellationToken + ) + where T : IFactory + { + fileInfo.NotNull(nameof(fileInfo)); + using Stream stream = fileInfo.OpenRead(); + return await FindFactoryAsync(stream, readerOptions, cancellationToken) + .ConfigureAwait(false); + } + + internal static async ValueTask FindFactoryAsync( + Stream stream, + CancellationToken cancellationToken = default + ) + where T : IFactory + { + stream.RequireReadable(); + stream.RequireSeekable(); + + // Use the shared async detection loop over all factories. If the matched factory + // implements T we return it; otherwise (or if nothing matched) we fall through + // to the same "unsupported format" exception that the original code produced, + // listing the T-typed factories as the hint for the caller. + return await FindFactoryAsync(stream, ReaderOptions.ForExternalStream, cancellationToken) + .ConfigureAwait(false); + } + + [Zomp.SyncMethodGenerator.CreateSyncVersion] + private static async ValueTask FindFactoryAsync( + Stream stream, + ReaderOptions readerOptions, + CancellationToken cancellationToken + ) + where T : IFactory + { + stream.RequireReadable(); + stream.RequireSeekable(); + + var factory = await TryFindFactoryAsync(stream, readerOptions, cancellationToken) + .ConfigureAwait(false); + if (factory is T typedFactory) + { + return typedFactory; + } + + var extensions = string.Join(", ", Factory.Factories.OfType().Select(item => item.Name)); + + throw new ArchiveOperationException( + $"Cannot determine compressed stream type. Supported Archive Formats: {extensions}" + ); + } + + /// + /// Iterates all registered factories and returns the first one whose + /// recognises the stream, or . + /// Stream position is restored to its value at entry on both success and failure. + /// + [Zomp.SyncMethodGenerator.CreateSyncVersion] + private static async ValueTask TryFindFactoryAsync( + Stream stream, + ReaderOptions readerOptions, + CancellationToken cancellationToken + ) + { + var startPosition = stream.Position; + + try + { + foreach (var factory in Factory.Factories) + { + stream.Seek(startPosition, SeekOrigin.Begin); + var isArchive = await factory + .IsArchiveAsync(stream, readerOptions, cancellationToken) + .ConfigureAwait(false); + + if (isArchive) + { + stream.Seek(startPosition, SeekOrigin.Begin); + if ( + await IsCompressedTarAsync( + stream, + factory, + readerOptions, + cancellationToken + ) + .ConfigureAwait(false) + ) + { + continue; + } + + return factory; + } + } + + return null; + } + finally + { + stream.Seek(startPosition, SeekOrigin.Begin); + } + } + + [Zomp.SyncMethodGenerator.CreateSyncVersion] + private static async ValueTask IsCompressedTarAsync( + Stream stream, + IFactory factory, + ReaderOptions readerOptions, + CancellationToken cancellationToken + ) => + GetCompressedTarType(factory) is { } compressionType + && await IsCompressedTarAsync(stream, readerOptions, compressionType, cancellationToken) + .ConfigureAwait(false); + + [Zomp.SyncMethodGenerator.CreateSyncVersion] + private static async ValueTask IsCompressedTarAsync( + Stream stream, + ReaderOptions readerOptions, + CompressionType compressionType, + CancellationToken cancellationToken + ) + { + using var nonDisposingStream = SharpCompressStream.CreateNonDisposing(stream); + var testStream = + compressionType == CompressionType.GZip + ? await readerOptions + .Providers.CreateDecompressStreamAsync( + compressionType, + nonDisposingStream, + CompressionContext + .FromStream(nonDisposingStream) + .WithReaderOptions(readerOptions), + cancellationToken + ) + .ConfigureAwait(false) + : await readerOptions + .Providers.CreateDecompressStreamAsync( + compressionType, + nonDisposingStream, + cancellationToken + ) + .ConfigureAwait(false); + + try + { + return await TarArchive + .IsTarFileAsync(testStream, cancellationToken) + .ConfigureAwait(false); + } + finally + { + DisposeProbeStream(testStream); + } + } + + [Zomp.SyncMethodGenerator.CreateSyncVersion] + private static async ValueTask TryDetectArchiveAsync( + Stream stream, + ReaderOptions readerOptions, + CancellationToken cancellationToken + ) + { + var startPosition = stream.Position; + + try + { + foreach (var factory in Factory.Factories) + { + stream.Seek(startPosition, SeekOrigin.Begin); + if ( + !await factory + .IsArchiveAsync(stream, readerOptions, cancellationToken) + .ConfigureAwait(false) + ) + { + continue; + } + + if (GetCompressedTarType(factory) is { } compressionType) + { + stream.Seek(startPosition, SeekOrigin.Begin); + if ( + await IsCompressedTarAsync( + stream, + readerOptions, + compressionType, + cancellationToken + ) + .ConfigureAwait(false) + ) + { + return CreateCompressedTarDetection(compressionType); + } + } + + return CreateDetection(factory); + } + + var compressedTarType = await TryDetectCompressedTarAsync( + stream, + readerOptions, + startPosition, + cancellationToken + ) + .ConfigureAwait(false); + return compressedTarType is { } value ? CreateCompressedTarDetection(value) : null; + } + finally + { + stream.Seek(startPosition, SeekOrigin.Begin); + } + } + + [Zomp.SyncMethodGenerator.CreateSyncVersion] + private static async ValueTask TryDetectCompressedTarAsync( + Stream stream, + ReaderOptions readerOptions, + long startPosition, + CancellationToken cancellationToken + ) + { + foreach (var wrapper in TarWrapper.Wrappers) + { +#if !SYNC_ONLY + cancellationToken.ThrowIfCancellationRequested(); +#endif + if (wrapper.CompressionType == CompressionType.None) + { + continue; + } + + stream.Seek(startPosition, SeekOrigin.Begin); + if (!await wrapper.IsMatchAsync(stream, cancellationToken).ConfigureAwait(false)) + { + continue; + } + + stream.Seek(startPosition, SeekOrigin.Begin); + if ( + await IsCompressedTarAsync( + stream, + readerOptions, + wrapper.CompressionType, + cancellationToken + ) + .ConfigureAwait(false) + ) + { + return wrapper.CompressionType; + } + } + + return null; + } +} diff --git a/src/SharpCompress/Archives/ArchiveFactory.Detection.cs b/src/SharpCompress/Archives/ArchiveFactory.Detection.cs index 88168d92..85721dbf 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.Detection.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.Detection.cs @@ -1,391 +1,13 @@ using System.IO; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using SharpCompress.Archives.Tar; using SharpCompress.Common; using SharpCompress.Detection; using SharpCompress.Factories; -using SharpCompress.IO; -using SharpCompress.Providers; using SharpCompress.Readers; namespace SharpCompress.Archives; public static partial class ArchiveFactory { - /// - /// Identifies the archive at the given file path without enumerating its entries. - /// - /// Path to the archive file. - /// Cancellation token. - public static async ValueTask DetectArchiveAsync( - string filePath, - CancellationToken cancellationToken = default - ) => - await DetectArchiveAsync(filePath, ReaderOptions.ForFilePath, cancellationToken) - .ConfigureAwait(false); - - /// - /// Identifies the archive at the given file path without enumerating its entries. - /// - /// Path to the archive file. - /// Options controlling archive detection. - /// Cancellation token. - public static async ValueTask DetectArchiveAsync( - string filePath, - ReaderOptions? readerOptions, - CancellationToken cancellationToken = default - ) - { - filePath.NotNullOrEmpty(nameof(filePath)); - using Stream stream = File.OpenRead(filePath); - return await DetectArchiveAsync( - stream, - readerOptions ?? ReaderOptions.ForFilePath, - cancellationToken - ) - .ConfigureAwait(false); - } - - /// - /// Identifies the archive in the given stream without enumerating its entries. - /// - /// A readable and seekable stream positioned at the start of the archive. - /// Cancellation token. - public static async ValueTask DetectArchiveAsync( - Stream stream, - CancellationToken cancellationToken = default - ) => - await DetectArchiveAsync(stream, ReaderOptions.ForExternalStream, cancellationToken) - .ConfigureAwait(false); - - /// - /// Identifies the archive in the given stream without enumerating its entries. - /// - /// A readable and seekable stream positioned at the start of the archive. - /// Options controlling archive detection. - /// Cancellation token. - public static async ValueTask DetectArchiveAsync( - Stream stream, - ReaderOptions? readerOptions, - CancellationToken cancellationToken = default - ) - { - stream.RequireReadable(); - stream.RequireSeekable(); - - return await TryDetectArchiveAsync( - stream, - readerOptions ?? ReaderOptions.ForExternalStream, - cancellationToken - ) - .ConfigureAwait(false); - } - - internal static ValueTask FindFactoryAsync( - string filePath, - CancellationToken cancellationToken = default - ) - where T : IFactory - { - filePath.NotNullOrEmpty(nameof(filePath)); - return FindFactoryAsync(new FileInfo(filePath), cancellationToken); - } - - internal static async ValueTask FindFactoryAsync( - FileInfo fileInfo, - CancellationToken cancellationToken = default - ) - where T : IFactory - { - fileInfo.NotNull(nameof(fileInfo)); - using Stream stream = fileInfo.OpenRead(); - return await FindFactoryAsync(stream, cancellationToken).ConfigureAwait(false); - } - - private static async ValueTask FindFactoryAsync( - FileInfo fileInfo, - ReaderOptions readerOptions, - CancellationToken cancellationToken - ) - where T : IFactory - { - fileInfo.NotNull(nameof(fileInfo)); - using Stream stream = fileInfo.OpenRead(); - return await FindFactoryAsync(stream, readerOptions, cancellationToken) - .ConfigureAwait(false); - } - - internal static async ValueTask FindFactoryAsync( - Stream stream, - CancellationToken cancellationToken = default - ) - where T : IFactory - { - stream.RequireReadable(); - stream.RequireSeekable(); - - // Use the shared async detection loop over all factories. If the matched factory - // implements T we return it; otherwise (or if nothing matched) we fall through - // to the same "unsupported format" exception that the original code produced, - // listing the T-typed factories as the hint for the caller. - return await FindFactoryAsync(stream, ReaderOptions.ForExternalStream, cancellationToken) - .ConfigureAwait(false); - } - - private static async ValueTask FindFactoryAsync( - Stream stream, - ReaderOptions readerOptions, - CancellationToken cancellationToken - ) - where T : IFactory - { - stream.RequireReadable(); - stream.RequireSeekable(); - - var factory = await TryFindFactoryAsync(stream, readerOptions, cancellationToken) - .ConfigureAwait(false); - if (factory is T typedFactory) - { - return typedFactory; - } - - var extensions = string.Join(", ", Factory.Factories.OfType().Select(item => item.Name)); - - throw new ArchiveOperationException( - $"Cannot determine compressed stream type. Supported Archive Formats: {extensions}" - ); - } - - /// - /// Async counterpart of the synchronous factory detection path. - /// Iterates all registered factories and returns the first one whose - /// recognises the stream, or . - /// Stream position is restored to its value at entry on both success and failure. - /// - private static async ValueTask TryFindFactoryAsync( - Stream stream, - ReaderOptions readerOptions, - CancellationToken cancellationToken - ) - { - var startPosition = stream.Position; - - try - { - foreach (var factory in Factory.Factories) - { - stream.Seek(startPosition, SeekOrigin.Begin); - var isArchive = await factory - .IsArchiveAsync(stream, readerOptions, cancellationToken) - .ConfigureAwait(false); - - if (isArchive) - { - stream.Seek(startPosition, SeekOrigin.Begin); - if ( - await IsCompressedTarAsync( - stream, - factory, - readerOptions, - cancellationToken - ) - .ConfigureAwait(false) - ) - { - continue; - } - - return factory; - } - } - - return null; - } - finally - { - stream.Seek(startPosition, SeekOrigin.Begin); - } - } - - /// - /// Identifies the archive at the given file path without enumerating its entries. - /// - /// Path to the archive file. - public static ArchiveDetection? DetectArchive(string filePath) => - DetectArchive(filePath, ReaderOptions.ForFilePath); - - /// - /// Identifies the archive at the given file path without enumerating its entries. - /// - /// Path to the archive file. - /// Options controlling archive detection. - public static ArchiveDetection? DetectArchive(string filePath, ReaderOptions? readerOptions) - { - filePath.NotNullOrEmpty(nameof(filePath)); - using Stream stream = File.OpenRead(filePath); - return DetectArchive(stream, readerOptions ?? ReaderOptions.ForFilePath); - } - - /// - /// Identifies the archive in the given stream without enumerating its entries. - /// - /// A readable and seekable stream positioned at the start of the archive. - public static ArchiveDetection? DetectArchive(Stream stream) => - DetectArchive(stream, ReaderOptions.ForExternalStream); - - /// - /// Identifies the archive in the given stream without enumerating its entries. - /// - /// A readable and seekable stream positioned at the start of the archive. - /// Options controlling archive detection. - public static ArchiveDetection? DetectArchive(Stream stream, ReaderOptions? readerOptions) - { - stream.RequireReadable(); - stream.RequireSeekable(); - - return TryDetectArchive(stream, readerOptions ?? ReaderOptions.ForExternalStream); - } - - /// - /// Iterates all registered factories and returns the first one whose - /// recognises the stream, or . - /// Stream position is restored to its value at entry on both success and failure. - /// - /// - /// This is the shared, seekable-stream detection core used by - /// , , - /// and . - /// - /// uses a separate code path - /// based on rewindable buffering, which supports - /// non-seekable streams and is therefore not unified with this helper. - /// - /// - private static IFactory? TryFindFactory(Stream stream) => - TryFindFactory(stream, ReaderOptions.ForExternalStream); - - private static IFactory? TryFindFactory(Stream stream, ReaderOptions readerOptions) - { - var startPosition = stream.Position; - - try - { - foreach (var factory in Factory.Factories) - { - stream.Seek(startPosition, SeekOrigin.Begin); - var isArchive = factory.IsArchive(stream, readerOptions); - - if (isArchive) - { - stream.Seek(startPosition, SeekOrigin.Begin); - if (IsCompressedTar(stream, factory, readerOptions)) - { - continue; - } - - return factory; - } - } - - return null; - } - finally - { - stream.Seek(startPosition, SeekOrigin.Begin); - } - } - - private static bool IsCompressedTar( - Stream stream, - IFactory factory, - ReaderOptions readerOptions - ) => - GetCompressedTarType(factory) is { } compressionType - && IsCompressedTar(stream, readerOptions, compressionType); - - private static bool IsCompressedTar( - Stream stream, - ReaderOptions readerOptions, - CompressionType compressionType - ) - { - using var nonDisposingStream = SharpCompressStream.CreateNonDisposing(stream); - var testStream = - compressionType == CompressionType.GZip - ? readerOptions.Providers.CreateDecompressStream( - compressionType, - nonDisposingStream, - CompressionContext - .FromStream(nonDisposingStream) - .WithReaderOptions(readerOptions) - ) - : readerOptions.Providers.CreateDecompressStream( - compressionType, - nonDisposingStream - ); - - try - { - return TarArchive.IsTarFile(testStream); - } - finally - { - DisposeProbeStream(testStream); - } - } - - private static async ValueTask IsCompressedTarAsync( - Stream stream, - IFactory factory, - ReaderOptions readerOptions, - CancellationToken cancellationToken - ) => - GetCompressedTarType(factory) is { } compressionType - && await IsCompressedTarAsync(stream, readerOptions, compressionType, cancellationToken) - .ConfigureAwait(false); - - private static async ValueTask IsCompressedTarAsync( - Stream stream, - ReaderOptions readerOptions, - CompressionType compressionType, - CancellationToken cancellationToken - ) - { - using var nonDisposingStream = SharpCompressStream.CreateNonDisposing(stream); - var testStream = - compressionType == CompressionType.GZip - ? await readerOptions - .Providers.CreateDecompressStreamAsync( - compressionType, - nonDisposingStream, - CompressionContext - .FromStream(nonDisposingStream) - .WithReaderOptions(readerOptions), - cancellationToken - ) - .ConfigureAwait(false) - : await readerOptions - .Providers.CreateDecompressStreamAsync( - compressionType, - nonDisposingStream, - cancellationToken - ) - .ConfigureAwait(false); - - try - { - return await TarArchive - .IsTarFileAsync(testStream, cancellationToken) - .ConfigureAwait(false); - } - finally - { - DisposeProbeStream(testStream); - } - } - private static void DisposeProbeStream(Stream stream) { try @@ -408,169 +30,6 @@ public static partial class ArchiveFactory _ => null, }; - private static ArchiveDetection? TryDetectArchive(Stream stream, ReaderOptions readerOptions) - { - var startPosition = stream.Position; - - try - { - foreach (var factory in Factory.Factories) - { - stream.Seek(startPosition, SeekOrigin.Begin); - if (!factory.IsArchive(stream, readerOptions)) - { - continue; - } - - if (GetCompressedTarType(factory) is { } compressionType) - { - stream.Seek(startPosition, SeekOrigin.Begin); - if (IsCompressedTar(stream, readerOptions, compressionType)) - { - return CreateCompressedTarDetection(compressionType); - } - } - - return CreateDetection(factory); - } - - return - TryDetectCompressedTar(stream, readerOptions, startPosition) - is { } compressedTarType - ? CreateCompressedTarDetection(compressedTarType) - : null; - } - finally - { - stream.Seek(startPosition, SeekOrigin.Begin); - } - } - - private static async ValueTask TryDetectArchiveAsync( - Stream stream, - ReaderOptions readerOptions, - CancellationToken cancellationToken - ) - { - var startPosition = stream.Position; - - try - { - foreach (var factory in Factory.Factories) - { - stream.Seek(startPosition, SeekOrigin.Begin); - if ( - !await factory - .IsArchiveAsync(stream, readerOptions, cancellationToken) - .ConfigureAwait(false) - ) - { - continue; - } - - if (GetCompressedTarType(factory) is { } compressionType) - { - stream.Seek(startPosition, SeekOrigin.Begin); - if ( - await IsCompressedTarAsync( - stream, - readerOptions, - compressionType, - cancellationToken - ) - .ConfigureAwait(false) - ) - { - return CreateCompressedTarDetection(compressionType); - } - } - - return CreateDetection(factory); - } - - var compressedTarType = await TryDetectCompressedTarAsync( - stream, - readerOptions, - startPosition, - cancellationToken - ) - .ConfigureAwait(false); - return compressedTarType is { } value ? CreateCompressedTarDetection(value) : null; - } - finally - { - stream.Seek(startPosition, SeekOrigin.Begin); - } - } - - private static CompressionType? TryDetectCompressedTar( - Stream stream, - ReaderOptions readerOptions, - long startPosition - ) - { - foreach (var wrapper in TarWrapper.Wrappers) - { - if (wrapper.CompressionType == CompressionType.None) - { - continue; - } - - stream.Seek(startPosition, SeekOrigin.Begin); - if (!wrapper.IsMatch(stream)) - { - continue; - } - - stream.Seek(startPosition, SeekOrigin.Begin); - if (IsCompressedTar(stream, readerOptions, wrapper.CompressionType)) - { - return wrapper.CompressionType; - } - } - - return null; - } - - private static async ValueTask TryDetectCompressedTarAsync( - Stream stream, - ReaderOptions readerOptions, - long startPosition, - CancellationToken cancellationToken - ) - { - foreach (var wrapper in TarWrapper.Wrappers) - { - cancellationToken.ThrowIfCancellationRequested(); - if (wrapper.CompressionType == CompressionType.None) - { - continue; - } - - stream.Seek(startPosition, SeekOrigin.Begin); - if (!await wrapper.IsMatchAsync(stream, cancellationToken).ConfigureAwait(false)) - { - continue; - } - - stream.Seek(startPosition, SeekOrigin.Begin); - if ( - await IsCompressedTarAsync( - stream, - readerOptions, - wrapper.CompressionType, - cancellationToken - ) - .ConfigureAwait(false) - ) - { - return wrapper.CompressionType; - } - } - - return null; - } - private static ArchiveDetection CreateDetection(IFactory factory) { var supportedApis = ArchiveAccessMode.None; diff --git a/src/SharpCompress/Archives/ArchiveFactory.Information.Async.cs b/src/SharpCompress/Archives/ArchiveFactory.Information.Async.cs index 1f26bd3e..8ac74f5d 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.Information.Async.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.Information.Async.cs @@ -20,11 +20,12 @@ namespace SharpCompress.Archives; public static partial class ArchiveFactory { /// - /// Asynchronously collects metadata for the archive at the given file path. + /// Collects metadata for the archive at the given file path. /// /// Path to the archive file. /// Cancellation token. /// Archive metadata, or when the file is not a supported archive. + [Zomp.SyncMethodGenerator.CreateSyncVersion] public static async ValueTask InspectArchiveAsync( string filePath, CancellationToken cancellationToken = default @@ -33,12 +34,13 @@ public static partial class ArchiveFactory .ConfigureAwait(false); /// - /// Asynchronously collects metadata for the archive at the given file path. + /// Collects metadata for the archive at the given file path. /// /// Path to the archive file. /// Options controlling archive inspection. /// Cancellation token. /// Archive metadata, or when the file is not a supported archive. + [Zomp.SyncMethodGenerator.CreateSyncVersion] public static async ValueTask InspectArchiveAsync( string filePath, ReaderOptions? readerOptions, @@ -68,11 +70,12 @@ public static partial class ArchiveFactory } /// - /// Asynchronously collects metadata for the archive in the given stream. + /// Collects metadata for the archive in the given stream. /// /// A readable and seekable stream positioned at the start of the archive. /// Cancellation token. /// Archive metadata, or when the stream is not a supported archive. + [Zomp.SyncMethodGenerator.CreateSyncVersion] public static async ValueTask InspectArchiveAsync( Stream stream, CancellationToken cancellationToken = default @@ -81,13 +84,14 @@ public static partial class ArchiveFactory .ConfigureAwait(false); /// - /// Asynchronously collects metadata for the archive in the given stream. + /// Collects metadata for the archive in the given stream. /// /// A readable and seekable stream positioned at the start of the archive. /// Options controlling archive inspection. /// Cancellation token. /// Archive metadata, or when the stream is not a supported archive. /// The supplied stream remains open and is restored to its original position. + [Zomp.SyncMethodGenerator.CreateSyncVersion] public static async ValueTask InspectArchiveAsync( Stream stream, ReaderOptions? readerOptions, @@ -96,7 +100,9 @@ public static partial class ArchiveFactory { stream.RequireReadable(); stream.RequireSeekable(); +#if !SYNC_ONLY cancellationToken.ThrowIfCancellationRequested(); +#endif var options = readerOptions ?? ReaderOptions.ForExternalStream; var startPosition = stream.Position; @@ -119,6 +125,10 @@ public static partial class ArchiveFactory if ((detection.SupportedApis & ArchiveAccessMode.Archive) != 0) { +#if SYNC_ONLY + using var archive = OpenArchive(archiveStream, inspectionOptions); + return InspectOpenedArchive(archive, detection, physicalSize, 1); +#else await using var archive = await OpenAsyncArchive( archiveStream, inspectionOptions, @@ -133,8 +143,14 @@ public static partial class ArchiveFactory cancellationToken ) .ConfigureAwait(false); +#endif } +#if SYNC_ONLY + var aceHeader = ReadAceHeader(archiveStream, detection, inspectionOptions); + using var reader = ReaderFactory.OpenReader(archiveStream, inspectionOptions); + return InspectOpenedReader(reader, detection, physicalSize, 1, aceHeader); +#else var aceHeader = await ReadAceHeaderAsync( archiveStream, detection, @@ -154,6 +170,7 @@ public static partial class ArchiveFactory cancellationToken ) .ConfigureAwait(false); +#endif } catch (CryptographicException) when (string.IsNullOrEmpty(options.Password)) { @@ -198,12 +215,13 @@ public static partial class ArchiveFactory } /// - /// Asynchronously collects metadata for an archive opened from multiple files. + /// Collects metadata for an archive opened from multiple files. /// /// Archive source files in archive order. /// Options controlling archive inspection. /// Cancellation token. /// Archive metadata, or when the files are not a supported archive. + [Zomp.SyncMethodGenerator.CreateSyncVersion] public static async ValueTask InspectArchiveAsync( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, @@ -242,6 +260,10 @@ public static partial class ArchiveFactory var physicalSize = GetPhysicalSize(fileInfos); try { +#if SYNC_ONLY + using var archive = OpenArchive(fileInfos, options); + return InspectOpenedArchive(archive, detection, physicalSize, fileInfos.Count); +#else await using var archive = await OpenAsyncArchive(fileInfos, options, cancellationToken) .ConfigureAwait(false); return await InspectOpenedArchiveAsync( @@ -252,6 +274,7 @@ public static partial class ArchiveFactory cancellationToken ) .ConfigureAwait(false); +#endif } catch (CryptographicException) when (string.IsNullOrEmpty(options.Password)) { @@ -265,12 +288,13 @@ public static partial class ArchiveFactory } /// - /// Asynchronously collects metadata for an archive opened from multiple streams. + /// Collects metadata for an archive opened from multiple streams. /// /// Archive source streams in archive order. /// Options controlling archive inspection. /// Cancellation token. /// Archive metadata, or when the streams are not a supported archive. + [Zomp.SyncMethodGenerator.CreateSyncVersion] public static async ValueTask InspectArchiveAsync( IReadOnlyList streams, ReaderOptions? readerOptions = null, @@ -317,6 +341,10 @@ public static partial class ArchiveFactory archiveStreams.AddRange( streams.Skip(1).Select(stream => new ArchiveOffsetStream(stream)) ); +#if SYNC_ONLY + using var archive = OpenArchive(archiveStreams, inspectionOptions); + return InspectOpenedArchive(archive, detection, physicalSize, streams.Count); +#else await using var archive = await OpenAsyncArchive( archiveStreams, inspectionOptions, @@ -331,6 +359,7 @@ public static partial class ArchiveFactory cancellationToken ) .ConfigureAwait(false); +#endif } catch (CryptographicException) when (string.IsNullOrEmpty(options.Password)) { @@ -466,6 +495,7 @@ public static partial class ArchiveFactory ); } + [Zomp.SyncMethodGenerator.CreateSyncVersion] private static async ValueTask ReadAceHeaderAsync( Stream stream, ArchiveDetection detection, @@ -566,6 +596,7 @@ public static partial class ArchiveFactory return (new ZipArchiveInformation(deferredSizeEntryCount > 0), deferredSizeEntryCount); } + [Zomp.SyncMethodGenerator.CreateSyncVersion] private static async ValueTask RedetectArchiveAsync( Stream stream, long startPosition, diff --git a/src/SharpCompress/Archives/ArchiveFactory.Information.cs b/src/SharpCompress/Archives/ArchiveFactory.Information.cs index b89774ef..4b12fe8d 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.Information.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.Information.cs @@ -2,8 +2,6 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Threading; -using System.Threading.Tasks; using SharpCompress.Archives.Rar; using SharpCompress.Archives.SevenZip; using SharpCompress.Archives.Zip; @@ -12,7 +10,6 @@ using SharpCompress.Common.Rar; using SharpCompress.Common.Zip; using SharpCompress.Common.Zip.Headers; using SharpCompress.Detection; -using SharpCompress.IO; using SharpCompress.Readers; using AceMainHeader = SharpCompress.Common.Ace.Headers.AceMainHeader; @@ -20,236 +17,6 @@ namespace SharpCompress.Archives; public static partial class ArchiveFactory { - /// - /// Collects metadata for the archive at the given file path. - /// - /// Path to the archive file. - /// Archive metadata, or when the file is not a supported archive. - public static ArchiveInformation? InspectArchive(string filePath) => - InspectArchive(filePath, ReaderOptions.ForFilePath); - - /// - /// Collects metadata for the archive at the given file path. - /// - /// Path to the archive file. - /// Options controlling archive inspection. - /// Archive metadata, or when the file is not a supported archive. - public static ArchiveInformation? InspectArchive(string filePath, ReaderOptions? readerOptions) - { - filePath.NotNullOrEmpty(nameof(filePath)); - var options = readerOptions ?? ReaderOptions.ForFilePath; - var detection = DetectArchive(filePath, options); - if (detection is null) - { - return null; - } - if ((detection.SupportedApis & ArchiveAccessMode.Archive) != 0) - { - var fileInfos = GetArchiveFileParts(new FileInfo(filePath), options); - if (fileInfos.Length > 1) - { - return InspectArchive(fileInfos, options); - } - } - - using Stream stream = File.OpenRead(filePath); - return InspectArchive(stream, options); - } - - /// - /// Collects metadata for the archive in the given stream. - /// - /// A readable and seekable stream positioned at the start of the archive. - /// Archive metadata, or when the stream is not a supported archive. - public static ArchiveInformation? InspectArchive(Stream stream) => - InspectArchive(stream, ReaderOptions.ForExternalStream); - - /// - /// Collects metadata for the archive in the given stream. - /// - /// A readable and seekable stream positioned at the start of the archive. - /// Options controlling archive inspection. - /// Archive metadata, or when the stream is not a supported archive. - /// The supplied stream remains open and is restored to its original position. - public static ArchiveInformation? InspectArchive(Stream stream, ReaderOptions? readerOptions) - { - stream.RequireReadable(); - stream.RequireSeekable(); - - var options = readerOptions ?? ReaderOptions.ForExternalStream; - var startPosition = stream.Position; - var physicalSize = GetPhysicalSize(stream, startPosition); - - try - { - using var archiveStream = new ArchiveOffsetStream(stream); - var inspectionOptions = options with { LeaveStreamOpen = true }; - var detection = TryDetectArchive(archiveStream, inspectionOptions); - if (detection is null) - { - return null; - } - - if ((detection.SupportedApis & ArchiveAccessMode.Archive) != 0) - { - using var archive = OpenArchive(archiveStream, inspectionOptions); - return InspectOpenedArchive(archive, detection, physicalSize, 1); - } - - var aceHeader = ReadAceHeader(archiveStream, detection, inspectionOptions); - using var reader = ReaderFactory.OpenReader(archiveStream, inspectionOptions); - return InspectOpenedReader(reader, detection, physicalSize, 1, aceHeader); - } - catch (CryptographicException) when (string.IsNullOrEmpty(options.Password)) - { - var detection = RedetectArchive(stream, startPosition, options); - return detection is null - ? null - : CreatePartialInformation( - detection, - physicalSize, - 1, - ArchiveInformationLimitations.EncryptedHeaders - ); - } - catch (MultipartStreamRequiredException) - { - var detection = RedetectArchive(stream, startPosition, options); - return detection is null - ? null - : CreatePartialInformation( - detection, - physicalSize, - 1, - ArchiveInformationLimitations.MissingVolumes - ); - } - finally - { - stream.Seek(startPosition, SeekOrigin.Begin); - } - } - - /// - /// Collects metadata for an archive opened from multiple files. - /// - /// Archive source files in archive order. - /// Options controlling archive inspection. - /// Archive metadata, or when the files are not a supported archive. - public static ArchiveInformation? InspectArchive( - IReadOnlyList fileInfos, - ReaderOptions? readerOptions = null - ) - { - fileInfos.NotNull(nameof(fileInfos)); - if (fileInfos.Count == 0) - { - throw new ArchiveOperationException("No files to inspect"); - } - if (fileInfos.Count == 1) - { - return InspectArchive(fileInfos[0].FullName, readerOptions); - } - - var options = readerOptions ?? ReaderOptions.ForFilePath; - var detection = DetectArchive(fileInfos[0].FullName, options); - if (detection is null) - { - return null; - } - if ((detection.SupportedApis & ArchiveAccessMode.Archive) == 0) - { - throw new NotSupportedException( - "Inspecting multiple source files is supported only for formats with an Archive API." - ); - } - - var physicalSize = GetPhysicalSize(fileInfos); - try - { - using var archive = OpenArchive(fileInfos, options); - return InspectOpenedArchive(archive, detection, physicalSize, fileInfos.Count); - } - catch (CryptographicException) when (string.IsNullOrEmpty(options.Password)) - { - return CreatePartialInformation( - detection, - physicalSize, - fileInfos.Count, - ArchiveInformationLimitations.EncryptedHeaders - ); - } - } - - /// - /// Collects metadata for an archive opened from multiple streams. - /// - /// Archive source streams in archive order. - /// Options controlling archive inspection. - /// Archive metadata, or when the streams are not a supported archive. - public static ArchiveInformation? InspectArchive( - IReadOnlyList streams, - ReaderOptions? readerOptions = null - ) - { - streams.NotNull(nameof(streams)); - if (streams.Count == 0) - { - throw new ArchiveOperationException("No streams to inspect"); - } - if (streams.Count == 1) - { - return InspectArchive(streams[0], readerOptions); - } - - var options = readerOptions ?? ReaderOptions.ForExternalStream; - var startPositions = streams.Select(stream => stream.Position).ToArray(); - var physicalSize = GetPhysicalSize(streams, startPositions); - - try - { - using var firstArchiveStream = new ArchiveOffsetStream(streams[0]); - var inspectionOptions = options with { LeaveStreamOpen = true }; - var detection = TryDetectArchive(firstArchiveStream, inspectionOptions); - if (detection is null) - { - return null; - } - if ((detection.SupportedApis & ArchiveAccessMode.Archive) == 0) - { - throw new NotSupportedException( - "Inspecting multiple source streams is supported only for formats with an Archive API." - ); - } - - var archiveStreams = new List { firstArchiveStream }; - archiveStreams.AddRange( - streams.Skip(1).Select(stream => new ArchiveOffsetStream(stream)) - ); - using var archive = OpenArchive(archiveStreams, inspectionOptions); - return InspectOpenedArchive(archive, detection, physicalSize, streams.Count); - } - catch (CryptographicException) when (string.IsNullOrEmpty(options.Password)) - { - var detection = RedetectArchive(streams[0], startPositions[0], options); - return detection is null - ? null - : CreatePartialInformation( - detection, - physicalSize, - streams.Count, - ArchiveInformationLimitations.EncryptedHeaders - ); - } - finally - { - for (var i = 0; i < streams.Count; i++) - { - streams[i].Seek(startPositions[i], SeekOrigin.Begin); - } - } - } - private static ArchiveInformation InspectOpenedArchive( IArchive archive, ArchiveDetection detection, @@ -331,28 +98,6 @@ public static partial class ArchiveFactory ); } - private static AceMainHeader? ReadAceHeader( - Stream stream, - ArchiveDetection detection, - ReaderOptions options - ) - { - if (detection.ContainerType != ArchiveType.Ace) - { - return null; - } - - try - { - stream.Position = 0; - return new AceMainHeader(options.ArchiveEncoding).Read(stream) as AceMainHeader; - } - finally - { - stream.Position = 0; - } - } - private static ArchiveInformation CreatePartialInformation( ArchiveDetection detection, long? physicalSize, @@ -486,17 +231,6 @@ public static partial class ArchiveFactory } } - private static ArchiveDetection? RedetectArchive( - Stream stream, - long startPosition, - ReaderOptions options - ) - { - stream.Seek(startPosition, SeekOrigin.Begin); - using var archiveStream = new ArchiveOffsetStream(stream); - return TryDetectArchive(archiveStream, options); - } - private static FileInfo[] GetArchiveFileParts(FileInfo firstPart, ReaderOptions options) { using Stream stream = firstPart.OpenRead(); diff --git a/src/SharpCompress/Archives/ArchiveFactory.cs b/src/SharpCompress/Archives/ArchiveFactory.cs index 63b63954..efff5604 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.cs @@ -123,37 +123,6 @@ public static partial class ArchiveFactory public static T FindFactory(Stream stream) where T : IFactory => FindFactory(stream, ReaderOptions.ForExternalStream); - private static T FindFactory(FileInfo fileInfo, ReaderOptions readerOptions) - where T : IFactory - { - fileInfo.NotNull(nameof(fileInfo)); - using Stream stream = fileInfo.OpenRead(); - return FindFactory(stream, readerOptions); - } - - private static T FindFactory(Stream stream, ReaderOptions readerOptions) - where T : IFactory - { - stream.RequireReadable(); - stream.RequireSeekable(); - - // Use the shared detection loop over all factories. If the matched factory - // implements T we return it; otherwise (or if nothing matched) we fall through - // to the same "unsupported format" exception that the original code produced, - // listing the T-typed factories as the hint for the caller. - var factory = TryFindFactory(stream, readerOptions); - if (factory is T typedFactory) - { - return typedFactory; - } - - var extensions = string.Join(", ", Factory.Factories.OfType().Select(item => item.Name)); - - throw new ArchiveOperationException( - $"Cannot determine compressed stream type. Supported Archive Formats: {extensions}" - ); - } - public static bool IsArchive(string filePath, out ArchiveType? type) { return IsArchive(filePath, ReaderOptions.ForFilePath, out type); From be0ed362d2cf6c63509064d6ba52693a026b20b4 Mon Sep 17 00:00:00 2001 From: Iain Date: Tue, 22 Sep 2026 01:40:55 +0100 Subject: [PATCH 38/40] Fix the exception catches and ArchiveType.BZip2 in the API quick reference --- docs/API.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/docs/API.md b/docs/API.md index 6fd2fafa..7da04787 100644 --- a/docs/API.md +++ b/docs/API.md @@ -10,7 +10,7 @@ Quick reference for commonly used SharpCompress APIs. // Auto-detect format using (var reader = ReaderFactory.OpenReader(stream)) { - // Works with Zip, Tar, GZip, Rar, 7Zip, etc. + // Works with Zip, Tar, GZip, Rar, etc. Reading 7Zip needs the Archive API } // Specific format - Archive API @@ -256,7 +256,7 @@ using (var reader = ReaderFactory.OpenReader(stream)) { while (reader.MoveToNextEntry()) { - IArchiveEntry entry = reader.Entry; + IEntry entry = reader.Entry; if (!entry.IsDirectory) { @@ -611,7 +611,6 @@ using (var writer = WriterFactory.OpenWriter(stream, ArchiveType.Tar, Compressio ArchiveType.Zip ArchiveType.Tar ArchiveType.GZip -ArchiveType.BZip2 ArchiveType.Rar ArchiveType.SevenZip ArchiveType.Arc @@ -635,11 +634,11 @@ try archive.WriteToDirectory(@"C:\output"); } } -catch (PasswordRequiredException) +catch (CryptographicException) { Console.WriteLine("Password required"); } -catch (InvalidArchiveException) +catch (ArchiveException) { Console.WriteLine("Archive is invalid"); } From 0d38d18c589a07a70f199de6540bee6407af27a0 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 24 Sep 2026 09:39:49 +0000 Subject: [PATCH 39/40] Bump AwesomeAssertions and 5 others Bumps AwesomeAssertions from 9.5.0 to 9.6.0 Bumps Bullseye from 6.1.0 to 6.2.0 Bumps Microsoft.NET.Test.Sdk from 18.6.0 to 18.10.1 Bumps Microsoft.SourceLink.GitHub from 10.0.300 to 10.0.401 Bumps SimpleExec from 13.0.0 to 13.1.0 Bumps Zomp.SyncMethodGenerator from 2.0.42 to 2.0.56 --- updated-dependencies: - dependency-name: AwesomeAssertions dependency-version: 9.6.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: all-dependencies - dependency-name: Bullseye dependency-version: 6.2.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: all-dependencies - dependency-name: Microsoft.NET.Test.Sdk dependency-version: 18.10.1 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: all-dependencies - dependency-name: Microsoft.SourceLink.GitHub dependency-version: 10.0.401 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: all-dependencies - dependency-name: SimpleExec dependency-version: 13.1.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: all-dependencies - dependency-name: Zomp.SyncMethodGenerator dependency-version: 2.0.56 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: all-dependencies ... Signed-off-by: dependabot[bot] --- Directory.Packages.props | 12 +- build/packages.lock.json | 44 +- src/SharpCompress/packages.lock.json | 501 +----------------- .../SharpCompress.AotSmoke/packages.lock.json | 32 +- .../packages.lock.json | 32 +- tests/SharpCompress.Test/packages.lock.json | 401 ++------------ 6 files changed, 108 insertions(+), 914 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index a9438d7b..18d207e9 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -1,24 +1,24 @@ - - + + - + - + - + - + diff --git a/build/packages.lock.json b/build/packages.lock.json index 0251f8f5..cf4e18fe 100644 --- a/build/packages.lock.json +++ b/build/packages.lock.json @@ -4,9 +4,9 @@ "net10.0": { "Bullseye": { "type": "Direct", - "requested": "[6.1.0, )", - "resolved": "6.1.0", - "contentHash": "fltnAJDe0BEX5eymXGUq+il2rSUA0pHqUonNDRH2TrvRu8SkU17mYG0IVpdmG2ibtfhdjNrv4CuTCxHOwcozCA==" + "requested": "[6.2.0, )", + "resolved": "6.2.0", + "contentHash": "YWQxe+pvudGSzW1QqnRUlr9XV0xtlNvMxorVaFPqoMH8qAYNxniMQKGrPSiQkgEy0ckn/TAq98E+DIwTSR7vbQ==" }, "Glob": { "type": "Direct", @@ -25,13 +25,13 @@ }, "Microsoft.SourceLink.GitHub": { "type": "Direct", - "requested": "[10.0.300, )", - "resolved": "10.0.300", - "contentHash": "QzCtLkXVb3l4IxcpvJCbzUwMLihAmLN6vVLjQGSzYSF8d2dvXxqJAZk83RV3gYnp2egz8jRMgSR2woY3vOahTA==", + "requested": "[10.0.401, )", + "resolved": "10.0.401", + "contentHash": "LGmlwgP1Cx37JEWzyjS0o1/+xs/s/e3E2TBSuogI5ePA/9L0pfIfeYX0k5in7Bfcw8Nn2y2sG9jXxydTjiR2Fg==", "dependencies": { - "Microsoft.Build.Tasks.Git": "10.0.300", - "Microsoft.SourceLink.Common": "10.0.300", - "System.IO.Hashing": "10.0.8" + "Microsoft.Build.Tasks.Git": "10.0.401", + "Microsoft.SourceLink.Common": "10.0.401", + "System.IO.Hashing": "10.0.12" } }, "Microsoft.VisualStudio.Threading.Analyzers": { @@ -48,22 +48,22 @@ }, "SimpleExec": { "type": "Direct", - "requested": "[13.0.0, )", - "resolved": "13.0.0", - "contentHash": "zcCR1pupa1wI1VqBULRiQKeHKKZOuJhi/K+4V5oO+rHJZlaOD53ViFo1c3PavDoMAfSn/FAXGAWpPoF57rwhYg==" + "requested": "[13.1.0, )", + "resolved": "13.1.0", + "contentHash": "Q/xUWjrB98Aq+guCJtGFAHmnH00J3Ux7Q/gbnVkuh1U4FHZ2/3PgBHTcZuuhs2cSv5pNgZeHEmGwLgSjjbFlrw==" }, "Zomp.SyncMethodGenerator": { "type": "Direct", - "requested": "[2.0.42, )", - "resolved": "2.0.42", - "contentHash": "kf2PvmD9N5xIzsdf6vy5tk2w0hlD7+Kbd3w/wFmgozgHTKJlh2RNiS+Jx+4d1AtgyJVBD0CO0nofbThu5zTsgQ==" + "requested": "[2.0.56, )", + "resolved": "2.0.56", + "contentHash": "P1iMA1meu0uX8YcPWhkFnpzV3e+QFDDYMU6hMNyofS+pBlLgeVV7QevBmJmJ/mrxA1jujo3cqNaO1IapdfjOlA==" }, "Microsoft.Build.Tasks.Git": { "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "P0kaQwVZx4xIUe2FtrLyBadYNXuAljttJUPvjBYRuHhPE8L77L42KakLDkaADRiUrGspoLcMwayjrbQhYTr0zA==", + "resolved": "10.0.401", + "contentHash": "ZYctNuT10V9IYyCFydy63DXx0ggZQuynuzQOdLvW62dPgzjIz7f0ISEP75RGiq1jFQh8p6TmGSqxeQZQ87LCig==", "dependencies": { - "System.IO.Hashing": "10.0.8" + "System.IO.Hashing": "10.0.12" } }, "Microsoft.NETFramework.ReferenceAssemblies.net461": { @@ -73,13 +73,13 @@ }, "Microsoft.SourceLink.Common": { "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "0jlkXaUGjYlWTIVPve5MftjKHnT3SlAtq9BCLV4J9IjdPrxV/+4rMlBSjfr1khG8/GC6KGojjola8E1VvWF0qQ==" + "resolved": "10.0.401", + "contentHash": "u3rLxIwi/9MqDFaWGE/QQgLR1NBEzLOW2lv5+9OrZPDBYIAmFdYSWCWrR1ufpXWOqFn+x02TgKropl/oDuHmgA==" }, "System.IO.Hashing": { "type": "Transitive", - "resolved": "10.0.8", - "contentHash": "+dJsbPJ3FyUbTZNplFj0RCKePFizmv6ewDV46JE9q/IVH4c3xTCftHfHelLsAKf0jryIPqgMb5GpS0x7TAY3mg==" + "resolved": "10.0.12", + "contentHash": "jDix4bBMYnpZdSPcnY+KDV6ik3SRMzpMKby/bZl/XUwIiflwRNAFZ0oOl61R/pSaveIJ8t1gs2BUlrGsPs/bcg==" } } } diff --git a/src/SharpCompress/packages.lock.json b/src/SharpCompress/packages.lock.json index e0072e82..812aad1d 100644 --- a/src/SharpCompress/packages.lock.json +++ b/src/SharpCompress/packages.lock.json @@ -1,341 +1,6 @@ { "version": 2, "dependencies": { - ".NETFramework,Version=v4.8": { - "Microsoft.Bcl.AsyncInterfaces": { - "type": "Direct", - "requested": "[8.0.0, )", - "resolved": "8.0.0", - "contentHash": "3WA9q9yVqJp222P3x1wYIGDAkpjAku0TMUaaQV22g6L67AI0LdOIrVS7Ht2vJfLHGSPVuqN94vIr15qn+HEkHw==", - "dependencies": { - "System.Threading.Tasks.Extensions": "4.5.4" - } - }, - "Microsoft.NETFramework.ReferenceAssemblies": { - "type": "Direct", - "requested": "[1.0.3, )", - "resolved": "1.0.3", - "contentHash": "vUc9Npcs14QsyOD01tnv/m8sQUnGTGOw1BCmKcv77LBJY7OxhJ+zJF7UD/sCL3lYNFuqmQEVlkfS4Quif6FyYg==", - "dependencies": { - "Microsoft.NETFramework.ReferenceAssemblies.net48": "1.0.3" - } - }, - "Microsoft.SourceLink.GitHub": { - "type": "Direct", - "requested": "[10.0.300, )", - "resolved": "10.0.300", - "contentHash": "QzCtLkXVb3l4IxcpvJCbzUwMLihAmLN6vVLjQGSzYSF8d2dvXxqJAZk83RV3gYnp2egz8jRMgSR2woY3vOahTA==", - "dependencies": { - "Microsoft.Build.Tasks.Git": "10.0.300", - "Microsoft.SourceLink.Common": "10.0.300", - "System.IO.Hashing": "10.0.8" - } - }, - "Microsoft.VisualStudio.Threading.Analyzers": { - "type": "Direct", - "requested": "[17.14.15, )", - "resolved": "17.14.15", - "contentHash": "mXQPJsbuUD2ydq4/ffd8h8tSOFCXec+2xJOVNCvXjuMOq/+5EKHq3D2m2MC2+nUaXeFMSt66VS/J4HdKBixgcw==" - }, - "PolySharp": { - "type": "Direct", - "requested": "[1.16.0, )", - "resolved": "1.16.0", - "contentHash": "3kdIIceBPumwjw279FuiVMfVENT2cGASXJgcigdySsbX2dJB8ofUgG6i47yqF/k1qu6fvNR3csrSekZPviR6kQ==" - }, - "System.Text.Encoding.CodePages": { - "type": "Direct", - "requested": "[8.0.0, )", - "resolved": "8.0.0", - "contentHash": "OZIsVplFGaVY90G2SbpgU7EnCoOO5pw1t4ic21dBF3/1omrJFpAGoNAVpPyMVOC90/hvgkGG3VFqR13YgZMQfg==", - "dependencies": { - "System.Memory": "4.5.5", - "System.Runtime.CompilerServices.Unsafe": "6.0.0" - } - }, - "Zomp.SyncMethodGenerator": { - "type": "Direct", - "requested": "[2.0.42, )", - "resolved": "2.0.42", - "contentHash": "kf2PvmD9N5xIzsdf6vy5tk2w0hlD7+Kbd3w/wFmgozgHTKJlh2RNiS+Jx+4d1AtgyJVBD0CO0nofbThu5zTsgQ==" - }, - "Microsoft.Build.Tasks.Git": { - "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "P0kaQwVZx4xIUe2FtrLyBadYNXuAljttJUPvjBYRuHhPE8L77L42KakLDkaADRiUrGspoLcMwayjrbQhYTr0zA==", - "dependencies": { - "System.IO.Hashing": "10.0.8" - } - }, - "Microsoft.NETFramework.ReferenceAssemblies.net48": { - "type": "Transitive", - "resolved": "1.0.3", - "contentHash": "zMk4D+9zyiEWByyQ7oPImPN/Jhpj166Ky0Nlla4eXlNL8hI/BtSJsgR8Inldd4NNpIAH3oh8yym0W2DrhXdSLQ==" - }, - "Microsoft.SourceLink.Common": { - "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "0jlkXaUGjYlWTIVPve5MftjKHnT3SlAtq9BCLV4J9IjdPrxV/+4rMlBSjfr1khG8/GC6KGojjola8E1VvWF0qQ==" - }, - "System.Buffers": { - "type": "Transitive", - "resolved": "4.6.1", - "contentHash": "N8GXpmiLMtljq7gwvyS+1QvKT/W2J8sNAvx+HVg4NGmsG/H+2k/y9QI23auLJRterrzCiDH+IWAw4V/GPwsMlw==" - }, - "System.IO.Hashing": { - "type": "Transitive", - "resolved": "10.0.8", - "contentHash": "+dJsbPJ3FyUbTZNplFj0RCKePFizmv6ewDV46JE9q/IVH4c3xTCftHfHelLsAKf0jryIPqgMb5GpS0x7TAY3mg==", - "dependencies": { - "System.Buffers": "4.6.1", - "System.Memory": "4.6.3" - } - }, - "System.Memory": { - "type": "Transitive", - "resolved": "4.6.3", - "contentHash": "qdcDOgnFZY40+Q9876JUHnlHu7bosOHX8XISRoH94fwk6hgaeQGSgfZd8srWRZNt5bV9ZW2TljcegDNxsf+96A==", - "dependencies": { - "System.Buffers": "4.6.1", - "System.Numerics.Vectors": "4.6.1", - "System.Runtime.CompilerServices.Unsafe": "6.1.2" - } - }, - "System.Numerics.Vectors": { - "type": "Transitive", - "resolved": "4.6.1", - "contentHash": "sQxefTnhagrhoq2ReR0D/6K0zJcr9Hrd6kikeXsA1I8kOCboTavcUC4r7TSfpKFeE163uMuxZcyfO1mGO3EN8Q==" - }, - "System.Runtime.CompilerServices.Unsafe": { - "type": "Transitive", - "resolved": "6.1.2", - "contentHash": "2hBr6zdbIBTDE3EhK7NSVNdX58uTK6iHW/P/Axmm9sl1xoGSLqDvMtpecn226TNwHByFokYwJmt/aQQNlO5CRw==" - }, - "System.Threading.Tasks.Extensions": { - "type": "Transitive", - "resolved": "4.5.4", - "contentHash": "zteT+G8xuGu6mS+mzDzYXbzS7rd3K6Fjb9RiZlYlJPam2/hU7JCBZBVEcywNuR+oZ1ncTvc/cq0faRr3P01OVg==", - "dependencies": { - "System.Runtime.CompilerServices.Unsafe": "4.5.3" - } - } - }, - ".NETStandard,Version=v2.0": { - "Microsoft.Bcl.AsyncInterfaces": { - "type": "Direct", - "requested": "[8.0.0, )", - "resolved": "8.0.0", - "contentHash": "3WA9q9yVqJp222P3x1wYIGDAkpjAku0TMUaaQV22g6L67AI0LdOIrVS7Ht2vJfLHGSPVuqN94vIr15qn+HEkHw==", - "dependencies": { - "System.Threading.Tasks.Extensions": "4.5.4" - } - }, - "Microsoft.NETFramework.ReferenceAssemblies": { - "type": "Direct", - "requested": "[1.0.3, )", - "resolved": "1.0.3", - "contentHash": "vUc9Npcs14QsyOD01tnv/m8sQUnGTGOw1BCmKcv77LBJY7OxhJ+zJF7UD/sCL3lYNFuqmQEVlkfS4Quif6FyYg==", - "dependencies": { - "Microsoft.NETFramework.ReferenceAssemblies.net461": "1.0.3" - } - }, - "Microsoft.SourceLink.GitHub": { - "type": "Direct", - "requested": "[10.0.300, )", - "resolved": "10.0.300", - "contentHash": "QzCtLkXVb3l4IxcpvJCbzUwMLihAmLN6vVLjQGSzYSF8d2dvXxqJAZk83RV3gYnp2egz8jRMgSR2woY3vOahTA==", - "dependencies": { - "Microsoft.Build.Tasks.Git": "10.0.300", - "Microsoft.SourceLink.Common": "10.0.300", - "System.IO.Hashing": "10.0.8" - } - }, - "Microsoft.VisualStudio.Threading.Analyzers": { - "type": "Direct", - "requested": "[17.14.15, )", - "resolved": "17.14.15", - "contentHash": "mXQPJsbuUD2ydq4/ffd8h8tSOFCXec+2xJOVNCvXjuMOq/+5EKHq3D2m2MC2+nUaXeFMSt66VS/J4HdKBixgcw==" - }, - "NETStandard.Library": { - "type": "Direct", - "requested": "[2.0.3, )", - "resolved": "2.0.3", - "contentHash": "st47PosZSHrjECdjeIzZQbzivYBJFv6P2nv4cj2ypdI204DO+vZ7l5raGMiX4eXMJ53RfOIg+/s4DHVZ54Nu2A==", - "dependencies": { - "Microsoft.NETCore.Platforms": "1.1.0" - } - }, - "PolySharp": { - "type": "Direct", - "requested": "[1.16.0, )", - "resolved": "1.16.0", - "contentHash": "3kdIIceBPumwjw279FuiVMfVENT2cGASXJgcigdySsbX2dJB8ofUgG6i47yqF/k1qu6fvNR3csrSekZPviR6kQ==" - }, - "System.Text.Encoding.CodePages": { - "type": "Direct", - "requested": "[8.0.0, )", - "resolved": "8.0.0", - "contentHash": "OZIsVplFGaVY90G2SbpgU7EnCoOO5pw1t4ic21dBF3/1omrJFpAGoNAVpPyMVOC90/hvgkGG3VFqR13YgZMQfg==", - "dependencies": { - "System.Memory": "4.5.5", - "System.Runtime.CompilerServices.Unsafe": "6.0.0" - } - }, - "Zomp.SyncMethodGenerator": { - "type": "Direct", - "requested": "[2.0.42, )", - "resolved": "2.0.42", - "contentHash": "kf2PvmD9N5xIzsdf6vy5tk2w0hlD7+Kbd3w/wFmgozgHTKJlh2RNiS+Jx+4d1AtgyJVBD0CO0nofbThu5zTsgQ==" - }, - "Microsoft.Build.Tasks.Git": { - "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "P0kaQwVZx4xIUe2FtrLyBadYNXuAljttJUPvjBYRuHhPE8L77L42KakLDkaADRiUrGspoLcMwayjrbQhYTr0zA==", - "dependencies": { - "System.IO.Hashing": "10.0.8" - } - }, - "Microsoft.NETCore.Platforms": { - "type": "Transitive", - "resolved": "1.1.0", - "contentHash": "kz0PEW2lhqygehI/d6XsPCQzD7ff7gUJaVGPVETX611eadGsA3A877GdSlU0LRVMCTH/+P3o2iDTak+S08V2+A==" - }, - "Microsoft.NETFramework.ReferenceAssemblies.net461": { - "type": "Transitive", - "resolved": "1.0.3", - "contentHash": "AmOJZwCqnOCNp6PPcf9joyogScWLtwy0M1WkqfEQ0M9nYwyDD7EX9ZjscKS5iYnyvteX7kzSKFCKt9I9dXA6mA==" - }, - "Microsoft.SourceLink.Common": { - "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "0jlkXaUGjYlWTIVPve5MftjKHnT3SlAtq9BCLV4J9IjdPrxV/+4rMlBSjfr1khG8/GC6KGojjola8E1VvWF0qQ==" - }, - "System.Buffers": { - "type": "Transitive", - "resolved": "4.6.1", - "contentHash": "N8GXpmiLMtljq7gwvyS+1QvKT/W2J8sNAvx+HVg4NGmsG/H+2k/y9QI23auLJRterrzCiDH+IWAw4V/GPwsMlw==" - }, - "System.IO.Hashing": { - "type": "Transitive", - "resolved": "10.0.8", - "contentHash": "+dJsbPJ3FyUbTZNplFj0RCKePFizmv6ewDV46JE9q/IVH4c3xTCftHfHelLsAKf0jryIPqgMb5GpS0x7TAY3mg==", - "dependencies": { - "System.Buffers": "4.6.1", - "System.Memory": "4.6.3" - } - }, - "System.Memory": { - "type": "Transitive", - "resolved": "4.6.3", - "contentHash": "qdcDOgnFZY40+Q9876JUHnlHu7bosOHX8XISRoH94fwk6hgaeQGSgfZd8srWRZNt5bV9ZW2TljcegDNxsf+96A==", - "dependencies": { - "System.Buffers": "4.6.1", - "System.Numerics.Vectors": "4.6.1", - "System.Runtime.CompilerServices.Unsafe": "6.1.2" - } - }, - "System.Numerics.Vectors": { - "type": "Transitive", - "resolved": "4.6.1", - "contentHash": "sQxefTnhagrhoq2ReR0D/6K0zJcr9Hrd6kikeXsA1I8kOCboTavcUC4r7TSfpKFeE163uMuxZcyfO1mGO3EN8Q==" - }, - "System.Runtime.CompilerServices.Unsafe": { - "type": "Transitive", - "resolved": "6.1.2", - "contentHash": "2hBr6zdbIBTDE3EhK7NSVNdX58uTK6iHW/P/Axmm9sl1xoGSLqDvMtpecn226TNwHByFokYwJmt/aQQNlO5CRw==" - }, - "System.Threading.Tasks.Extensions": { - "type": "Transitive", - "resolved": "4.5.4", - "contentHash": "zteT+G8xuGu6mS+mzDzYXbzS7rd3K6Fjb9RiZlYlJPam2/hU7JCBZBVEcywNuR+oZ1ncTvc/cq0faRr3P01OVg==", - "dependencies": { - "System.Runtime.CompilerServices.Unsafe": "4.5.3" - } - } - }, - ".NETStandard,Version=v2.1": { - "Microsoft.Bcl.AsyncInterfaces": { - "type": "Direct", - "requested": "[8.0.0, )", - "resolved": "8.0.0", - "contentHash": "3WA9q9yVqJp222P3x1wYIGDAkpjAku0TMUaaQV22g6L67AI0LdOIrVS7Ht2vJfLHGSPVuqN94vIr15qn+HEkHw==" - }, - "Microsoft.NETFramework.ReferenceAssemblies": { - "type": "Direct", - "requested": "[1.0.3, )", - "resolved": "1.0.3", - "contentHash": "vUc9Npcs14QsyOD01tnv/m8sQUnGTGOw1BCmKcv77LBJY7OxhJ+zJF7UD/sCL3lYNFuqmQEVlkfS4Quif6FyYg==", - "dependencies": { - "Microsoft.NETFramework.ReferenceAssemblies.net461": "1.0.3" - } - }, - "Microsoft.SourceLink.GitHub": { - "type": "Direct", - "requested": "[10.0.300, )", - "resolved": "10.0.300", - "contentHash": "QzCtLkXVb3l4IxcpvJCbzUwMLihAmLN6vVLjQGSzYSF8d2dvXxqJAZk83RV3gYnp2egz8jRMgSR2woY3vOahTA==", - "dependencies": { - "Microsoft.Build.Tasks.Git": "10.0.300", - "Microsoft.SourceLink.Common": "10.0.300", - "System.IO.Hashing": "10.0.8" - } - }, - "Microsoft.VisualStudio.Threading.Analyzers": { - "type": "Direct", - "requested": "[17.14.15, )", - "resolved": "17.14.15", - "contentHash": "mXQPJsbuUD2ydq4/ffd8h8tSOFCXec+2xJOVNCvXjuMOq/+5EKHq3D2m2MC2+nUaXeFMSt66VS/J4HdKBixgcw==" - }, - "PolySharp": { - "type": "Direct", - "requested": "[1.16.0, )", - "resolved": "1.16.0", - "contentHash": "3kdIIceBPumwjw279FuiVMfVENT2cGASXJgcigdySsbX2dJB8ofUgG6i47yqF/k1qu6fvNR3csrSekZPviR6kQ==" - }, - "System.Text.Encoding.CodePages": { - "type": "Direct", - "requested": "[8.0.0, )", - "resolved": "8.0.0", - "contentHash": "OZIsVplFGaVY90G2SbpgU7EnCoOO5pw1t4ic21dBF3/1omrJFpAGoNAVpPyMVOC90/hvgkGG3VFqR13YgZMQfg==", - "dependencies": { - "System.Runtime.CompilerServices.Unsafe": "6.0.0" - } - }, - "Zomp.SyncMethodGenerator": { - "type": "Direct", - "requested": "[2.0.42, )", - "resolved": "2.0.42", - "contentHash": "kf2PvmD9N5xIzsdf6vy5tk2w0hlD7+Kbd3w/wFmgozgHTKJlh2RNiS+Jx+4d1AtgyJVBD0CO0nofbThu5zTsgQ==" - }, - "Microsoft.Build.Tasks.Git": { - "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "P0kaQwVZx4xIUe2FtrLyBadYNXuAljttJUPvjBYRuHhPE8L77L42KakLDkaADRiUrGspoLcMwayjrbQhYTr0zA==", - "dependencies": { - "System.IO.Hashing": "10.0.8" - } - }, - "Microsoft.NETFramework.ReferenceAssemblies.net461": { - "type": "Transitive", - "resolved": "1.0.3", - "contentHash": "AmOJZwCqnOCNp6PPcf9joyogScWLtwy0M1WkqfEQ0M9nYwyDD7EX9ZjscKS5iYnyvteX7kzSKFCKt9I9dXA6mA==" - }, - "Microsoft.SourceLink.Common": { - "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "0jlkXaUGjYlWTIVPve5MftjKHnT3SlAtq9BCLV4J9IjdPrxV/+4rMlBSjfr1khG8/GC6KGojjola8E1VvWF0qQ==" - }, - "System.IO.Hashing": { - "type": "Transitive", - "resolved": "10.0.8", - "contentHash": "+dJsbPJ3FyUbTZNplFj0RCKePFizmv6ewDV46JE9q/IVH4c3xTCftHfHelLsAKf0jryIPqgMb5GpS0x7TAY3mg==" - }, - "System.Runtime.CompilerServices.Unsafe": { - "type": "Transitive", - "resolved": "6.0.0", - "contentHash": "/iUeP3tq1S0XdNNoMz5C9twLSrM/TH+qElHkXWaPvuNOt+99G75NrV0OS2EqHx5wMN7popYjpc8oTjC1y16DLg==" - } - }, "net10.0": { "Microsoft.NET.ILLink.Tasks": { "type": "Direct", @@ -354,13 +19,13 @@ }, "Microsoft.SourceLink.GitHub": { "type": "Direct", - "requested": "[10.0.300, )", - "resolved": "10.0.300", - "contentHash": "QzCtLkXVb3l4IxcpvJCbzUwMLihAmLN6vVLjQGSzYSF8d2dvXxqJAZk83RV3gYnp2egz8jRMgSR2woY3vOahTA==", + "requested": "[10.0.401, )", + "resolved": "10.0.401", + "contentHash": "LGmlwgP1Cx37JEWzyjS0o1/+xs/s/e3E2TBSuogI5ePA/9L0pfIfeYX0k5in7Bfcw8Nn2y2sG9jXxydTjiR2Fg==", "dependencies": { - "Microsoft.Build.Tasks.Git": "10.0.300", - "Microsoft.SourceLink.Common": "10.0.300", - "System.IO.Hashing": "10.0.8" + "Microsoft.Build.Tasks.Git": "10.0.401", + "Microsoft.SourceLink.Common": "10.0.401", + "System.IO.Hashing": "10.0.12" } }, "Microsoft.VisualStudio.Threading.Analyzers": { @@ -377,16 +42,16 @@ }, "Zomp.SyncMethodGenerator": { "type": "Direct", - "requested": "[2.0.42, )", - "resolved": "2.0.42", - "contentHash": "kf2PvmD9N5xIzsdf6vy5tk2w0hlD7+Kbd3w/wFmgozgHTKJlh2RNiS+Jx+4d1AtgyJVBD0CO0nofbThu5zTsgQ==" + "requested": "[2.0.56, )", + "resolved": "2.0.56", + "contentHash": "P1iMA1meu0uX8YcPWhkFnpzV3e+QFDDYMU6hMNyofS+pBlLgeVV7QevBmJmJ/mrxA1jujo3cqNaO1IapdfjOlA==" }, "Microsoft.Build.Tasks.Git": { "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "P0kaQwVZx4xIUe2FtrLyBadYNXuAljttJUPvjBYRuHhPE8L77L42KakLDkaADRiUrGspoLcMwayjrbQhYTr0zA==", + "resolved": "10.0.401", + "contentHash": "ZYctNuT10V9IYyCFydy63DXx0ggZQuynuzQOdLvW62dPgzjIz7f0ISEP75RGiq1jFQh8p6TmGSqxeQZQ87LCig==", "dependencies": { - "System.IO.Hashing": "10.0.8" + "System.IO.Hashing": "10.0.12" } }, "Microsoft.NETFramework.ReferenceAssemblies.net461": { @@ -396,146 +61,14 @@ }, "Microsoft.SourceLink.Common": { "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "0jlkXaUGjYlWTIVPve5MftjKHnT3SlAtq9BCLV4J9IjdPrxV/+4rMlBSjfr1khG8/GC6KGojjola8E1VvWF0qQ==" + "resolved": "10.0.401", + "contentHash": "u3rLxIwi/9MqDFaWGE/QQgLR1NBEzLOW2lv5+9OrZPDBYIAmFdYSWCWrR1ufpXWOqFn+x02TgKropl/oDuHmgA==" }, "System.IO.Hashing": { "type": "Transitive", - "resolved": "10.0.8", - "contentHash": "+dJsbPJ3FyUbTZNplFj0RCKePFizmv6ewDV46JE9q/IVH4c3xTCftHfHelLsAKf0jryIPqgMb5GpS0x7TAY3mg==" - } - }, - "net6.0": { - "Microsoft.NETFramework.ReferenceAssemblies": { - "type": "Direct", - "requested": "[1.0.3, )", - "resolved": "1.0.3", - "contentHash": "vUc9Npcs14QsyOD01tnv/m8sQUnGTGOw1BCmKcv77LBJY7OxhJ+zJF7UD/sCL3lYNFuqmQEVlkfS4Quif6FyYg==", - "dependencies": { - "Microsoft.NETFramework.ReferenceAssemblies.net461": "1.0.3" - } - }, - "Microsoft.SourceLink.GitHub": { - "type": "Direct", - "requested": "[10.0.300, )", - "resolved": "10.0.300", - "contentHash": "QzCtLkXVb3l4IxcpvJCbzUwMLihAmLN6vVLjQGSzYSF8d2dvXxqJAZk83RV3gYnp2egz8jRMgSR2woY3vOahTA==", - "dependencies": { - "Microsoft.Build.Tasks.Git": "10.0.300", - "Microsoft.SourceLink.Common": "10.0.300", - "System.IO.Hashing": "10.0.8" - } - }, - "Microsoft.VisualStudio.Threading.Analyzers": { - "type": "Direct", - "requested": "[17.14.15, )", - "resolved": "17.14.15", - "contentHash": "mXQPJsbuUD2ydq4/ffd8h8tSOFCXec+2xJOVNCvXjuMOq/+5EKHq3D2m2MC2+nUaXeFMSt66VS/J4HdKBixgcw==" - }, - "PolySharp": { - "type": "Direct", - "requested": "[1.16.0, )", - "resolved": "1.16.0", - "contentHash": "3kdIIceBPumwjw279FuiVMfVENT2cGASXJgcigdySsbX2dJB8ofUgG6i47yqF/k1qu6fvNR3csrSekZPviR6kQ==" - }, - "Zomp.SyncMethodGenerator": { - "type": "Direct", - "requested": "[2.0.42, )", - "resolved": "2.0.42", - "contentHash": "kf2PvmD9N5xIzsdf6vy5tk2w0hlD7+Kbd3w/wFmgozgHTKJlh2RNiS+Jx+4d1AtgyJVBD0CO0nofbThu5zTsgQ==" - }, - "Microsoft.Build.Tasks.Git": { - "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "P0kaQwVZx4xIUe2FtrLyBadYNXuAljttJUPvjBYRuHhPE8L77L42KakLDkaADRiUrGspoLcMwayjrbQhYTr0zA==", - "dependencies": { - "System.IO.Hashing": "10.0.8" - } - }, - "Microsoft.NETFramework.ReferenceAssemblies.net461": { - "type": "Transitive", - "resolved": "1.0.3", - "contentHash": "AmOJZwCqnOCNp6PPcf9joyogScWLtwy0M1WkqfEQ0M9nYwyDD7EX9ZjscKS5iYnyvteX7kzSKFCKt9I9dXA6mA==" - }, - "Microsoft.SourceLink.Common": { - "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "0jlkXaUGjYlWTIVPve5MftjKHnT3SlAtq9BCLV4J9IjdPrxV/+4rMlBSjfr1khG8/GC6KGojjola8E1VvWF0qQ==" - }, - "System.IO.Hashing": { - "type": "Transitive", - "resolved": "10.0.8", - "contentHash": "+dJsbPJ3FyUbTZNplFj0RCKePFizmv6ewDV46JE9q/IVH4c3xTCftHfHelLsAKf0jryIPqgMb5GpS0x7TAY3mg==" - } - }, - "net8.0": { - "Microsoft.NET.ILLink.Tasks": { - "type": "Direct", - "requested": "[8.0.29, )", - "resolved": "8.0.29", - "contentHash": "HSBTfrkIZijz8z3ybLRKB7E8rHk4QQufFwpHa9fc5CMIgRhRzdn4mBGmlyXZqaueiMPtuJcnjresGvSTfaW8Mg==" - }, - "Microsoft.NETFramework.ReferenceAssemblies": { - "type": "Direct", - "requested": "[1.0.3, )", - "resolved": "1.0.3", - "contentHash": "vUc9Npcs14QsyOD01tnv/m8sQUnGTGOw1BCmKcv77LBJY7OxhJ+zJF7UD/sCL3lYNFuqmQEVlkfS4Quif6FyYg==", - "dependencies": { - "Microsoft.NETFramework.ReferenceAssemblies.net461": "1.0.3" - } - }, - "Microsoft.SourceLink.GitHub": { - "type": "Direct", - "requested": "[10.0.300, )", - "resolved": "10.0.300", - "contentHash": "QzCtLkXVb3l4IxcpvJCbzUwMLihAmLN6vVLjQGSzYSF8d2dvXxqJAZk83RV3gYnp2egz8jRMgSR2woY3vOahTA==", - "dependencies": { - "Microsoft.Build.Tasks.Git": "10.0.300", - "Microsoft.SourceLink.Common": "10.0.300", - "System.IO.Hashing": "10.0.8" - } - }, - "Microsoft.VisualStudio.Threading.Analyzers": { - "type": "Direct", - "requested": "[17.14.15, )", - "resolved": "17.14.15", - "contentHash": "mXQPJsbuUD2ydq4/ffd8h8tSOFCXec+2xJOVNCvXjuMOq/+5EKHq3D2m2MC2+nUaXeFMSt66VS/J4HdKBixgcw==" - }, - "PolySharp": { - "type": "Direct", - "requested": "[1.16.0, )", - "resolved": "1.16.0", - "contentHash": "3kdIIceBPumwjw279FuiVMfVENT2cGASXJgcigdySsbX2dJB8ofUgG6i47yqF/k1qu6fvNR3csrSekZPviR6kQ==" - }, - "Zomp.SyncMethodGenerator": { - "type": "Direct", - "requested": "[2.0.42, )", - "resolved": "2.0.42", - "contentHash": "kf2PvmD9N5xIzsdf6vy5tk2w0hlD7+Kbd3w/wFmgozgHTKJlh2RNiS+Jx+4d1AtgyJVBD0CO0nofbThu5zTsgQ==" - }, - "Microsoft.Build.Tasks.Git": { - "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "P0kaQwVZx4xIUe2FtrLyBadYNXuAljttJUPvjBYRuHhPE8L77L42KakLDkaADRiUrGspoLcMwayjrbQhYTr0zA==", - "dependencies": { - "System.IO.Hashing": "10.0.8" - } - }, - "Microsoft.NETFramework.ReferenceAssemblies.net461": { - "type": "Transitive", - "resolved": "1.0.3", - "contentHash": "AmOJZwCqnOCNp6PPcf9joyogScWLtwy0M1WkqfEQ0M9nYwyDD7EX9ZjscKS5iYnyvteX7kzSKFCKt9I9dXA6mA==" - }, - "Microsoft.SourceLink.Common": { - "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "0jlkXaUGjYlWTIVPve5MftjKHnT3SlAtq9BCLV4J9IjdPrxV/+4rMlBSjfr1khG8/GC6KGojjola8E1VvWF0qQ==" - }, - "System.IO.Hashing": { - "type": "Transitive", - "resolved": "10.0.8", - "contentHash": "+dJsbPJ3FyUbTZNplFj0RCKePFizmv6ewDV46JE9q/IVH4c3xTCftHfHelLsAKf0jryIPqgMb5GpS0x7TAY3mg==" + "resolved": "10.0.12", + "contentHash": "jDix4bBMYnpZdSPcnY+KDV6ik3SRMzpMKby/bZl/XUwIiflwRNAFZ0oOl61R/pSaveIJ8t1gs2BUlrGsPs/bcg==" } } } -} +} \ No newline at end of file diff --git a/tests/SharpCompress.AotSmoke/packages.lock.json b/tests/SharpCompress.AotSmoke/packages.lock.json index 28172cef..5904a29a 100644 --- a/tests/SharpCompress.AotSmoke/packages.lock.json +++ b/tests/SharpCompress.AotSmoke/packages.lock.json @@ -25,13 +25,13 @@ }, "Microsoft.SourceLink.GitHub": { "type": "Direct", - "requested": "[10.0.300, )", - "resolved": "10.0.300", - "contentHash": "QzCtLkXVb3l4IxcpvJCbzUwMLihAmLN6vVLjQGSzYSF8d2dvXxqJAZk83RV3gYnp2egz8jRMgSR2woY3vOahTA==", + "requested": "[10.0.401, )", + "resolved": "10.0.401", + "contentHash": "LGmlwgP1Cx37JEWzyjS0o1/+xs/s/e3E2TBSuogI5ePA/9L0pfIfeYX0k5in7Bfcw8Nn2y2sG9jXxydTjiR2Fg==", "dependencies": { - "Microsoft.Build.Tasks.Git": "10.0.300", - "Microsoft.SourceLink.Common": "10.0.300", - "System.IO.Hashing": "10.0.8" + "Microsoft.Build.Tasks.Git": "10.0.401", + "Microsoft.SourceLink.Common": "10.0.401", + "System.IO.Hashing": "10.0.12" } }, "Microsoft.VisualStudio.Threading.Analyzers": { @@ -48,16 +48,16 @@ }, "Zomp.SyncMethodGenerator": { "type": "Direct", - "requested": "[2.0.42, )", - "resolved": "2.0.42", - "contentHash": "kf2PvmD9N5xIzsdf6vy5tk2w0hlD7+Kbd3w/wFmgozgHTKJlh2RNiS+Jx+4d1AtgyJVBD0CO0nofbThu5zTsgQ==" + "requested": "[2.0.56, )", + "resolved": "2.0.56", + "contentHash": "P1iMA1meu0uX8YcPWhkFnpzV3e+QFDDYMU6hMNyofS+pBlLgeVV7QevBmJmJ/mrxA1jujo3cqNaO1IapdfjOlA==" }, "Microsoft.Build.Tasks.Git": { "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "P0kaQwVZx4xIUe2FtrLyBadYNXuAljttJUPvjBYRuHhPE8L77L42KakLDkaADRiUrGspoLcMwayjrbQhYTr0zA==", + "resolved": "10.0.401", + "contentHash": "ZYctNuT10V9IYyCFydy63DXx0ggZQuynuzQOdLvW62dPgzjIz7f0ISEP75RGiq1jFQh8p6TmGSqxeQZQ87LCig==", "dependencies": { - "System.IO.Hashing": "10.0.8" + "System.IO.Hashing": "10.0.12" } }, "Microsoft.NETFramework.ReferenceAssemblies.net461": { @@ -67,13 +67,13 @@ }, "Microsoft.SourceLink.Common": { "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "0jlkXaUGjYlWTIVPve5MftjKHnT3SlAtq9BCLV4J9IjdPrxV/+4rMlBSjfr1khG8/GC6KGojjola8E1VvWF0qQ==" + "resolved": "10.0.401", + "contentHash": "u3rLxIwi/9MqDFaWGE/QQgLR1NBEzLOW2lv5+9OrZPDBYIAmFdYSWCWrR1ufpXWOqFn+x02TgKropl/oDuHmgA==" }, "System.IO.Hashing": { "type": "Transitive", - "resolved": "10.0.8", - "contentHash": "+dJsbPJ3FyUbTZNplFj0RCKePFizmv6ewDV46JE9q/IVH4c3xTCftHfHelLsAKf0jryIPqgMb5GpS0x7TAY3mg==" + "resolved": "10.0.12", + "contentHash": "jDix4bBMYnpZdSPcnY+KDV6ik3SRMzpMKby/bZl/XUwIiflwRNAFZ0oOl61R/pSaveIJ8t1gs2BUlrGsPs/bcg==" }, "sharpcompress": { "type": "Project" diff --git a/tests/SharpCompress.Performance/packages.lock.json b/tests/SharpCompress.Performance/packages.lock.json index 27ed5689..eb03c8cd 100644 --- a/tests/SharpCompress.Performance/packages.lock.json +++ b/tests/SharpCompress.Performance/packages.lock.json @@ -41,13 +41,13 @@ }, "Microsoft.SourceLink.GitHub": { "type": "Direct", - "requested": "[10.0.300, )", - "resolved": "10.0.300", - "contentHash": "QzCtLkXVb3l4IxcpvJCbzUwMLihAmLN6vVLjQGSzYSF8d2dvXxqJAZk83RV3gYnp2egz8jRMgSR2woY3vOahTA==", + "requested": "[10.0.401, )", + "resolved": "10.0.401", + "contentHash": "LGmlwgP1Cx37JEWzyjS0o1/+xs/s/e3E2TBSuogI5ePA/9L0pfIfeYX0k5in7Bfcw8Nn2y2sG9jXxydTjiR2Fg==", "dependencies": { - "Microsoft.Build.Tasks.Git": "10.0.300", - "Microsoft.SourceLink.Common": "10.0.300", - "System.IO.Hashing": "10.0.8" + "Microsoft.Build.Tasks.Git": "10.0.401", + "Microsoft.SourceLink.Common": "10.0.401", + "System.IO.Hashing": "10.0.12" } }, "Microsoft.VisualStudio.Threading.Analyzers": { @@ -64,9 +64,9 @@ }, "Zomp.SyncMethodGenerator": { "type": "Direct", - "requested": "[2.0.42, )", - "resolved": "2.0.42", - "contentHash": "kf2PvmD9N5xIzsdf6vy5tk2w0hlD7+Kbd3w/wFmgozgHTKJlh2RNiS+Jx+4d1AtgyJVBD0CO0nofbThu5zTsgQ==" + "requested": "[2.0.56, )", + "resolved": "2.0.56", + "contentHash": "P1iMA1meu0uX8YcPWhkFnpzV3e+QFDDYMU6hMNyofS+pBlLgeVV7QevBmJmJ/mrxA1jujo3cqNaO1IapdfjOlA==" }, "BenchmarkDotNet.Annotations": { "type": "Transitive", @@ -111,10 +111,10 @@ }, "Microsoft.Build.Tasks.Git": { "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "P0kaQwVZx4xIUe2FtrLyBadYNXuAljttJUPvjBYRuHhPE8L77L42KakLDkaADRiUrGspoLcMwayjrbQhYTr0zA==", + "resolved": "10.0.401", + "contentHash": "ZYctNuT10V9IYyCFydy63DXx0ggZQuynuzQOdLvW62dPgzjIz7f0ISEP75RGiq1jFQh8p6TmGSqxeQZQ87LCig==", "dependencies": { - "System.IO.Hashing": "10.0.8" + "System.IO.Hashing": "10.0.12" } }, "Microsoft.CodeAnalysis.Analyzers": { @@ -219,8 +219,8 @@ }, "Microsoft.SourceLink.Common": { "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "0jlkXaUGjYlWTIVPve5MftjKHnT3SlAtq9BCLV4J9IjdPrxV/+4rMlBSjfr1khG8/GC6KGojjola8E1VvWF0qQ==" + "resolved": "10.0.401", + "contentHash": "u3rLxIwi/9MqDFaWGE/QQgLR1NBEzLOW2lv5+9OrZPDBYIAmFdYSWCWrR1ufpXWOqFn+x02TgKropl/oDuHmgA==" }, "Perfolizer": { "type": "Transitive", @@ -242,8 +242,8 @@ }, "System.IO.Hashing": { "type": "Transitive", - "resolved": "10.0.8", - "contentHash": "+dJsbPJ3FyUbTZNplFj0RCKePFizmv6ewDV46JE9q/IVH4c3xTCftHfHelLsAKf0jryIPqgMb5GpS0x7TAY3mg==" + "resolved": "10.0.12", + "contentHash": "jDix4bBMYnpZdSPcnY+KDV6ik3SRMzpMKby/bZl/XUwIiflwRNAFZ0oOl61R/pSaveIJ8t1gs2BUlrGsPs/bcg==" }, "System.Management": { "type": "Transitive", diff --git a/tests/SharpCompress.Test/packages.lock.json b/tests/SharpCompress.Test/packages.lock.json index 4be1c126..be63f7b4 100644 --- a/tests/SharpCompress.Test/packages.lock.json +++ b/tests/SharpCompress.Test/packages.lock.json @@ -1,354 +1,21 @@ { "version": 2, "dependencies": { - ".NETFramework,Version=v4.8": { - "AwesomeAssertions": { - "type": "Direct", - "requested": "[9.5.0, )", - "resolved": "9.5.0", - "contentHash": "F92MsjoF8B7IdcWETru4QGZx6VAl38qQPKNGS3kwPwlLB5lb0O4bhFqG8a8sz2estQJWkKmZkVy1r19HTaFcpg==", - "dependencies": { - "System.Threading.Tasks.Extensions": "4.5.4" - } - }, - "Microsoft.NET.Test.Sdk": { - "type": "Direct", - "requested": "[18.6.0, )", - "resolved": "18.6.0", - "contentHash": "kAIBt0MsYR0o2RULmlW5BhQ1ha50aGEgLKG4f1p0kePBGLJCprqs3S+NxRrYN8UH7mSQRPKpeiH9mwPMEKUObQ==", - "dependencies": { - "Microsoft.CodeCoverage": "18.6.0" - } - }, - "Microsoft.NETFramework.ReferenceAssemblies": { - "type": "Direct", - "requested": "[1.0.3, )", - "resolved": "1.0.3", - "contentHash": "vUc9Npcs14QsyOD01tnv/m8sQUnGTGOw1BCmKcv77LBJY7OxhJ+zJF7UD/sCL3lYNFuqmQEVlkfS4Quif6FyYg==", - "dependencies": { - "Microsoft.NETFramework.ReferenceAssemblies.net48": "1.0.3" - } - }, - "Microsoft.SourceLink.GitHub": { - "type": "Direct", - "requested": "[10.0.300, )", - "resolved": "10.0.300", - "contentHash": "QzCtLkXVb3l4IxcpvJCbzUwMLihAmLN6vVLjQGSzYSF8d2dvXxqJAZk83RV3gYnp2egz8jRMgSR2woY3vOahTA==", - "dependencies": { - "Microsoft.Build.Tasks.Git": "10.0.300", - "Microsoft.SourceLink.Common": "10.0.300", - "System.IO.Hashing": "10.0.8" - } - }, - "Microsoft.VisualStudio.Threading.Analyzers": { - "type": "Direct", - "requested": "[17.14.15, )", - "resolved": "17.14.15", - "contentHash": "mXQPJsbuUD2ydq4/ffd8h8tSOFCXec+2xJOVNCvXjuMOq/+5EKHq3D2m2MC2+nUaXeFMSt66VS/J4HdKBixgcw==" - }, - "PolySharp": { - "type": "Direct", - "requested": "[1.16.0, )", - "resolved": "1.16.0", - "contentHash": "3kdIIceBPumwjw279FuiVMfVENT2cGASXJgcigdySsbX2dJB8ofUgG6i47yqF/k1qu6fvNR3csrSekZPviR6kQ==" - }, - "xunit.runner.visualstudio": { - "type": "Direct", - "requested": "[3.1.5, )", - "resolved": "3.1.5", - "contentHash": "tKi7dSTwP4m5m9eXPM2Ime4Kn7xNf4x4zT9sdLO/G4hZVnQCRiMTWoSZqI/pYTVeI27oPPqHBKYI/DjJ9GsYgA==", - "dependencies": { - "Microsoft.TestPlatform.ObjectModel": "17.13.0" - } - }, - "xunit.v3": { - "type": "Direct", - "requested": "[3.2.2, )", - "resolved": "3.2.2", - "contentHash": "L+4/4y0Uqcg8/d6hfnxhnwh4j9FaeULvefTwrk30rr1o4n/vdPfyUQ8k0yzH8VJx7bmFEkDdcRfbtbjEHlaYcA==", - "dependencies": { - "xunit.v3.mtp-v1": "[3.2.2]" - } - }, - "Zomp.SyncMethodGenerator": { - "type": "Direct", - "requested": "[2.0.42, )", - "resolved": "2.0.42", - "contentHash": "kf2PvmD9N5xIzsdf6vy5tk2w0hlD7+Kbd3w/wFmgozgHTKJlh2RNiS+Jx+4d1AtgyJVBD0CO0nofbThu5zTsgQ==" - }, - "Microsoft.ApplicationInsights": { - "type": "Transitive", - "resolved": "2.23.0", - "contentHash": "nWArUZTdU7iqZLycLKWe0TDms48KKGE6pONH2terYNa8REXiqixrMOkf1sk5DHGMaUTqONU2YkS4SAXBhLStgw==", - "dependencies": { - "System.Diagnostics.DiagnosticSource": "5.0.0" - } - }, - "Microsoft.Build.Tasks.Git": { - "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "P0kaQwVZx4xIUe2FtrLyBadYNXuAljttJUPvjBYRuHhPE8L77L42KakLDkaADRiUrGspoLcMwayjrbQhYTr0zA==", - "dependencies": { - "System.IO.Hashing": "10.0.8" - } - }, - "Microsoft.CodeCoverage": { - "type": "Transitive", - "resolved": "18.6.0", - "contentHash": "bkmCXn/65Cd0LdO2zTb/ValGAJ1H8y/CgYOiBb3jsDyHI3Y1ljKx6RBvhvn3e5D/4R4I00RRwLf+Bd2Sn6bJjA==" - }, - "Microsoft.NETFramework.ReferenceAssemblies.net48": { - "type": "Transitive", - "resolved": "1.0.3", - "contentHash": "zMk4D+9zyiEWByyQ7oPImPN/Jhpj166Ky0Nlla4eXlNL8hI/BtSJsgR8Inldd4NNpIAH3oh8yym0W2DrhXdSLQ==" - }, - "Microsoft.SourceLink.Common": { - "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "0jlkXaUGjYlWTIVPve5MftjKHnT3SlAtq9BCLV4J9IjdPrxV/+4rMlBSjfr1khG8/GC6KGojjola8E1VvWF0qQ==" - }, - "Microsoft.Testing.Extensions.Telemetry": { - "type": "Transitive", - "resolved": "1.9.1", - "contentHash": "No5AudZMmSb+uNXjlgL2y3/stHD2IT4uxqc5yHwkE+/nNux9jbKcaJMvcp9SwgP4DVD8L9/P3OUz8mmmcvEIdQ==", - "dependencies": { - "Microsoft.ApplicationInsights": "2.23.0", - "Microsoft.Testing.Platform": "1.9.1", - "System.Diagnostics.DiagnosticSource": "6.0.0" - } - }, - "Microsoft.Testing.Extensions.TrxReport.Abstractions": { - "type": "Transitive", - "resolved": "1.9.1", - "contentHash": "AL46Xe1WBi85Ntd4mNPvat5ZSsZ2uejiVqoKCypr8J3wK0elA5xJ3AN4G/Q4GIwzUFnggZoH/DBjnr9J18IO/g==", - "dependencies": { - "Microsoft.Testing.Platform": "1.9.1" - } - }, - "Microsoft.Testing.Platform": { - "type": "Transitive", - "resolved": "1.9.1", - "contentHash": "QafNtNSmEI0zazdebnsIkDKmFtTSpmx/5PLOjURWwozcPb3tvRxzosQSL8xwYNM1iPhhKiBksXZyRSE2COisrA==" - }, - "Microsoft.Testing.Platform.MSBuild": { - "type": "Transitive", - "resolved": "1.9.1", - "contentHash": "oTUtyR4X/s9ytuiNA29FGsNCCH0rNmY5Wdm14NCKLjTM1cT9edVSlA+rGS/mVmusPqcP0l/x9qOnMXg16v87RQ==", - "dependencies": { - "Microsoft.Testing.Platform": "1.9.1" - } - }, - "Microsoft.TestPlatform.ObjectModel": { - "type": "Transitive", - "resolved": "17.13.0", - "contentHash": "bt0E0Dx+iqW97o4A59RCmUmz/5NarJ7LRL+jXbSHod72ibL5XdNm1Ke+UO5tFhBG4VwHLcSjqq9BUSblGNWamw==", - "dependencies": { - "System.Reflection.Metadata": "1.6.0" - } - }, - "Microsoft.Win32.Registry": { - "type": "Transitive", - "resolved": "5.0.0", - "contentHash": "dDoKi0PnDz31yAyETfRntsLArTlVAVzUzCIvvEDsDsucrl33Dl8pIJG06ePTJTI3tGpeyHS9Cq7Foc/s4EeKcg==", - "dependencies": { - "System.Security.AccessControl": "5.0.0", - "System.Security.Principal.Windows": "5.0.0" - } - }, - "System.Buffers": { - "type": "Transitive", - "resolved": "4.6.1", - "contentHash": "N8GXpmiLMtljq7gwvyS+1QvKT/W2J8sNAvx+HVg4NGmsG/H+2k/y9QI23auLJRterrzCiDH+IWAw4V/GPwsMlw==" - }, - "System.Collections.Immutable": { - "type": "Transitive", - "resolved": "6.0.0", - "contentHash": "l4zZJ1WU2hqpQQHXz1rvC3etVZN+2DLmQMO79FhOTZHMn8tDRr+WU287sbomD0BETlmKDn0ygUgVy9k5xkkJdA==", - "dependencies": { - "System.Memory": "4.5.4", - "System.Runtime.CompilerServices.Unsafe": "6.0.0" - } - }, - "System.Diagnostics.DiagnosticSource": { - "type": "Transitive", - "resolved": "6.0.0", - "contentHash": "frQDfv0rl209cKm1lnwTgFPzNigy2EKk1BS3uAvHvlBVKe5cymGyHO+Sj+NLv5VF/AhHsqPIUUwya5oV4CHMUw==", - "dependencies": { - "System.Memory": "4.5.4", - "System.Runtime.CompilerServices.Unsafe": "6.0.0" - } - }, - "System.IO.Hashing": { - "type": "Transitive", - "resolved": "10.0.8", - "contentHash": "+dJsbPJ3FyUbTZNplFj0RCKePFizmv6ewDV46JE9q/IVH4c3xTCftHfHelLsAKf0jryIPqgMb5GpS0x7TAY3mg==", - "dependencies": { - "System.Buffers": "4.6.1", - "System.Memory": "4.6.3" - } - }, - "System.Memory": { - "type": "Transitive", - "resolved": "4.6.3", - "contentHash": "qdcDOgnFZY40+Q9876JUHnlHu7bosOHX8XISRoH94fwk6hgaeQGSgfZd8srWRZNt5bV9ZW2TljcegDNxsf+96A==", - "dependencies": { - "System.Buffers": "4.6.1", - "System.Numerics.Vectors": "4.6.1", - "System.Runtime.CompilerServices.Unsafe": "6.1.2" - } - }, - "System.Numerics.Vectors": { - "type": "Transitive", - "resolved": "4.6.1", - "contentHash": "sQxefTnhagrhoq2ReR0D/6K0zJcr9Hrd6kikeXsA1I8kOCboTavcUC4r7TSfpKFeE163uMuxZcyfO1mGO3EN8Q==" - }, - "System.Reflection.Metadata": { - "type": "Transitive", - "resolved": "1.6.0", - "contentHash": "COC1aiAJjCoA5GBF+QKL2uLqEBew4JsCkQmoHKbN3TlOZKa2fKLz5CpiRQKDz0RsAOEGsVKqOD5bomsXq/4STQ==", - "dependencies": { - "System.Collections.Immutable": "1.5.0" - } - }, - "System.Runtime.CompilerServices.Unsafe": { - "type": "Transitive", - "resolved": "6.1.2", - "contentHash": "2hBr6zdbIBTDE3EhK7NSVNdX58uTK6iHW/P/Axmm9sl1xoGSLqDvMtpecn226TNwHByFokYwJmt/aQQNlO5CRw==" - }, - "System.Security.AccessControl": { - "type": "Transitive", - "resolved": "5.0.0", - "contentHash": "dagJ1mHZO3Ani8GH0PHpPEe/oYO+rVdbQjvjJkBRNQkX4t0r1iaeGn8+/ybkSLEan3/slM0t59SVdHzuHf2jmw==", - "dependencies": { - "System.Security.Principal.Windows": "5.0.0" - } - }, - "System.Security.Principal.Windows": { - "type": "Transitive", - "resolved": "5.0.0", - "contentHash": "t0MGLukB5WAVU9bO3MGzvlGnyJPgUlcwerXn1kzBRjwLKixT96XV0Uza41W49gVd8zEMFu9vQEFlv0IOrytICA==" - }, - "System.Threading.Tasks.Extensions": { - "type": "Transitive", - "resolved": "4.5.4", - "contentHash": "zteT+G8xuGu6mS+mzDzYXbzS7rd3K6Fjb9RiZlYlJPam2/hU7JCBZBVEcywNuR+oZ1ncTvc/cq0faRr3P01OVg==", - "dependencies": { - "System.Runtime.CompilerServices.Unsafe": "4.5.3" - } - }, - "xunit.analyzers": { - "type": "Transitive", - "resolved": "1.27.0", - "contentHash": "y/pxIQaLvk/kxAoDkZW9GnHLCEqzwl5TW0vtX3pweyQpjizB9y3DXhb9pkw2dGeUqhLjsxvvJM1k89JowU6z3g==" - }, - "xunit.v3.assert": { - "type": "Transitive", - "resolved": "3.2.2", - "contentHash": "BPciBghgEEaJN/JG00QfCYDfEfnLgQhfnYEy+j1izoeHVNYd5+3Wm8GJ6JgYysOhpBPYGE+sbf75JtrRc7jrdA==", - "dependencies": { - "System.Collections.Immutable": "6.0.0", - "System.Memory": "4.5.5" - } - }, - "xunit.v3.common": { - "type": "Transitive", - "resolved": "3.2.2", - "contentHash": "Hj775PEH6GTbbg0wfKRvG2hNspDCvTH9irXhH4qIWgdrOSV1sQlqPie+DOvFeigsFg2fxSM3ZAaaCDQs+KreFA==", - "dependencies": { - "Microsoft.Bcl.AsyncInterfaces": "6.0.0" - } - }, - "xunit.v3.core.mtp-v1": { - "type": "Transitive", - "resolved": "3.2.2", - "contentHash": "Ga5aA2Ca9ktz+5k3g5ukzwfexwoqwDUpV6z7atSEUvqtd6JuybU1XopHqg1oFd78QdTfZgZE9h5sHpO4qYIi5w==", - "dependencies": { - "Microsoft.Testing.Extensions.Telemetry": "1.9.1", - "Microsoft.Testing.Extensions.TrxReport.Abstractions": "1.9.1", - "Microsoft.Testing.Platform": "1.9.1", - "Microsoft.Testing.Platform.MSBuild": "1.9.1", - "xunit.v3.extensibility.core": "[3.2.2]", - "xunit.v3.runner.inproc.console": "[3.2.2]" - } - }, - "xunit.v3.extensibility.core": { - "type": "Transitive", - "resolved": "3.2.2", - "contentHash": "srY8z/oMPvh/t8axtO2DwrHajhFMH7tnqKildvYrVQIfICi8fOn3yIBWkVPAcrKmHMwvXRJ/XsQM3VMR6DOYfQ==", - "dependencies": { - "xunit.v3.common": "[3.2.2]" - } - }, - "xunit.v3.mtp-v1": { - "type": "Transitive", - "resolved": "3.2.2", - "contentHash": "O41aAzYKBT5PWqATa1oEWVNCyEUypFQ4va6K0kz37dduV3EKzXNMaV2UnEhufzU4Cce1I33gg0oldS8tGL5I0A==", - "dependencies": { - "xunit.analyzers": "1.27.0", - "xunit.v3.assert": "[3.2.2]", - "xunit.v3.core.mtp-v1": "[3.2.2]" - } - }, - "xunit.v3.runner.common": { - "type": "Transitive", - "resolved": "3.2.2", - "contentHash": "/hkHkQCzGrugelOAehprm7RIWdsUFVmIVaD6jDH/8DNGCymTlKKPTbGokD5czbAfqfex47mBP0sb0zbHYwrO/g==", - "dependencies": { - "Microsoft.Win32.Registry": "[5.0.0]", - "xunit.v3.common": "[3.2.2]" - } - }, - "xunit.v3.runner.inproc.console": { - "type": "Transitive", - "resolved": "3.2.2", - "contentHash": "ulWOdSvCk+bPXijJZ73bth9NyoOHsAs1ZOvamYbCkD4DNLX/Bd29Ve2ZNUwBbK0MqfIYWXHZViy/HKrdEC/izw==", - "dependencies": { - "xunit.v3.extensibility.core": "[3.2.2]", - "xunit.v3.runner.common": "[3.2.2]" - } - }, - "sharpcompress": { - "type": "Project", - "dependencies": { - "Microsoft.Bcl.AsyncInterfaces": "[8.0.0, )", - "System.Text.Encoding.CodePages": "[8.0.0, )" - } - }, - "Microsoft.Bcl.AsyncInterfaces": { - "type": "CentralTransitive", - "requested": "[8.0.0, )", - "resolved": "8.0.0", - "contentHash": "3WA9q9yVqJp222P3x1wYIGDAkpjAku0TMUaaQV22g6L67AI0LdOIrVS7Ht2vJfLHGSPVuqN94vIr15qn+HEkHw==", - "dependencies": { - "System.Threading.Tasks.Extensions": "4.5.4" - } - }, - "System.Text.Encoding.CodePages": { - "type": "CentralTransitive", - "requested": "[8.0.0, )", - "resolved": "8.0.0", - "contentHash": "OZIsVplFGaVY90G2SbpgU7EnCoOO5pw1t4ic21dBF3/1omrJFpAGoNAVpPyMVOC90/hvgkGG3VFqR13YgZMQfg==", - "dependencies": { - "System.Memory": "4.5.5", - "System.Runtime.CompilerServices.Unsafe": "6.0.0" - } - } - }, "net10.0": { "AwesomeAssertions": { "type": "Direct", - "requested": "[9.5.0, )", - "resolved": "9.5.0", - "contentHash": "F92MsjoF8B7IdcWETru4QGZx6VAl38qQPKNGS3kwPwlLB5lb0O4bhFqG8a8sz2estQJWkKmZkVy1r19HTaFcpg==" + "requested": "[9.6.0, )", + "resolved": "9.6.0", + "contentHash": "8kdtgI+tZIgP46ncnk650/dU4JSrn+n2Dkic8FvjIFn72P6Nv1Afyr5F8F3PsokHg1GBSBVo1/Z+eZOrU5esSA==" }, "Microsoft.NET.Test.Sdk": { "type": "Direct", - "requested": "[18.6.0, )", - "resolved": "18.6.0", - "contentHash": "kAIBt0MsYR0o2RULmlW5BhQ1ha50aGEgLKG4f1p0kePBGLJCprqs3S+NxRrYN8UH7mSQRPKpeiH9mwPMEKUObQ==", + "requested": "[18.10.1, )", + "resolved": "18.10.1", + "contentHash": "SxCFvJE/2ltUQgCgv6+Ixagohw4sZc9hM7Wid2foaNM7paBscktoHLoRDz91fW8wbHG87Rg9Ku59/8PXumqUrA==", "dependencies": { - "Microsoft.CodeCoverage": "18.6.0", - "Microsoft.TestPlatform.TestHost": "18.6.0" + "Microsoft.CodeCoverage": "18.10.1", + "Microsoft.TestPlatform.TestHost": "18.10.1" } }, "Microsoft.NETFramework.ReferenceAssemblies": { @@ -362,13 +29,13 @@ }, "Microsoft.SourceLink.GitHub": { "type": "Direct", - "requested": "[10.0.300, )", - "resolved": "10.0.300", - "contentHash": "QzCtLkXVb3l4IxcpvJCbzUwMLihAmLN6vVLjQGSzYSF8d2dvXxqJAZk83RV3gYnp2egz8jRMgSR2woY3vOahTA==", + "requested": "[10.0.401, )", + "resolved": "10.0.401", + "contentHash": "LGmlwgP1Cx37JEWzyjS0o1/+xs/s/e3E2TBSuogI5ePA/9L0pfIfeYX0k5in7Bfcw8Nn2y2sG9jXxydTjiR2Fg==", "dependencies": { - "Microsoft.Build.Tasks.Git": "10.0.300", - "Microsoft.SourceLink.Common": "10.0.300", - "System.IO.Hashing": "10.0.8" + "Microsoft.Build.Tasks.Git": "10.0.401", + "Microsoft.SourceLink.Common": "10.0.401", + "System.IO.Hashing": "10.0.12" } }, "Microsoft.VisualStudio.Threading.Analyzers": { @@ -400,9 +67,9 @@ }, "Zomp.SyncMethodGenerator": { "type": "Direct", - "requested": "[2.0.42, )", - "resolved": "2.0.42", - "contentHash": "kf2PvmD9N5xIzsdf6vy5tk2w0hlD7+Kbd3w/wFmgozgHTKJlh2RNiS+Jx+4d1AtgyJVBD0CO0nofbThu5zTsgQ==" + "requested": "[2.0.56, )", + "resolved": "2.0.56", + "contentHash": "P1iMA1meu0uX8YcPWhkFnpzV3e+QFDDYMU6hMNyofS+pBlLgeVV7QevBmJmJ/mrxA1jujo3cqNaO1IapdfjOlA==" }, "Microsoft.ApplicationInsights": { "type": "Transitive", @@ -411,16 +78,16 @@ }, "Microsoft.Build.Tasks.Git": { "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "P0kaQwVZx4xIUe2FtrLyBadYNXuAljttJUPvjBYRuHhPE8L77L42KakLDkaADRiUrGspoLcMwayjrbQhYTr0zA==", + "resolved": "10.0.401", + "contentHash": "ZYctNuT10V9IYyCFydy63DXx0ggZQuynuzQOdLvW62dPgzjIz7f0ISEP75RGiq1jFQh8p6TmGSqxeQZQ87LCig==", "dependencies": { - "System.IO.Hashing": "10.0.8" + "System.IO.Hashing": "10.0.12" } }, "Microsoft.CodeCoverage": { "type": "Transitive", - "resolved": "18.6.0", - "contentHash": "bkmCXn/65Cd0LdO2zTb/ValGAJ1H8y/CgYOiBb3jsDyHI3Y1ljKx6RBvhvn3e5D/4R4I00RRwLf+Bd2Sn6bJjA==" + "resolved": "18.10.1", + "contentHash": "tV7tCyp/t+DuwQugLkCeUN7NBFFB4fEGRPr0gkMQ2nQgAvQQjmGlZZi1dsLzbyoe/A0mbnKybmkIh8qhT9p4Zw==" }, "Microsoft.NETFramework.ReferenceAssemblies.net461": { "type": "Transitive", @@ -429,8 +96,8 @@ }, "Microsoft.SourceLink.Common": { "type": "Transitive", - "resolved": "10.0.300", - "contentHash": "0jlkXaUGjYlWTIVPve5MftjKHnT3SlAtq9BCLV4J9IjdPrxV/+4rMlBSjfr1khG8/GC6KGojjola8E1VvWF0qQ==" + "resolved": "10.0.401", + "contentHash": "u3rLxIwi/9MqDFaWGE/QQgLR1NBEzLOW2lv5+9OrZPDBYIAmFdYSWCWrR1ufpXWOqFn+x02TgKropl/oDuHmgA==" }, "Microsoft.Testing.Extensions.Telemetry": { "type": "Transitive", @@ -464,16 +131,15 @@ }, "Microsoft.TestPlatform.ObjectModel": { "type": "Transitive", - "resolved": "18.6.0", - "contentHash": "gQTW4BIfM2ZLxixo9ITXoulLKjn20FiiHtqTsx9PENqTrX7368ZeJ5L0QZJyReXDWORPRV8jXwZR6Aar8JOyaA==" + "resolved": "18.10.1", + "contentHash": "o+PuLDRVp1iwiVAPBADZItXgm9Ck23XtPhE4kZtZVi0uYnMiiz1Dx9Cjh75kJ+ijfqnORL3VOTOaQVKi0f/ixg==" }, "Microsoft.TestPlatform.TestHost": { "type": "Transitive", - "resolved": "18.6.0", - "contentHash": "em1eLz5Q46+hsCtAXdXggWAPd9gQyT4ngdsQ7k1eWvQgpsjtS/wAOJ/5TteieFdiAvrEq1iVn00LtusAxRaVmQ==", + "resolved": "18.10.1", + "contentHash": "KYz2omqkoz9E2mWa4fdgT+Ycie1wi5968EmyJlusjlp87RxxtA2FvDm8tcBEYIDzLnkWGnsKjE4d13Z7/rxkQA==", "dependencies": { - "Microsoft.TestPlatform.ObjectModel": "18.6.0", - "Newtonsoft.Json": "13.0.3" + "Microsoft.TestPlatform.ObjectModel": "18.10.1" } }, "Microsoft.Win32.Registry": { @@ -481,15 +147,10 @@ "resolved": "5.0.0", "contentHash": "dDoKi0PnDz31yAyETfRntsLArTlVAVzUzCIvvEDsDsucrl33Dl8pIJG06ePTJTI3tGpeyHS9Cq7Foc/s4EeKcg==" }, - "Newtonsoft.Json": { - "type": "Transitive", - "resolved": "13.0.3", - "contentHash": "HrC5BXdl00IP9zeV+0Z848QWPAoCr9P3bDEZguI+gkLcBKAOxix/tLEAAHC+UvDNPv4a2d18lOReHMOagPa+zQ==" - }, "System.IO.Hashing": { "type": "Transitive", - "resolved": "10.0.8", - "contentHash": "+dJsbPJ3FyUbTZNplFj0RCKePFizmv6ewDV46JE9q/IVH4c3xTCftHfHelLsAKf0jryIPqgMb5GpS0x7TAY3mg==" + "resolved": "10.0.12", + "contentHash": "jDix4bBMYnpZdSPcnY+KDV6ik3SRMzpMKby/bZl/XUwIiflwRNAFZ0oOl61R/pSaveIJ8t1gs2BUlrGsPs/bcg==" }, "xunit.analyzers": { "type": "Transitive", From 677fa5137874554a336a33c6e24e1282191f215b Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Thu, 1 Oct 2026 16:08:19 +0100 Subject: [PATCH 40/40] fix locks --- src/SharpCompress/packages.lock.json | 467 ++++++++++++++++++++ tests/SharpCompress.Test/packages.lock.json | 333 ++++++++++++++ 2 files changed, 800 insertions(+) diff --git a/src/SharpCompress/packages.lock.json b/src/SharpCompress/packages.lock.json index 812aad1d..65b5301d 100644 --- a/src/SharpCompress/packages.lock.json +++ b/src/SharpCompress/packages.lock.json @@ -1,6 +1,341 @@ { "version": 2, "dependencies": { + ".NETFramework,Version=v4.8": { + "Microsoft.Bcl.AsyncInterfaces": { + "type": "Direct", + "requested": "[8.0.0, )", + "resolved": "8.0.0", + "contentHash": "3WA9q9yVqJp222P3x1wYIGDAkpjAku0TMUaaQV22g6L67AI0LdOIrVS7Ht2vJfLHGSPVuqN94vIr15qn+HEkHw==", + "dependencies": { + "System.Threading.Tasks.Extensions": "4.5.4" + } + }, + "Microsoft.NETFramework.ReferenceAssemblies": { + "type": "Direct", + "requested": "[1.0.3, )", + "resolved": "1.0.3", + "contentHash": "vUc9Npcs14QsyOD01tnv/m8sQUnGTGOw1BCmKcv77LBJY7OxhJ+zJF7UD/sCL3lYNFuqmQEVlkfS4Quif6FyYg==", + "dependencies": { + "Microsoft.NETFramework.ReferenceAssemblies.net48": "1.0.3" + } + }, + "Microsoft.SourceLink.GitHub": { + "type": "Direct", + "requested": "[10.0.401, )", + "resolved": "10.0.401", + "contentHash": "LGmlwgP1Cx37JEWzyjS0o1/+xs/s/e3E2TBSuogI5ePA/9L0pfIfeYX0k5in7Bfcw8Nn2y2sG9jXxydTjiR2Fg==", + "dependencies": { + "Microsoft.Build.Tasks.Git": "10.0.401", + "Microsoft.SourceLink.Common": "10.0.401", + "System.IO.Hashing": "10.0.12" + } + }, + "Microsoft.VisualStudio.Threading.Analyzers": { + "type": "Direct", + "requested": "[17.14.15, )", + "resolved": "17.14.15", + "contentHash": "mXQPJsbuUD2ydq4/ffd8h8tSOFCXec+2xJOVNCvXjuMOq/+5EKHq3D2m2MC2+nUaXeFMSt66VS/J4HdKBixgcw==" + }, + "PolySharp": { + "type": "Direct", + "requested": "[1.16.0, )", + "resolved": "1.16.0", + "contentHash": "3kdIIceBPumwjw279FuiVMfVENT2cGASXJgcigdySsbX2dJB8ofUgG6i47yqF/k1qu6fvNR3csrSekZPviR6kQ==" + }, + "System.Text.Encoding.CodePages": { + "type": "Direct", + "requested": "[8.0.0, )", + "resolved": "8.0.0", + "contentHash": "OZIsVplFGaVY90G2SbpgU7EnCoOO5pw1t4ic21dBF3/1omrJFpAGoNAVpPyMVOC90/hvgkGG3VFqR13YgZMQfg==", + "dependencies": { + "System.Memory": "4.5.5", + "System.Runtime.CompilerServices.Unsafe": "6.0.0" + } + }, + "Zomp.SyncMethodGenerator": { + "type": "Direct", + "requested": "[2.0.56, )", + "resolved": "2.0.56", + "contentHash": "P1iMA1meu0uX8YcPWhkFnpzV3e+QFDDYMU6hMNyofS+pBlLgeVV7QevBmJmJ/mrxA1jujo3cqNaO1IapdfjOlA==" + }, + "Microsoft.Build.Tasks.Git": { + "type": "Transitive", + "resolved": "10.0.401", + "contentHash": "ZYctNuT10V9IYyCFydy63DXx0ggZQuynuzQOdLvW62dPgzjIz7f0ISEP75RGiq1jFQh8p6TmGSqxeQZQ87LCig==", + "dependencies": { + "System.IO.Hashing": "10.0.12" + } + }, + "Microsoft.NETFramework.ReferenceAssemblies.net48": { + "type": "Transitive", + "resolved": "1.0.3", + "contentHash": "zMk4D+9zyiEWByyQ7oPImPN/Jhpj166Ky0Nlla4eXlNL8hI/BtSJsgR8Inldd4NNpIAH3oh8yym0W2DrhXdSLQ==" + }, + "Microsoft.SourceLink.Common": { + "type": "Transitive", + "resolved": "10.0.401", + "contentHash": "u3rLxIwi/9MqDFaWGE/QQgLR1NBEzLOW2lv5+9OrZPDBYIAmFdYSWCWrR1ufpXWOqFn+x02TgKropl/oDuHmgA==" + }, + "System.Buffers": { + "type": "Transitive", + "resolved": "4.6.1", + "contentHash": "N8GXpmiLMtljq7gwvyS+1QvKT/W2J8sNAvx+HVg4NGmsG/H+2k/y9QI23auLJRterrzCiDH+IWAw4V/GPwsMlw==" + }, + "System.IO.Hashing": { + "type": "Transitive", + "resolved": "10.0.12", + "contentHash": "jDix4bBMYnpZdSPcnY+KDV6ik3SRMzpMKby/bZl/XUwIiflwRNAFZ0oOl61R/pSaveIJ8t1gs2BUlrGsPs/bcg==", + "dependencies": { + "System.Buffers": "4.6.1", + "System.Memory": "4.6.3" + } + }, + "System.Memory": { + "type": "Transitive", + "resolved": "4.6.3", + "contentHash": "qdcDOgnFZY40+Q9876JUHnlHu7bosOHX8XISRoH94fwk6hgaeQGSgfZd8srWRZNt5bV9ZW2TljcegDNxsf+96A==", + "dependencies": { + "System.Buffers": "4.6.1", + "System.Numerics.Vectors": "4.6.1", + "System.Runtime.CompilerServices.Unsafe": "6.1.2" + } + }, + "System.Numerics.Vectors": { + "type": "Transitive", + "resolved": "4.6.1", + "contentHash": "sQxefTnhagrhoq2ReR0D/6K0zJcr9Hrd6kikeXsA1I8kOCboTavcUC4r7TSfpKFeE163uMuxZcyfO1mGO3EN8Q==" + }, + "System.Runtime.CompilerServices.Unsafe": { + "type": "Transitive", + "resolved": "6.1.2", + "contentHash": "2hBr6zdbIBTDE3EhK7NSVNdX58uTK6iHW/P/Axmm9sl1xoGSLqDvMtpecn226TNwHByFokYwJmt/aQQNlO5CRw==" + }, + "System.Threading.Tasks.Extensions": { + "type": "Transitive", + "resolved": "4.5.4", + "contentHash": "zteT+G8xuGu6mS+mzDzYXbzS7rd3K6Fjb9RiZlYlJPam2/hU7JCBZBVEcywNuR+oZ1ncTvc/cq0faRr3P01OVg==", + "dependencies": { + "System.Runtime.CompilerServices.Unsafe": "4.5.3" + } + } + }, + ".NETStandard,Version=v2.0": { + "Microsoft.Bcl.AsyncInterfaces": { + "type": "Direct", + "requested": "[8.0.0, )", + "resolved": "8.0.0", + "contentHash": "3WA9q9yVqJp222P3x1wYIGDAkpjAku0TMUaaQV22g6L67AI0LdOIrVS7Ht2vJfLHGSPVuqN94vIr15qn+HEkHw==", + "dependencies": { + "System.Threading.Tasks.Extensions": "4.5.4" + } + }, + "Microsoft.NETFramework.ReferenceAssemblies": { + "type": "Direct", + "requested": "[1.0.3, )", + "resolved": "1.0.3", + "contentHash": "vUc9Npcs14QsyOD01tnv/m8sQUnGTGOw1BCmKcv77LBJY7OxhJ+zJF7UD/sCL3lYNFuqmQEVlkfS4Quif6FyYg==", + "dependencies": { + "Microsoft.NETFramework.ReferenceAssemblies.net461": "1.0.3" + } + }, + "Microsoft.SourceLink.GitHub": { + "type": "Direct", + "requested": "[10.0.401, )", + "resolved": "10.0.401", + "contentHash": "LGmlwgP1Cx37JEWzyjS0o1/+xs/s/e3E2TBSuogI5ePA/9L0pfIfeYX0k5in7Bfcw8Nn2y2sG9jXxydTjiR2Fg==", + "dependencies": { + "Microsoft.Build.Tasks.Git": "10.0.401", + "Microsoft.SourceLink.Common": "10.0.401", + "System.IO.Hashing": "10.0.12" + } + }, + "Microsoft.VisualStudio.Threading.Analyzers": { + "type": "Direct", + "requested": "[17.14.15, )", + "resolved": "17.14.15", + "contentHash": "mXQPJsbuUD2ydq4/ffd8h8tSOFCXec+2xJOVNCvXjuMOq/+5EKHq3D2m2MC2+nUaXeFMSt66VS/J4HdKBixgcw==" + }, + "NETStandard.Library": { + "type": "Direct", + "requested": "[2.0.3, )", + "resolved": "2.0.3", + "contentHash": "st47PosZSHrjECdjeIzZQbzivYBJFv6P2nv4cj2ypdI204DO+vZ7l5raGMiX4eXMJ53RfOIg+/s4DHVZ54Nu2A==", + "dependencies": { + "Microsoft.NETCore.Platforms": "1.1.0" + } + }, + "PolySharp": { + "type": "Direct", + "requested": "[1.16.0, )", + "resolved": "1.16.0", + "contentHash": "3kdIIceBPumwjw279FuiVMfVENT2cGASXJgcigdySsbX2dJB8ofUgG6i47yqF/k1qu6fvNR3csrSekZPviR6kQ==" + }, + "System.Text.Encoding.CodePages": { + "type": "Direct", + "requested": "[8.0.0, )", + "resolved": "8.0.0", + "contentHash": "OZIsVplFGaVY90G2SbpgU7EnCoOO5pw1t4ic21dBF3/1omrJFpAGoNAVpPyMVOC90/hvgkGG3VFqR13YgZMQfg==", + "dependencies": { + "System.Memory": "4.5.5", + "System.Runtime.CompilerServices.Unsafe": "6.0.0" + } + }, + "Zomp.SyncMethodGenerator": { + "type": "Direct", + "requested": "[2.0.56, )", + "resolved": "2.0.56", + "contentHash": "P1iMA1meu0uX8YcPWhkFnpzV3e+QFDDYMU6hMNyofS+pBlLgeVV7QevBmJmJ/mrxA1jujo3cqNaO1IapdfjOlA==" + }, + "Microsoft.Build.Tasks.Git": { + "type": "Transitive", + "resolved": "10.0.401", + "contentHash": "ZYctNuT10V9IYyCFydy63DXx0ggZQuynuzQOdLvW62dPgzjIz7f0ISEP75RGiq1jFQh8p6TmGSqxeQZQ87LCig==", + "dependencies": { + "System.IO.Hashing": "10.0.12" + } + }, + "Microsoft.NETCore.Platforms": { + "type": "Transitive", + "resolved": "1.1.0", + "contentHash": "kz0PEW2lhqygehI/d6XsPCQzD7ff7gUJaVGPVETX611eadGsA3A877GdSlU0LRVMCTH/+P3o2iDTak+S08V2+A==" + }, + "Microsoft.NETFramework.ReferenceAssemblies.net461": { + "type": "Transitive", + "resolved": "1.0.3", + "contentHash": "AmOJZwCqnOCNp6PPcf9joyogScWLtwy0M1WkqfEQ0M9nYwyDD7EX9ZjscKS5iYnyvteX7kzSKFCKt9I9dXA6mA==" + }, + "Microsoft.SourceLink.Common": { + "type": "Transitive", + "resolved": "10.0.401", + "contentHash": "u3rLxIwi/9MqDFaWGE/QQgLR1NBEzLOW2lv5+9OrZPDBYIAmFdYSWCWrR1ufpXWOqFn+x02TgKropl/oDuHmgA==" + }, + "System.Buffers": { + "type": "Transitive", + "resolved": "4.6.1", + "contentHash": "N8GXpmiLMtljq7gwvyS+1QvKT/W2J8sNAvx+HVg4NGmsG/H+2k/y9QI23auLJRterrzCiDH+IWAw4V/GPwsMlw==" + }, + "System.IO.Hashing": { + "type": "Transitive", + "resolved": "10.0.12", + "contentHash": "jDix4bBMYnpZdSPcnY+KDV6ik3SRMzpMKby/bZl/XUwIiflwRNAFZ0oOl61R/pSaveIJ8t1gs2BUlrGsPs/bcg==", + "dependencies": { + "System.Buffers": "4.6.1", + "System.Memory": "4.6.3" + } + }, + "System.Memory": { + "type": "Transitive", + "resolved": "4.6.3", + "contentHash": "qdcDOgnFZY40+Q9876JUHnlHu7bosOHX8XISRoH94fwk6hgaeQGSgfZd8srWRZNt5bV9ZW2TljcegDNxsf+96A==", + "dependencies": { + "System.Buffers": "4.6.1", + "System.Numerics.Vectors": "4.6.1", + "System.Runtime.CompilerServices.Unsafe": "6.1.2" + } + }, + "System.Numerics.Vectors": { + "type": "Transitive", + "resolved": "4.6.1", + "contentHash": "sQxefTnhagrhoq2ReR0D/6K0zJcr9Hrd6kikeXsA1I8kOCboTavcUC4r7TSfpKFeE163uMuxZcyfO1mGO3EN8Q==" + }, + "System.Runtime.CompilerServices.Unsafe": { + "type": "Transitive", + "resolved": "6.1.2", + "contentHash": "2hBr6zdbIBTDE3EhK7NSVNdX58uTK6iHW/P/Axmm9sl1xoGSLqDvMtpecn226TNwHByFokYwJmt/aQQNlO5CRw==" + }, + "System.Threading.Tasks.Extensions": { + "type": "Transitive", + "resolved": "4.5.4", + "contentHash": "zteT+G8xuGu6mS+mzDzYXbzS7rd3K6Fjb9RiZlYlJPam2/hU7JCBZBVEcywNuR+oZ1ncTvc/cq0faRr3P01OVg==", + "dependencies": { + "System.Runtime.CompilerServices.Unsafe": "4.5.3" + } + } + }, + ".NETStandard,Version=v2.1": { + "Microsoft.Bcl.AsyncInterfaces": { + "type": "Direct", + "requested": "[8.0.0, )", + "resolved": "8.0.0", + "contentHash": "3WA9q9yVqJp222P3x1wYIGDAkpjAku0TMUaaQV22g6L67AI0LdOIrVS7Ht2vJfLHGSPVuqN94vIr15qn+HEkHw==" + }, + "Microsoft.NETFramework.ReferenceAssemblies": { + "type": "Direct", + "requested": "[1.0.3, )", + "resolved": "1.0.3", + "contentHash": "vUc9Npcs14QsyOD01tnv/m8sQUnGTGOw1BCmKcv77LBJY7OxhJ+zJF7UD/sCL3lYNFuqmQEVlkfS4Quif6FyYg==", + "dependencies": { + "Microsoft.NETFramework.ReferenceAssemblies.net461": "1.0.3" + } + }, + "Microsoft.SourceLink.GitHub": { + "type": "Direct", + "requested": "[10.0.401, )", + "resolved": "10.0.401", + "contentHash": "LGmlwgP1Cx37JEWzyjS0o1/+xs/s/e3E2TBSuogI5ePA/9L0pfIfeYX0k5in7Bfcw8Nn2y2sG9jXxydTjiR2Fg==", + "dependencies": { + "Microsoft.Build.Tasks.Git": "10.0.401", + "Microsoft.SourceLink.Common": "10.0.401", + "System.IO.Hashing": "10.0.12" + } + }, + "Microsoft.VisualStudio.Threading.Analyzers": { + "type": "Direct", + "requested": "[17.14.15, )", + "resolved": "17.14.15", + "contentHash": "mXQPJsbuUD2ydq4/ffd8h8tSOFCXec+2xJOVNCvXjuMOq/+5EKHq3D2m2MC2+nUaXeFMSt66VS/J4HdKBixgcw==" + }, + "PolySharp": { + "type": "Direct", + "requested": "[1.16.0, )", + "resolved": "1.16.0", + "contentHash": "3kdIIceBPumwjw279FuiVMfVENT2cGASXJgcigdySsbX2dJB8ofUgG6i47yqF/k1qu6fvNR3csrSekZPviR6kQ==" + }, + "System.Text.Encoding.CodePages": { + "type": "Direct", + "requested": "[8.0.0, )", + "resolved": "8.0.0", + "contentHash": "OZIsVplFGaVY90G2SbpgU7EnCoOO5pw1t4ic21dBF3/1omrJFpAGoNAVpPyMVOC90/hvgkGG3VFqR13YgZMQfg==", + "dependencies": { + "System.Runtime.CompilerServices.Unsafe": "6.0.0" + } + }, + "Zomp.SyncMethodGenerator": { + "type": "Direct", + "requested": "[2.0.56, )", + "resolved": "2.0.56", + "contentHash": "P1iMA1meu0uX8YcPWhkFnpzV3e+QFDDYMU6hMNyofS+pBlLgeVV7QevBmJmJ/mrxA1jujo3cqNaO1IapdfjOlA==" + }, + "Microsoft.Build.Tasks.Git": { + "type": "Transitive", + "resolved": "10.0.401", + "contentHash": "ZYctNuT10V9IYyCFydy63DXx0ggZQuynuzQOdLvW62dPgzjIz7f0ISEP75RGiq1jFQh8p6TmGSqxeQZQ87LCig==", + "dependencies": { + "System.IO.Hashing": "10.0.12" + } + }, + "Microsoft.NETFramework.ReferenceAssemblies.net461": { + "type": "Transitive", + "resolved": "1.0.3", + "contentHash": "AmOJZwCqnOCNp6PPcf9joyogScWLtwy0M1WkqfEQ0M9nYwyDD7EX9ZjscKS5iYnyvteX7kzSKFCKt9I9dXA6mA==" + }, + "Microsoft.SourceLink.Common": { + "type": "Transitive", + "resolved": "10.0.401", + "contentHash": "u3rLxIwi/9MqDFaWGE/QQgLR1NBEzLOW2lv5+9OrZPDBYIAmFdYSWCWrR1ufpXWOqFn+x02TgKropl/oDuHmgA==" + }, + "System.IO.Hashing": { + "type": "Transitive", + "resolved": "10.0.12", + "contentHash": "jDix4bBMYnpZdSPcnY+KDV6ik3SRMzpMKby/bZl/XUwIiflwRNAFZ0oOl61R/pSaveIJ8t1gs2BUlrGsPs/bcg==" + }, + "System.Runtime.CompilerServices.Unsafe": { + "type": "Transitive", + "resolved": "6.0.0", + "contentHash": "/iUeP3tq1S0XdNNoMz5C9twLSrM/TH+qElHkXWaPvuNOt+99G75NrV0OS2EqHx5wMN7popYjpc8oTjC1y16DLg==" + } + }, "net10.0": { "Microsoft.NET.ILLink.Tasks": { "type": "Direct", @@ -69,6 +404,138 @@ "resolved": "10.0.12", "contentHash": "jDix4bBMYnpZdSPcnY+KDV6ik3SRMzpMKby/bZl/XUwIiflwRNAFZ0oOl61R/pSaveIJ8t1gs2BUlrGsPs/bcg==" } + }, + "net6.0": { + "Microsoft.NETFramework.ReferenceAssemblies": { + "type": "Direct", + "requested": "[1.0.3, )", + "resolved": "1.0.3", + "contentHash": "vUc9Npcs14QsyOD01tnv/m8sQUnGTGOw1BCmKcv77LBJY7OxhJ+zJF7UD/sCL3lYNFuqmQEVlkfS4Quif6FyYg==", + "dependencies": { + "Microsoft.NETFramework.ReferenceAssemblies.net461": "1.0.3" + } + }, + "Microsoft.SourceLink.GitHub": { + "type": "Direct", + "requested": "[10.0.401, )", + "resolved": "10.0.401", + "contentHash": "LGmlwgP1Cx37JEWzyjS0o1/+xs/s/e3E2TBSuogI5ePA/9L0pfIfeYX0k5in7Bfcw8Nn2y2sG9jXxydTjiR2Fg==", + "dependencies": { + "Microsoft.Build.Tasks.Git": "10.0.401", + "Microsoft.SourceLink.Common": "10.0.401", + "System.IO.Hashing": "10.0.12" + } + }, + "Microsoft.VisualStudio.Threading.Analyzers": { + "type": "Direct", + "requested": "[17.14.15, )", + "resolved": "17.14.15", + "contentHash": "mXQPJsbuUD2ydq4/ffd8h8tSOFCXec+2xJOVNCvXjuMOq/+5EKHq3D2m2MC2+nUaXeFMSt66VS/J4HdKBixgcw==" + }, + "PolySharp": { + "type": "Direct", + "requested": "[1.16.0, )", + "resolved": "1.16.0", + "contentHash": "3kdIIceBPumwjw279FuiVMfVENT2cGASXJgcigdySsbX2dJB8ofUgG6i47yqF/k1qu6fvNR3csrSekZPviR6kQ==" + }, + "Zomp.SyncMethodGenerator": { + "type": "Direct", + "requested": "[2.0.56, )", + "resolved": "2.0.56", + "contentHash": "P1iMA1meu0uX8YcPWhkFnpzV3e+QFDDYMU6hMNyofS+pBlLgeVV7QevBmJmJ/mrxA1jujo3cqNaO1IapdfjOlA==" + }, + "Microsoft.Build.Tasks.Git": { + "type": "Transitive", + "resolved": "10.0.401", + "contentHash": "ZYctNuT10V9IYyCFydy63DXx0ggZQuynuzQOdLvW62dPgzjIz7f0ISEP75RGiq1jFQh8p6TmGSqxeQZQ87LCig==", + "dependencies": { + "System.IO.Hashing": "10.0.12" + } + }, + "Microsoft.NETFramework.ReferenceAssemblies.net461": { + "type": "Transitive", + "resolved": "1.0.3", + "contentHash": "AmOJZwCqnOCNp6PPcf9joyogScWLtwy0M1WkqfEQ0M9nYwyDD7EX9ZjscKS5iYnyvteX7kzSKFCKt9I9dXA6mA==" + }, + "Microsoft.SourceLink.Common": { + "type": "Transitive", + "resolved": "10.0.401", + "contentHash": "u3rLxIwi/9MqDFaWGE/QQgLR1NBEzLOW2lv5+9OrZPDBYIAmFdYSWCWrR1ufpXWOqFn+x02TgKropl/oDuHmgA==" + }, + "System.IO.Hashing": { + "type": "Transitive", + "resolved": "10.0.12", + "contentHash": "jDix4bBMYnpZdSPcnY+KDV6ik3SRMzpMKby/bZl/XUwIiflwRNAFZ0oOl61R/pSaveIJ8t1gs2BUlrGsPs/bcg==" + } + }, + "net8.0": { + "Microsoft.NET.ILLink.Tasks": { + "type": "Direct", + "requested": "[8.0.29, )", + "resolved": "8.0.29", + "contentHash": "HSBTfrkIZijz8z3ybLRKB7E8rHk4QQufFwpHa9fc5CMIgRhRzdn4mBGmlyXZqaueiMPtuJcnjresGvSTfaW8Mg==" + }, + "Microsoft.NETFramework.ReferenceAssemblies": { + "type": "Direct", + "requested": "[1.0.3, )", + "resolved": "1.0.3", + "contentHash": "vUc9Npcs14QsyOD01tnv/m8sQUnGTGOw1BCmKcv77LBJY7OxhJ+zJF7UD/sCL3lYNFuqmQEVlkfS4Quif6FyYg==", + "dependencies": { + "Microsoft.NETFramework.ReferenceAssemblies.net461": "1.0.3" + } + }, + "Microsoft.SourceLink.GitHub": { + "type": "Direct", + "requested": "[10.0.401, )", + "resolved": "10.0.401", + "contentHash": "LGmlwgP1Cx37JEWzyjS0o1/+xs/s/e3E2TBSuogI5ePA/9L0pfIfeYX0k5in7Bfcw8Nn2y2sG9jXxydTjiR2Fg==", + "dependencies": { + "Microsoft.Build.Tasks.Git": "10.0.401", + "Microsoft.SourceLink.Common": "10.0.401", + "System.IO.Hashing": "10.0.12" + } + }, + "Microsoft.VisualStudio.Threading.Analyzers": { + "type": "Direct", + "requested": "[17.14.15, )", + "resolved": "17.14.15", + "contentHash": "mXQPJsbuUD2ydq4/ffd8h8tSOFCXec+2xJOVNCvXjuMOq/+5EKHq3D2m2MC2+nUaXeFMSt66VS/J4HdKBixgcw==" + }, + "PolySharp": { + "type": "Direct", + "requested": "[1.16.0, )", + "resolved": "1.16.0", + "contentHash": "3kdIIceBPumwjw279FuiVMfVENT2cGASXJgcigdySsbX2dJB8ofUgG6i47yqF/k1qu6fvNR3csrSekZPviR6kQ==" + }, + "Zomp.SyncMethodGenerator": { + "type": "Direct", + "requested": "[2.0.56, )", + "resolved": "2.0.56", + "contentHash": "P1iMA1meu0uX8YcPWhkFnpzV3e+QFDDYMU6hMNyofS+pBlLgeVV7QevBmJmJ/mrxA1jujo3cqNaO1IapdfjOlA==" + }, + "Microsoft.Build.Tasks.Git": { + "type": "Transitive", + "resolved": "10.0.401", + "contentHash": "ZYctNuT10V9IYyCFydy63DXx0ggZQuynuzQOdLvW62dPgzjIz7f0ISEP75RGiq1jFQh8p6TmGSqxeQZQ87LCig==", + "dependencies": { + "System.IO.Hashing": "10.0.12" + } + }, + "Microsoft.NETFramework.ReferenceAssemblies.net461": { + "type": "Transitive", + "resolved": "1.0.3", + "contentHash": "AmOJZwCqnOCNp6PPcf9joyogScWLtwy0M1WkqfEQ0M9nYwyDD7EX9ZjscKS5iYnyvteX7kzSKFCKt9I9dXA6mA==" + }, + "Microsoft.SourceLink.Common": { + "type": "Transitive", + "resolved": "10.0.401", + "contentHash": "u3rLxIwi/9MqDFaWGE/QQgLR1NBEzLOW2lv5+9OrZPDBYIAmFdYSWCWrR1ufpXWOqFn+x02TgKropl/oDuHmgA==" + }, + "System.IO.Hashing": { + "type": "Transitive", + "resolved": "10.0.12", + "contentHash": "jDix4bBMYnpZdSPcnY+KDV6ik3SRMzpMKby/bZl/XUwIiflwRNAFZ0oOl61R/pSaveIJ8t1gs2BUlrGsPs/bcg==" + } } } } \ No newline at end of file diff --git a/tests/SharpCompress.Test/packages.lock.json b/tests/SharpCompress.Test/packages.lock.json index be63f7b4..db9a3aa7 100644 --- a/tests/SharpCompress.Test/packages.lock.json +++ b/tests/SharpCompress.Test/packages.lock.json @@ -1,6 +1,339 @@ { "version": 2, "dependencies": { + ".NETFramework,Version=v4.8": { + "AwesomeAssertions": { + "type": "Direct", + "requested": "[9.6.0, )", + "resolved": "9.6.0", + "contentHash": "8kdtgI+tZIgP46ncnk650/dU4JSrn+n2Dkic8FvjIFn72P6Nv1Afyr5F8F3PsokHg1GBSBVo1/Z+eZOrU5esSA==", + "dependencies": { + "System.Threading.Tasks.Extensions": "4.5.4" + } + }, + "Microsoft.NET.Test.Sdk": { + "type": "Direct", + "requested": "[18.10.1, )", + "resolved": "18.10.1", + "contentHash": "SxCFvJE/2ltUQgCgv6+Ixagohw4sZc9hM7Wid2foaNM7paBscktoHLoRDz91fW8wbHG87Rg9Ku59/8PXumqUrA==", + "dependencies": { + "Microsoft.CodeCoverage": "18.10.1" + } + }, + "Microsoft.NETFramework.ReferenceAssemblies": { + "type": "Direct", + "requested": "[1.0.3, )", + "resolved": "1.0.3", + "contentHash": "vUc9Npcs14QsyOD01tnv/m8sQUnGTGOw1BCmKcv77LBJY7OxhJ+zJF7UD/sCL3lYNFuqmQEVlkfS4Quif6FyYg==", + "dependencies": { + "Microsoft.NETFramework.ReferenceAssemblies.net48": "1.0.3" + } + }, + "Microsoft.SourceLink.GitHub": { + "type": "Direct", + "requested": "[10.0.401, )", + "resolved": "10.0.401", + "contentHash": "LGmlwgP1Cx37JEWzyjS0o1/+xs/s/e3E2TBSuogI5ePA/9L0pfIfeYX0k5in7Bfcw8Nn2y2sG9jXxydTjiR2Fg==", + "dependencies": { + "Microsoft.Build.Tasks.Git": "10.0.401", + "Microsoft.SourceLink.Common": "10.0.401", + "System.IO.Hashing": "10.0.12" + } + }, + "Microsoft.VisualStudio.Threading.Analyzers": { + "type": "Direct", + "requested": "[17.14.15, )", + "resolved": "17.14.15", + "contentHash": "mXQPJsbuUD2ydq4/ffd8h8tSOFCXec+2xJOVNCvXjuMOq/+5EKHq3D2m2MC2+nUaXeFMSt66VS/J4HdKBixgcw==" + }, + "PolySharp": { + "type": "Direct", + "requested": "[1.16.0, )", + "resolved": "1.16.0", + "contentHash": "3kdIIceBPumwjw279FuiVMfVENT2cGASXJgcigdySsbX2dJB8ofUgG6i47yqF/k1qu6fvNR3csrSekZPviR6kQ==" + }, + "xunit.runner.visualstudio": { + "type": "Direct", + "requested": "[3.1.5, )", + "resolved": "3.1.5", + "contentHash": "tKi7dSTwP4m5m9eXPM2Ime4Kn7xNf4x4zT9sdLO/G4hZVnQCRiMTWoSZqI/pYTVeI27oPPqHBKYI/DjJ9GsYgA==", + "dependencies": { + "Microsoft.TestPlatform.ObjectModel": "17.13.0" + } + }, + "xunit.v3": { + "type": "Direct", + "requested": "[3.2.2, )", + "resolved": "3.2.2", + "contentHash": "L+4/4y0Uqcg8/d6hfnxhnwh4j9FaeULvefTwrk30rr1o4n/vdPfyUQ8k0yzH8VJx7bmFEkDdcRfbtbjEHlaYcA==", + "dependencies": { + "xunit.v3.mtp-v1": "[3.2.2]" + } + }, + "Zomp.SyncMethodGenerator": { + "type": "Direct", + "requested": "[2.0.56, )", + "resolved": "2.0.56", + "contentHash": "P1iMA1meu0uX8YcPWhkFnpzV3e+QFDDYMU6hMNyofS+pBlLgeVV7QevBmJmJ/mrxA1jujo3cqNaO1IapdfjOlA==" + }, + "Microsoft.ApplicationInsights": { + "type": "Transitive", + "resolved": "2.23.0", + "contentHash": "nWArUZTdU7iqZLycLKWe0TDms48KKGE6pONH2terYNa8REXiqixrMOkf1sk5DHGMaUTqONU2YkS4SAXBhLStgw==", + "dependencies": { + "System.Diagnostics.DiagnosticSource": "5.0.0" + } + }, + "Microsoft.Build.Tasks.Git": { + "type": "Transitive", + "resolved": "10.0.401", + "contentHash": "ZYctNuT10V9IYyCFydy63DXx0ggZQuynuzQOdLvW62dPgzjIz7f0ISEP75RGiq1jFQh8p6TmGSqxeQZQ87LCig==", + "dependencies": { + "System.IO.Hashing": "10.0.12" + } + }, + "Microsoft.CodeCoverage": { + "type": "Transitive", + "resolved": "18.10.1", + "contentHash": "tV7tCyp/t+DuwQugLkCeUN7NBFFB4fEGRPr0gkMQ2nQgAvQQjmGlZZi1dsLzbyoe/A0mbnKybmkIh8qhT9p4Zw==" + }, + "Microsoft.NETFramework.ReferenceAssemblies.net48": { + "type": "Transitive", + "resolved": "1.0.3", + "contentHash": "zMk4D+9zyiEWByyQ7oPImPN/Jhpj166Ky0Nlla4eXlNL8hI/BtSJsgR8Inldd4NNpIAH3oh8yym0W2DrhXdSLQ==" + }, + "Microsoft.SourceLink.Common": { + "type": "Transitive", + "resolved": "10.0.401", + "contentHash": "u3rLxIwi/9MqDFaWGE/QQgLR1NBEzLOW2lv5+9OrZPDBYIAmFdYSWCWrR1ufpXWOqFn+x02TgKropl/oDuHmgA==" + }, + "Microsoft.Testing.Extensions.Telemetry": { + "type": "Transitive", + "resolved": "1.9.1", + "contentHash": "No5AudZMmSb+uNXjlgL2y3/stHD2IT4uxqc5yHwkE+/nNux9jbKcaJMvcp9SwgP4DVD8L9/P3OUz8mmmcvEIdQ==", + "dependencies": { + "Microsoft.ApplicationInsights": "2.23.0", + "Microsoft.Testing.Platform": "1.9.1", + "System.Diagnostics.DiagnosticSource": "6.0.0" + } + }, + "Microsoft.Testing.Extensions.TrxReport.Abstractions": { + "type": "Transitive", + "resolved": "1.9.1", + "contentHash": "AL46Xe1WBi85Ntd4mNPvat5ZSsZ2uejiVqoKCypr8J3wK0elA5xJ3AN4G/Q4GIwzUFnggZoH/DBjnr9J18IO/g==", + "dependencies": { + "Microsoft.Testing.Platform": "1.9.1" + } + }, + "Microsoft.Testing.Platform": { + "type": "Transitive", + "resolved": "1.9.1", + "contentHash": "QafNtNSmEI0zazdebnsIkDKmFtTSpmx/5PLOjURWwozcPb3tvRxzosQSL8xwYNM1iPhhKiBksXZyRSE2COisrA==" + }, + "Microsoft.Testing.Platform.MSBuild": { + "type": "Transitive", + "resolved": "1.9.1", + "contentHash": "oTUtyR4X/s9ytuiNA29FGsNCCH0rNmY5Wdm14NCKLjTM1cT9edVSlA+rGS/mVmusPqcP0l/x9qOnMXg16v87RQ==", + "dependencies": { + "Microsoft.Testing.Platform": "1.9.1" + } + }, + "Microsoft.TestPlatform.ObjectModel": { + "type": "Transitive", + "resolved": "17.13.0", + "contentHash": "bt0E0Dx+iqW97o4A59RCmUmz/5NarJ7LRL+jXbSHod72ibL5XdNm1Ke+UO5tFhBG4VwHLcSjqq9BUSblGNWamw==", + "dependencies": { + "System.Reflection.Metadata": "1.6.0" + } + }, + "Microsoft.Win32.Registry": { + "type": "Transitive", + "resolved": "5.0.0", + "contentHash": "dDoKi0PnDz31yAyETfRntsLArTlVAVzUzCIvvEDsDsucrl33Dl8pIJG06ePTJTI3tGpeyHS9Cq7Foc/s4EeKcg==", + "dependencies": { + "System.Security.AccessControl": "5.0.0", + "System.Security.Principal.Windows": "5.0.0" + } + }, + "System.Buffers": { + "type": "Transitive", + "resolved": "4.6.1", + "contentHash": "N8GXpmiLMtljq7gwvyS+1QvKT/W2J8sNAvx+HVg4NGmsG/H+2k/y9QI23auLJRterrzCiDH+IWAw4V/GPwsMlw==" + }, + "System.Collections.Immutable": { + "type": "Transitive", + "resolved": "6.0.0", + "contentHash": "l4zZJ1WU2hqpQQHXz1rvC3etVZN+2DLmQMO79FhOTZHMn8tDRr+WU287sbomD0BETlmKDn0ygUgVy9k5xkkJdA==", + "dependencies": { + "System.Memory": "4.5.4", + "System.Runtime.CompilerServices.Unsafe": "6.0.0" + } + }, + "System.Diagnostics.DiagnosticSource": { + "type": "Transitive", + "resolved": "6.0.0", + "contentHash": "frQDfv0rl209cKm1lnwTgFPzNigy2EKk1BS3uAvHvlBVKe5cymGyHO+Sj+NLv5VF/AhHsqPIUUwya5oV4CHMUw==", + "dependencies": { + "System.Memory": "4.5.4", + "System.Runtime.CompilerServices.Unsafe": "6.0.0" + } + }, + "System.IO.Hashing": { + "type": "Transitive", + "resolved": "10.0.12", + "contentHash": "jDix4bBMYnpZdSPcnY+KDV6ik3SRMzpMKby/bZl/XUwIiflwRNAFZ0oOl61R/pSaveIJ8t1gs2BUlrGsPs/bcg==", + "dependencies": { + "System.Buffers": "4.6.1", + "System.Memory": "4.6.3" + } + }, + "System.Memory": { + "type": "Transitive", + "resolved": "4.6.3", + "contentHash": "qdcDOgnFZY40+Q9876JUHnlHu7bosOHX8XISRoH94fwk6hgaeQGSgfZd8srWRZNt5bV9ZW2TljcegDNxsf+96A==", + "dependencies": { + "System.Buffers": "4.6.1", + "System.Numerics.Vectors": "4.6.1", + "System.Runtime.CompilerServices.Unsafe": "6.1.2" + } + }, + "System.Numerics.Vectors": { + "type": "Transitive", + "resolved": "4.6.1", + "contentHash": "sQxefTnhagrhoq2ReR0D/6K0zJcr9Hrd6kikeXsA1I8kOCboTavcUC4r7TSfpKFeE163uMuxZcyfO1mGO3EN8Q==" + }, + "System.Reflection.Metadata": { + "type": "Transitive", + "resolved": "1.6.0", + "contentHash": "COC1aiAJjCoA5GBF+QKL2uLqEBew4JsCkQmoHKbN3TlOZKa2fKLz5CpiRQKDz0RsAOEGsVKqOD5bomsXq/4STQ==", + "dependencies": { + "System.Collections.Immutable": "1.5.0" + } + }, + "System.Runtime.CompilerServices.Unsafe": { + "type": "Transitive", + "resolved": "6.1.2", + "contentHash": "2hBr6zdbIBTDE3EhK7NSVNdX58uTK6iHW/P/Axmm9sl1xoGSLqDvMtpecn226TNwHByFokYwJmt/aQQNlO5CRw==" + }, + "System.Security.AccessControl": { + "type": "Transitive", + "resolved": "5.0.0", + "contentHash": "dagJ1mHZO3Ani8GH0PHpPEe/oYO+rVdbQjvjJkBRNQkX4t0r1iaeGn8+/ybkSLEan3/slM0t59SVdHzuHf2jmw==", + "dependencies": { + "System.Security.Principal.Windows": "5.0.0" + } + }, + "System.Security.Principal.Windows": { + "type": "Transitive", + "resolved": "5.0.0", + "contentHash": "t0MGLukB5WAVU9bO3MGzvlGnyJPgUlcwerXn1kzBRjwLKixT96XV0Uza41W49gVd8zEMFu9vQEFlv0IOrytICA==" + }, + "System.Threading.Tasks.Extensions": { + "type": "Transitive", + "resolved": "4.5.4", + "contentHash": "zteT+G8xuGu6mS+mzDzYXbzS7rd3K6Fjb9RiZlYlJPam2/hU7JCBZBVEcywNuR+oZ1ncTvc/cq0faRr3P01OVg==", + "dependencies": { + "System.Runtime.CompilerServices.Unsafe": "4.5.3" + } + }, + "xunit.analyzers": { + "type": "Transitive", + "resolved": "1.27.0", + "contentHash": "y/pxIQaLvk/kxAoDkZW9GnHLCEqzwl5TW0vtX3pweyQpjizB9y3DXhb9pkw2dGeUqhLjsxvvJM1k89JowU6z3g==" + }, + "xunit.v3.assert": { + "type": "Transitive", + "resolved": "3.2.2", + "contentHash": "BPciBghgEEaJN/JG00QfCYDfEfnLgQhfnYEy+j1izoeHVNYd5+3Wm8GJ6JgYysOhpBPYGE+sbf75JtrRc7jrdA==", + "dependencies": { + "System.Collections.Immutable": "6.0.0", + "System.Memory": "4.5.5" + } + }, + "xunit.v3.common": { + "type": "Transitive", + "resolved": "3.2.2", + "contentHash": "Hj775PEH6GTbbg0wfKRvG2hNspDCvTH9irXhH4qIWgdrOSV1sQlqPie+DOvFeigsFg2fxSM3ZAaaCDQs+KreFA==", + "dependencies": { + "Microsoft.Bcl.AsyncInterfaces": "6.0.0" + } + }, + "xunit.v3.core.mtp-v1": { + "type": "Transitive", + "resolved": "3.2.2", + "contentHash": "Ga5aA2Ca9ktz+5k3g5ukzwfexwoqwDUpV6z7atSEUvqtd6JuybU1XopHqg1oFd78QdTfZgZE9h5sHpO4qYIi5w==", + "dependencies": { + "Microsoft.Testing.Extensions.Telemetry": "1.9.1", + "Microsoft.Testing.Extensions.TrxReport.Abstractions": "1.9.1", + "Microsoft.Testing.Platform": "1.9.1", + "Microsoft.Testing.Platform.MSBuild": "1.9.1", + "xunit.v3.extensibility.core": "[3.2.2]", + "xunit.v3.runner.inproc.console": "[3.2.2]" + } + }, + "xunit.v3.extensibility.core": { + "type": "Transitive", + "resolved": "3.2.2", + "contentHash": "srY8z/oMPvh/t8axtO2DwrHajhFMH7tnqKildvYrVQIfICi8fOn3yIBWkVPAcrKmHMwvXRJ/XsQM3VMR6DOYfQ==", + "dependencies": { + "xunit.v3.common": "[3.2.2]" + } + }, + "xunit.v3.mtp-v1": { + "type": "Transitive", + "resolved": "3.2.2", + "contentHash": "O41aAzYKBT5PWqATa1oEWVNCyEUypFQ4va6K0kz37dduV3EKzXNMaV2UnEhufzU4Cce1I33gg0oldS8tGL5I0A==", + "dependencies": { + "xunit.analyzers": "1.27.0", + "xunit.v3.assert": "[3.2.2]", + "xunit.v3.core.mtp-v1": "[3.2.2]" + } + }, + "xunit.v3.runner.common": { + "type": "Transitive", + "resolved": "3.2.2", + "contentHash": "/hkHkQCzGrugelOAehprm7RIWdsUFVmIVaD6jDH/8DNGCymTlKKPTbGokD5czbAfqfex47mBP0sb0zbHYwrO/g==", + "dependencies": { + "Microsoft.Win32.Registry": "[5.0.0]", + "xunit.v3.common": "[3.2.2]" + } + }, + "xunit.v3.runner.inproc.console": { + "type": "Transitive", + "resolved": "3.2.2", + "contentHash": "ulWOdSvCk+bPXijJZ73bth9NyoOHsAs1ZOvamYbCkD4DNLX/Bd29Ve2ZNUwBbK0MqfIYWXHZViy/HKrdEC/izw==", + "dependencies": { + "xunit.v3.extensibility.core": "[3.2.2]", + "xunit.v3.runner.common": "[3.2.2]" + } + }, + "sharpcompress": { + "type": "Project", + "dependencies": { + "Microsoft.Bcl.AsyncInterfaces": "[8.0.0, )", + "System.Text.Encoding.CodePages": "[8.0.0, )" + } + }, + "Microsoft.Bcl.AsyncInterfaces": { + "type": "CentralTransitive", + "requested": "[8.0.0, )", + "resolved": "8.0.0", + "contentHash": "3WA9q9yVqJp222P3x1wYIGDAkpjAku0TMUaaQV22g6L67AI0LdOIrVS7Ht2vJfLHGSPVuqN94vIr15qn+HEkHw==", + "dependencies": { + "System.Threading.Tasks.Extensions": "4.5.4" + } + }, + "System.Text.Encoding.CodePages": { + "type": "CentralTransitive", + "requested": "[8.0.0, )", + "resolved": "8.0.0", + "contentHash": "OZIsVplFGaVY90G2SbpgU7EnCoOO5pw1t4ic21dBF3/1omrJFpAGoNAVpPyMVOC90/hvgkGG3VFqR13YgZMQfg==", + "dependencies": { + "System.Memory": "4.5.5", + "System.Runtime.CompilerServices.Unsafe": "6.0.0" + } + } + }, "net10.0": { "AwesomeAssertions": { "type": "Direct",