Skip to content

ZipArchive.Open will read non seakable stream to memory & fail with bad error on large streams #59027

Open

Description

@ayende

Description

NetworkStream stream = await GetStreamFromUrl(remoteUrlFor_LARGE_file);
ZipArchive.Open(stream, ZipArchiveMode.Read); // <-- will fail here with `Stream was too long.`

Analysis

Reason for this is here:

extraTempStream = stream = new MemoryStream();

When we pass a non seekable stream to ZipArchive - it will read it into a MemoryStream. That will only work if the data can fit in MemoryStream. I assume that this is because the Zip format requires seeking (directory, etc).

However, this is very surprising and can cause both performance issues due to loading all contents to memory and unexpected failures if most of the data is < 2GB.

I'm not sure if there is a way to fix this, given backward compact issues.

Activity

  1. added
    tenet-performance
    on Sep 13, 2021
  2. ghost added
    untriaged
    on Sep 13, 2021
  3. adamsitnik commented on Sep 13, 2021

    @adamsitnik
    Member

    @carlossanlop this is something that we should consider for the .NET 7 Compression work as it's related to supporting large files (> 2GB)

  4. removed
    untriaged
    on Sep 13, 2021
  5. added this to the 7.0.0 milestone on Sep 13, 2021
  6. jnm2 commented on Sep 19, 2021

    @jnm2
    Contributor

    This is particularly bad when the stream being passed to the ZipArchive constructor is a 10-15 minute download and you want to be processing the contents as they become available.

    Please consider ZipArchive.OpenAsync or zipArchive.GetEntriesAsync to avoid blocking on I/O, too.

    The first thing ZipArchive currently does with a seekable stream is Seek(-18, SeekOrigin.End). I'm hoping that can be avoided for non-seekable streams. I'm rather worried that there's no help for it given that .zip files place the directory at the end of the stream. https://en.wikipedia.org/wiki/ZIP_(file_format)#Structure mentions that scanning for file entry headers isn't necessarily valid because a file may have been deleted from the directory. However, I would guess the vast majority of .zip files are built once and never deleted from, and thus would be packed with no unused space around the file entries.

    // This seeks backwards almost to the beginning of the EOCD, one byte after where the signature would be
    // located if the EOCD had the minimum possible size (no file zip comment)
    _archiveStream.Seek(-ZipEndOfCentralDirectoryBlock.SizeOfBlockWithoutSignature, SeekOrigin.End);

  7. jnm2 commented on Sep 20, 2021

    @jnm2
    Contributor

    https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT mentions streaming .zip files and says that every local file header must have an entry in the central directory. So that takes care of the deleted files concern and shows that streaming is a known use case.

    4.3.2 Each file placed into a ZIP file MUST be preceded by a "local file header" record for that file. Each "local file header" MUST be accompanied by a corresponding "central directory header" record within the central directory section of the ZIP file.

    4.3.5 File data MAY be followed by a "data descriptor" for the file. Data descriptors are used to facilitate ZIP file streaming.

  8. jnm2 commented on Sep 21, 2021

    @jnm2
    Contributor

    To get myself unblocked, I created a proof of concept which successfully reads a streaming .zip file: https://gist.github.com/jnm2/31bdf08357a44c91d01736ad43b9c447

    await using var reader = new StreamingZipReader(downloadStream);
    
    while (await reader.MoveToNextEntryAsync(skipDirectories: true, CancellationToken.None))
    {
        Console.WriteLine($"{reader.CurrentEntry.Name}: {reader.CurrentEntry.Length} bytes");
    
        using var stream = reader.GetCurrentEntryStream();
        using var testReader = new StreamReader(stream);
        var test = await testReader.ReadToEndAsync();
        // (my test download had only text files, and they all looked right!)
    }
  9. modified the milestones: 7.0.0, 8.0.0 on Jul 9, 2022
  10. modified the milestones: 8.0.0, Future on Jul 20, 2023
  11. bjornharrtell commented on May 26, 2024

    @bjornharrtell
    Contributor

    FWIW with permission from @jnm2 I've published StreamingZipReader as nuget https://www.nuget.org/packages/StreamingZipReader and recently fixed a bug with regard to ZIP64 support.

  12. bjornharrtell commented on May 23, 2025

    @bjornharrtell
    Contributor

    Did #114421 also fix this? I find it hard to fully understand the capabilities of that new stuff. @carlossanlop?

  13. adamsitnik commented on May 27, 2025

    @adamsitnik
    Member

    Did #114421 also fix this? I find it hard to fully understand the capabilities of that new stuff. @carlossanlop?

    #114421 has introduced a set of new async APIs, but non-seekable inputs are still being loaded into memory

  14. karelz commented on Oct 13, 2025

    @karelz
    Member

    Triage: We wonder if streaming APIs might be sufficient workaround for this issue: #1550
    Otherwise, we would have to handle larger amounts of memory buffered, which does not seem very desirable.
    Any thoughts here?

  15. added
    enhancement
    on Oct 13, 2025
  16. added theissue type on Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.IO.Compressionenhancementtenet-performance

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions