-
Notifications
You must be signed in to change notification settings - Fork 5k
Zip async implementation #114421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
carlossanlop
merged 40 commits into
dotnet:main
from
carlossanlop:ZipAsyncImplementation
Apr 17, 2025
Merged
Zip async implementation #114421
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
2161c6a
ref: System.IO.Compression
carlossanlop 4a70fb1
src: ZipHelper async
carlossanlop 7e902b1
src: ZipBlocks async
carlossanlop be1ef0b
src: ZipArchiveEntry async
carlossanlop 5939611
src: ZipArchive async
carlossanlop e63bfbb
src: System.IO.Compression.csproj
carlossanlop 76e2d4e
ref: System.IO.Compression.ZipFile
carlossanlop d4c4fd9
src: ZipFile.Create async
carlossanlop b723b9f
src: ZipFile.Extract async
carlossanlop c7319fa
src: ZipFileExtensions.ZipArchive.Create async
carlossanlop d889936
src: ZipFileExtensions.ZipArchive.Extract async
carlossanlop b25e514
src: ZipFileExtensions.ZipArchiveEntry.Extract async
carlossanlop 7848832
src: System.IO.Compression.ZipFile.csproj
carlossanlop 822fe91
test: Common async
carlossanlop 7f1d854
tests: ZipArchive async
carlossanlop cbaf380
tests: ZipTestHelper.ZipFile async
carlossanlop f0b7624
tests: ZipFile async
carlossanlop fc0e102
tests: ZipFileExtensions async
carlossanlop 4f0a6b0
tests: The rest of the tests adapted to async
carlossanlop f285d1e
Remove unnecessary GC.SuppressFinalize calls in Dispose methods.
carlossanlop f84bf1d
tests: Add tests that compare output of sync and async.
carlossanlop 5e65586
src: Add missed overloaded method in ZipFileExtensions.
carlossanlop 093238e
src: Share some more code in ZipFileExtensions, add missing docs
carlossanlop 2148fd5
src: Increase bufferSize in ZipFileExtensions FileStream to 0x4000 (1…
carlossanlop 4eefc87
src: Avoid nullable archiveStream in new constructor.
carlossanlop 7efc9ac
src: Remove default cancellation token in private and internal method…
carlossanlop 722de82
src: DceideArchiveStream should throw expected exception if stream is…
carlossanlop 7a9f252
src: Simplify GetFileStreamForOpen switch.
carlossanlop 82dbf3b
src: Share the file stream buffer size. Move the filestream comment t…
carlossanlop 59484c0
src: ZipBlocks put together two checks that return false.
carlossanlop c629fa3
src: Simplify return values in async code and shared code.
carlossanlop fda1978
Fuzz test for sync and async
carlossanlop f3566a6
tests: Add special tests that confirm that the new async APIs are not…
carlossanlop d4509c5
src: Fix bugs found with the tests that check if async APIs are not c…
carlossanlop 0ad6c39
src: Fix test method rename build failure
carlossanlop a87d789
tests: Remove two of the four outerloop tests as they're too heavy fo…
carlossanlop 6afdb55
src: Implement the missing async methods in internal helper stream cl…
carlossanlop 0b6e6fe
src: Fix NullReferenceException and static initialization issues in s…
carlossanlop 7458a57
tests: revert zipfile unix tests as they use remote executor and when…
carlossanlop cb1e358
tests: Split the ExtractOutOfRoot tests into sync and async instead o…
carlossanlop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/libraries/Common/tests/System/IO/Compression/NoAsyncCallsStream.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace System.IO.Compression.Tests; | ||
|
||
// A stream meant to be used for testing that an implementation's sync methods do not accidentally call any async methods. | ||
internal sealed class NoAsyncCallsStream : Stream | ||
{ | ||
private readonly Stream _s; | ||
|
||
public NoAsyncCallsStream(Stream stream) => _s = stream; | ||
|
||
// Allows temporarily disabling the current stream's sync API usage restriction. | ||
public bool IsRestrictionEnabled { get; set; } | ||
|
||
public override bool CanRead => _s.CanRead; | ||
public override bool CanSeek => _s.CanSeek; | ||
public override bool CanTimeout => _s.CanTimeout; | ||
public override bool CanWrite => _s.CanWrite; | ||
public override long Length => _s.Length; | ||
public override long Position { get => _s.Position; set => _s.Position = value; } | ||
public override int ReadTimeout { get => _s.ReadTimeout; set => _s.ReadTimeout = value; } | ||
public override int WriteTimeout { get => _s.WriteTimeout; set => _s.WriteTimeout = value; } | ||
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => _s.BeginRead(buffer, offset, count, callback, state); | ||
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => _s.BeginWrite(buffer, offset, count, callback, state); | ||
public override void Close() => _s.Close(); | ||
public override int EndRead(IAsyncResult asyncResult) => _s.EndRead(asyncResult); | ||
public override void EndWrite(IAsyncResult asyncResult) => _s.EndWrite(asyncResult); | ||
public override bool Equals(object? obj) => _s.Equals(obj); | ||
public override int GetHashCode() => _s.GetHashCode(); | ||
public override int ReadByte() => _s.ReadByte(); | ||
public override long Seek(long offset, SeekOrigin origin) => _s.Seek(offset, origin); | ||
public override void SetLength(long value) => _s.SetLength(value); | ||
public override string? ToString() => _s.ToString(); | ||
|
||
// Sync | ||
public override void CopyTo(Stream destination, int bufferSize) => _s.CopyTo(destination, bufferSize); | ||
protected override void Dispose(bool disposing) => _s.Dispose(); | ||
public override void Flush() => _s.Flush(); | ||
public override int Read(byte[] buffer, int offset, int count) => _s.Read(buffer, offset, count); | ||
public override int Read(Span<byte> buffer) => _s.Read(buffer); | ||
public override void Write(byte[] buffer, int offset, int count) => _s.Write(buffer, offset, count); | ||
public override void Write(ReadOnlySpan<byte> buffer) => _s.Write(buffer); | ||
public override void WriteByte(byte value) => _s.WriteByte(value); | ||
|
||
// Async | ||
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) => | ||
IsRestrictionEnabled ? throw new InvalidOperationException() : _s.CopyToAsync(destination, bufferSize, cancellationToken); | ||
public override ValueTask DisposeAsync() => | ||
IsRestrictionEnabled ? throw new InvalidOperationException() : _s.DisposeAsync(); | ||
public override Task FlushAsync(CancellationToken cancellationToken) => | ||
IsRestrictionEnabled ? throw new InvalidOperationException() : _s.FlushAsync(); | ||
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => | ||
IsRestrictionEnabled ? throw new InvalidOperationException() : _s.ReadAsync(buffer, offset, count, cancellationToken); | ||
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) => | ||
IsRestrictionEnabled ? throw new InvalidOperationException() : _s.ReadAsync(buffer, cancellationToken); | ||
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => | ||
IsRestrictionEnabled ? throw new InvalidOperationException() : _s.WriteAsync(buffer, offset, count, cancellationToken); | ||
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default) => | ||
IsRestrictionEnabled ? throw new InvalidOperationException() : _s.WriteAsync(buffer, cancellationToken); | ||
} |
132 changes: 132 additions & 0 deletions
132
src/libraries/Common/tests/System/IO/Compression/NoSyncCallsStream.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace System.IO.Compression.Tests; | ||
|
||
// A stream meant to be used for testing that an implementation's async methods do not accidentally call any sync methods. | ||
internal sealed class NoSyncCallsStream : Stream | ||
{ | ||
private readonly Stream _s; | ||
|
||
public NoSyncCallsStream(Stream stream) => _s = stream; | ||
|
||
// Allows temporarily disabling the current stream's sync API usage restriction. | ||
public bool IsRestrictionEnabled { get; set; } | ||
|
||
public override bool CanRead => _s.CanRead; | ||
public override bool CanSeek => _s.CanSeek; | ||
public override bool CanTimeout => _s.CanTimeout; | ||
public override bool CanWrite => _s.CanWrite; | ||
public override long Length => _s.Length; | ||
public override long Position { get => _s.Position; set => _s.Position = value; } | ||
public override int ReadTimeout { get => _s.ReadTimeout; set => _s.ReadTimeout = value; } | ||
public override int WriteTimeout { get => _s.WriteTimeout; set => _s.WriteTimeout = value; } | ||
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => _s.BeginRead(buffer, offset, count, callback, state); | ||
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => _s.BeginWrite(buffer, offset, count, callback, state); | ||
public override void Close() => _s.Close(); | ||
public override int EndRead(IAsyncResult asyncResult) => _s.EndRead(asyncResult); | ||
public override void EndWrite(IAsyncResult asyncResult) => _s.EndWrite(asyncResult); | ||
public override bool Equals(object? obj) => _s.Equals(obj); | ||
public override int GetHashCode() => _s.GetHashCode(); | ||
public override int ReadByte() => _s.ReadByte(); | ||
public override long Seek(long offset, SeekOrigin origin) => _s.Seek(offset, origin); | ||
public override void SetLength(long value) => _s.SetLength(value); | ||
public override string? ToString() => _s.ToString(); | ||
|
||
// Sync | ||
public override void CopyTo(Stream destination, int bufferSize) | ||
{ | ||
if (IsRestrictionEnabled) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
else | ||
{ | ||
_s.CopyTo(destination, bufferSize); | ||
} | ||
} | ||
protected override void Dispose(bool disposing) | ||
{ | ||
// _disposing = true; | ||
if (IsRestrictionEnabled) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
else | ||
{ | ||
_s.Dispose(); | ||
} | ||
} | ||
public override void Flush() | ||
{ | ||
if (IsRestrictionEnabled) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
else | ||
{ | ||
_s.Flush(); | ||
} | ||
} | ||
public override int Read(byte[] buffer, int offset, int count) => | ||
IsRestrictionEnabled ? throw new InvalidOperationException() : _s.Read(buffer, offset, count); | ||
public override int Read(Span<byte> buffer) => | ||
IsRestrictionEnabled ? throw new InvalidOperationException() : _s.Read(buffer); | ||
public override void Write(byte[] buffer, int offset, int count) | ||
{ | ||
bool isDeflateStream = false; | ||
|
||
// Get the stack trace to determine the calling method | ||
var stackTrace = new System.Diagnostics.StackTrace(); | ||
var callingMethod = stackTrace.GetFrame(1)?.GetMethod(); | ||
|
||
// Check if the calling method belongs to the DeflateStream class | ||
if (callingMethod?.DeclaringType == typeof(System.IO.Compression.DeflateStream)) | ||
{ | ||
isDeflateStream = true; | ||
} | ||
|
||
if (!isDeflateStream && IsRestrictionEnabled) | ||
{ | ||
throw new InvalidOperationException($"Parent class is {callingMethod.DeclaringType}"); | ||
} | ||
else | ||
{ | ||
_s.Write(buffer, offset, count); | ||
} | ||
} | ||
public override void Write(ReadOnlySpan<byte> buffer) | ||
{ | ||
if (IsRestrictionEnabled) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
else | ||
{ | ||
_s.Write(buffer); | ||
} | ||
} | ||
public override void WriteByte(byte value) | ||
{ | ||
if (IsRestrictionEnabled) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
else | ||
{ | ||
_s.WriteByte(value); | ||
} | ||
} | ||
|
||
// Async | ||
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) => _s.CopyToAsync(destination, bufferSize, cancellationToken); | ||
public override ValueTask DisposeAsync() => _s.DisposeAsync(); | ||
public override Task FlushAsync(CancellationToken cancellationToken) => _s.FlushAsync(cancellationToken); | ||
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => _s.ReadAsync(buffer, offset, count, cancellationToken); | ||
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) => _s.ReadAsync(buffer, cancellationToken); | ||
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => _s.WriteAsync(buffer, offset, count, cancellationToken); | ||
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default) => _s.WriteAsync(buffer, cancellationToken); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.