Skip to content

Add support to StreamsComparer for non-seekable streams #4476

Closed
@RenderMichael

Description

@RenderMichael

For example, ZipArchiveEntry.Open() opens unseekable steams

[Test]
public void ZipArchiveTest()
{
    const string fileContentString = "Hello!";
    using var expectedFile = new MemoryStream(Encoding.UTF8.GetBytes(fileContentString));
 
    using var archiveContents = new MemoryStream();
    using ZipArchive actualArchive = GetZipArchiveFromSomewhere(archiveContents); // opened in read mode
    ZipArchiveEntry entry = actualArchive.Entries[0];

    using Stream entryStream = entry.Open(); // an archive in read mode returns a DeflateStream, which is unseekable
    Assert.That(entryStream, Is.EqualTo(expectedFile)); // System.ArgumentException : Stream is not seekable (Parameter 'actual')

    static ZipArchive GetZipArchiveFromSomewhere(MemoryStream archiveContents)
    {
        using (var archive = new ZipArchive(archiveContents, ZipArchiveMode.Create, leaveOpen: true))
        {
            ZipArchiveEntry demoFile = archive.CreateEntry("Hello entry");

            using Stream entryStream = demoFile.Open();

            using var entryFs = new StreamWriter(entryStream);
            entryFs.Write(fileContentString);
        }

        return new ZipArchive(archiveContents, ZipArchiveMode.Read, leaveOpen: false);
    }
}

I can work on a PR, let me know if this is something worth pursuing.

edit: improved the repro to be all in-memory

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions