Skip to content

System.IO.Packaging.ZipPackage returns "Stream too long" exception with big files #42855

@qmatteoq

Description

@qmatteoq

Description

I'm using the following code, based on the System.IO.Packaging.ZipPackage API, to generate zip files in a WPF application:

private void AddFileToZip(ZipPackage zipPackage, string fileName)
{
    string extension = Path.GetExtension(fileName);
    if (extension == null) return;
 
    PackagePart part = zipPackage.CreatePart(new Uri("/" + Path.GetFileName(fileName), UriKind.Relative),
        "application/" + extension.Substring(1), CompressionOption.Maximum);
    if (part == null) return;
    using (Stream partStream = part.GetStream(FileMode.Create))
    {
        using (Stream fileStream = File.OpenRead(fileName))
        {
            CopyBytes(fileStream, partStream);
            fileStream.Close();
        }
 
        partStream.Close();
    }
}
 
private void CopyBytes(Stream readStream, Stream writeStream)
{
    byte[] bytes = new byte[4096];
 
    int bytesRead = readStream.Read(bytes, 0, bytes.Length);
    while (bytesRead > 0)
    {
        writeStream.Write(bytes, 0, bytesRead);
        bytesRead = readStream.Read(bytes, 0, bytes.Length);
    }
}

This code works fine in the version of the application based on .NET Framework 4.6, regardless of the size of the file to compress. On .NET Core, instead, the operation fails with an IOException and the message "Stream too long" if the file is too big.

Configuration

  • .NET Core 3.1 / .NET 5.0 RC1
  • Windows 10 19042.51
  • x86 and x64

Regression?

Yes, the same code works fine in .NET Framework 4.6.

Other information

You can find a simple repro project here: https://1drv.ms/u/s!AhGLm1VCMcxP7t4yZ5iWm2NwKGf0Dg?e=XajuD2.
This project creates a 2 GB text file in a temporary folder and then tries to zip it using the above code. The project comes with two .csproj files: one for the .NET Framework 4.6 version, which works fine; one for the .NET Core 3.1 version, which generates the IOException.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions