Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,9 @@ 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)
if (IsRestrictionEnabled)
{
throw new InvalidOperationException($"Parent class is {callingMethod.DeclaringType}");
throw new InvalidOperationException();
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,20 @@ protected override void Dispose(bool disposing)
}
base.Dispose(disposing);
}

public override async ValueTask DisposeAsync()
{
if (!_isDisposed)
{
_onClosed?.Invoke(_zipArchiveEntry);

if (_closeBaseStream)
await _baseStream.DisposeAsync().ConfigureAwait(false);

_isDisposed = true;
}
await base.DisposeAsync().ConfigureAwait(false);
}
}

internal sealed class SubReadStream : Stream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1572,7 +1572,7 @@ public static async Task NoAsyncCallsWhenUsingSync()
public static async Task NoSyncCallsWhenUsingAsync()
{
using MemoryStream ms = new();
using NoSyncCallsStream s = new(ms); // Only allows async calls
using NoSyncCallsStream s = new(ms) { IsRestrictionEnabled = true }; // Only allows async calls

// Create mode
await using (ZipArchive archive = await ZipArchive.CreateAsync(s, ZipArchiveMode.Create, leaveOpen: true, entryNameEncoding: Encoding.UTF8))
Expand All @@ -1593,8 +1593,8 @@ public static async Task NoSyncCallsWhenUsingAsync()
// Note the parent archive is not using NoSyncCallsStream, so it can be opened in sync mode
using (Stream normalEntryStream = normalEntry.Open())
{
// Note the parent archive is not using NoSyncCallsStream, so it can be copied in sync mode
normalEntryStream.CopyTo(newEntryStream);
// Use async CopyTo when writing to NoSyncCallsStream
await normalEntryStream.CopyToAsync(newEntryStream);
}
}
}
Expand Down
Loading