Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bubibubi opened this issue Apr 1, 2025 · 3 comments
Closed

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

bubibubi opened this issue Apr 1, 2025 · 3 comments

Comments

@bubibubi
Copy link

bubibubi commented Apr 1, 2025

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

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Apr 1, 2025
Copy link
Contributor

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

@ericstj
Copy link
Member

ericstj commented Apr 4, 2025

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

@ericstj ericstj closed this as completed Apr 4, 2025
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Apr 4, 2025
@bubibubi
Copy link
Author

bubibubi commented Apr 4, 2025

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

@github-actions github-actions bot locked and limited conversation to collaborators May 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants