-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Open
Milestone
Description
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
- Create folder with some files
- Add files to archive that
ZipArchivecan't process. For example, protect it with password - Try to extract entries from archive using
ExtractToFileAsync(destPath, overwrite: true) - After exception happens,
destinationFileNameis 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.
dmitry-azaraev