Skip to content

ZipArchiveEntry.ExtractToFile corrupt destination files on exceptionΒ #123059

@JCFruit

Description

@JCFruit

Description

When calling ZipArchiveEntry.ExtractToFile extension method with overwrite set to true, if ZipArchiveEntry.Open results in exception, existing destination file content is erased and file weights 0 bytes.

Reproduction Steps

  1. Create folder with some files
  2. Add files to archive that ZipArchive can't process. For example, protect it with password
  3. Try to extract entries from archive using ExtractToFileAsync(destPath, overwrite: true)
  4. After exception happens, destinationFileName is 0 bytes
using System.IO.Compression;

const string archive = @"D:\Tmp\MalformedArchive.zip";
const string folder = @"D:\Tmp\TestFolder";

await using var zipArchive = new ZipArchive(File.OpenRead(archive), ZipArchiveMode.Read, leaveOpen: false);

foreach (var zipArchiveEntry in zipArchive.Entries)
{
    var entryPath = zipArchiveEntry.FullName;
    
    var filePath = Path.Combine(folder, entryPath);
    
    await zipArchiveEntry.ExtractToFileAsync(filePath, true);
}

Expected behavior

On Exception to open the archive, original file is left intact

Actual behavior

On Exception to open the archive, original file content is deleted

Regression?

I don't know

Known Workarounds

No response

Configuration

  • First I encountered this bug in Technitium DNS (.Net 9) running in LXC container
  • I reproduced bug on Windows 10 using .Net 10

Other information

It probably happens because of this.

First, it's creating FileStream with FileMode.Create, and then it calls ZipArchiveEntry.Open. So by the time exception happens, original file is already overwritten.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions