Skip to content

System.IO.Packaging - net8 - Package.Flush not flushing all data #114114

Closed as duplicate

Description

@bubibubi

Description

net8.0
Calling Package.Flush does not flush.
Calling Package.Close works.

net48
Works fine

Reproduction Steps

Make console application with 2 Target Frameworks
Running net48 executable works, running net8.0 executable the MyFile.zip is empty

MyProj.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>net48;net8.0</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.IO.Packaging" Version="9.0.3" />
  </ItemGroup>

</Project>

Program.cs

using System;
using System.IO;
using System.IO.Packaging;

class Program
{

    public static void Main()
    {
        var ms = new MemoryStream();

        Package package = Package.Open(ms, FileMode.OpenOrCreate);

        Uri partUri = new Uri("/example.txt", UriKind.Relative);

        PackagePart packagePart = package.CreatePart(partUri, "text/plain");

        using (StreamWriter writer = new StreamWriter(packagePart.GetStream(FileMode.Create, FileAccess.Write)))
        {
            writer.Write("Hello, world!");
        }

        // Replacing package.Flush with package.Close() everything works fine
        package.Flush();


        File.WriteAllBytes("MyFile.zip", ms.GetBuffer());

    }
}

Expected behavior

Save the right content without closing the package

Actual behavior

The file is truncated (or empty)

Regression?

It works with net48

Known Workarounds

No response

Configuration

Which version of .NET is the code running on?
net8.0 (it works with net48
What OS and version, and what distro if applicable?
Windows 11
What is the architecture (x64, x86, ARM, ARM64)?
x64
Do you know whether it is specific to that configuration?
It is specific with net8.0 configuration. net48 works*

Other information

No response

Activity

  1. added
    untriaged
    on Apr 1, 2025
  2. dotnet-policy-service commented on Apr 1, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-io-compression
    See info in area-owners.md if you want to be subscribed.

  3. ericstj commented on Apr 4, 2025

    @ericstj
    Member

    This is due to the behavior of the ZipArchive implementation on .NETCore. Zip only ever writes the archive information on close.
    https://github.com/dotnet/runtime/blob/15872212c29cecc8d82da4548c3060f2614665f7/src/libraries/System.IO.Compression/src/System/IO/Compression/ZipArchive.cs#L314C29-L314C38

    On .NETFramework System.IO.Packaging had an entirely different implementation which would write the archive on flush:
    https://referencesource.microsoft.com/#WindowsBase/Base/MS/Internal/IO/Zip/ZipArchive.cs,348

    I think this is a duplicate of #24149

  4. removed
    untriaged
    on Apr 4, 2025
  5. bubibubi commented on Apr 4, 2025

    @bubibubi
    Author

    Not so sure it's a duplicate. Flush is implemented but it's not working... It's quite different.

  6. locked and limited conversation to collaborators on May 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions