Skip to content

Commit b5b1b07

Browse files
authored
fix(zip): skip reading position for non-seekable async streams (#754)
1 parent d843d6d commit b5b1b07

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -521,9 +521,10 @@ internal void PutNextEntry(Stream stream, ZipEntry entry, long streamOffset = 0,
521521
public async Task PutNextEntryAsync(ZipEntry entry, CancellationToken ct = default)
522522
{
523523
if (curEntry != null) await CloseEntryAsync(ct);
524+
var position = CanPatchEntries ? baseOutputStream_.Position : -1;
524525
await baseOutputStream_.WriteProcToStreamAsync(s =>
525526
{
526-
PutNextEntry(s, entry, baseOutputStream_.Position);
527+
PutNextEntry(s, entry, position);
527528
}, ct);
528529

529530
if (!entry.IsCrypted) return;

test/ICSharpCode.SharpZipLib.Tests/TestSupport/Streams.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,15 @@ public class MemoryStreamWithoutSeek : TrackedMemoryStream
177177
/// </summary>
178178
/// <value></value>
179179
/// <returns>true if the stream is open.</returns>
180-
public override bool CanSeek
180+
public override bool CanSeek => false;
181+
182+
/// <inheritdoc />
183+
public override long Position
181184
{
182-
get
183-
{
184-
return false;
185-
}
185+
get => throw new NotSupportedException("Getting position is not supported");
186+
set => throw new NotSupportedException("Setting position is not supported");
186187
}
188+
187189
}
188190

189191
/// <summary>

test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,10 @@ public void ReadAndWriteZip64NonSeekable()
111111
outStream.Close();
112112
}
113113

114-
Assert.That(msw.ToArray(), Does.PassTestArchive());
115-
116-
msw.Position = 0;
114+
var msBytes = msw.ToArray();
115+
Assert.That(msBytes, Does.PassTestArchive());
117116

118-
using (var zis = new ZipInputStream(msw))
117+
using (var zis = new ZipInputStream(new MemoryStream(msBytes)))
119118
{
120119
while (zis.GetNextEntry() != null)
121120
{

test/ICSharpCode.SharpZipLib.Tests/Zip/ZipStreamAsyncTests.cs

+21
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,26 @@ public async Task WriteZipStreamWithZipCryptoAsync()
9797
ZipTesting.AssertValidZip(ms, password, false);
9898
}
9999

100+
[Test]
101+
[Category("Zip")]
102+
[Category("Async")]
103+
public async Task WriteReadOnlyZipStreamAsync ()
104+
{
105+
using var ms = new MemoryStreamWithoutSeek();
106+
107+
using(var outStream = new ZipOutputStream(ms) { IsStreamOwner = false })
108+
{
109+
await outStream.PutNextEntryAsync(new ZipEntry("FirstFile"));
110+
await Utils.WriteDummyDataAsync(outStream, 12);
111+
112+
await outStream.PutNextEntryAsync(new ZipEntry("SecondFile"));
113+
await Utils.WriteDummyDataAsync(outStream, 12);
114+
115+
await outStream.FinishAsync(CancellationToken.None);
116+
}
117+
118+
ZipTesting.AssertValidZip(new MemoryStream(ms.ToArray()));
119+
}
120+
100121
}
101122
}

0 commit comments

Comments
 (0)