Skip to content

Commit

Permalink
fix(zip): skip reading position for non-seekable async streams (#754)
Browse files Browse the repository at this point in the history
  • Loading branch information
piksel authored May 24, 2022
1 parent d843d6d commit b5b1b07
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,10 @@ internal void PutNextEntry(Stream stream, ZipEntry entry, long streamOffset = 0,
public async Task PutNextEntryAsync(ZipEntry entry, CancellationToken ct = default)
{
if (curEntry != null) await CloseEntryAsync(ct);
var position = CanPatchEntries ? baseOutputStream_.Position : -1;
await baseOutputStream_.WriteProcToStreamAsync(s =>
{
PutNextEntry(s, entry, baseOutputStream_.Position);
PutNextEntry(s, entry, position);
}, ct);

if (!entry.IsCrypted) return;
Expand Down
12 changes: 7 additions & 5 deletions test/ICSharpCode.SharpZipLib.Tests/TestSupport/Streams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,15 @@ public class MemoryStreamWithoutSeek : TrackedMemoryStream
/// </summary>
/// <value></value>
/// <returns>true if the stream is open.</returns>
public override bool CanSeek
public override bool CanSeek => false;

/// <inheritdoc />
public override long Position
{
get
{
return false;
}
get => throw new NotSupportedException("Getting position is not supported");
set => throw new NotSupportedException("Setting position is not supported");
}

}

/// <summary>
Expand Down
7 changes: 3 additions & 4 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,10 @@ public void ReadAndWriteZip64NonSeekable()
outStream.Close();
}

Assert.That(msw.ToArray(), Does.PassTestArchive());

msw.Position = 0;
var msBytes = msw.ToArray();
Assert.That(msBytes, Does.PassTestArchive());

using (var zis = new ZipInputStream(msw))
using (var zis = new ZipInputStream(new MemoryStream(msBytes)))
{
while (zis.GetNextEntry() != null)
{
Expand Down
21 changes: 21 additions & 0 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/ZipStreamAsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,26 @@ public async Task WriteZipStreamWithZipCryptoAsync()
ZipTesting.AssertValidZip(ms, password, false);
}

[Test]
[Category("Zip")]
[Category("Async")]
public async Task WriteReadOnlyZipStreamAsync ()
{
using var ms = new MemoryStreamWithoutSeek();

using(var outStream = new ZipOutputStream(ms) { IsStreamOwner = false })
{
await outStream.PutNextEntryAsync(new ZipEntry("FirstFile"));
await Utils.WriteDummyDataAsync(outStream, 12);

await outStream.PutNextEntryAsync(new ZipEntry("SecondFile"));
await Utils.WriteDummyDataAsync(outStream, 12);

await outStream.FinishAsync(CancellationToken.None);
}

ZipTesting.AssertValidZip(new MemoryStream(ms.ToArray()));
}

}
}

0 comments on commit b5b1b07

Please sign in to comment.