[PR #1018] Fix ExtractToDirectory file corruption on overwrite #1444

Open
opened 2026-01-29 22:20:36 +00:00 by claunia · 0 comments
Owner

Original Pull Request: https://github.com/adamhathcock/sharpcompress/pull/1018

State: closed
Merged: No


ExtractToDirectory uses File.OpenWrite() which doesn't truncate existing files. When extracting over a directory with existing files, if the new content is shorter, old data remains at the end causing corruption.

Changes

  • IArchiveExtensions.cs: Changed File.OpenWrite(path) to File.Create(path) to ensure files are truncated before writing
  • ZipArchiveTests.cs: Added test Zip_Deflate_ArchiveExtractToDirectory_Overwrite that verifies correct behavior when extracting twice to the same directory

Example

// Create a 20-byte file
File.WriteAllText("test.txt", "12345678901234567890");

// File.OpenWrite leaves old data when writing 5 bytes
using (var fs = File.OpenWrite("test.txt"))
    fs.Write(Encoding.UTF8.GetBytes("ABCDE"));
// Result: "ABCDE678901234567890" (15 extra bytes)

// File.Create properly truncates
using (var fs = File.Create("test.txt"))
    fs.Write(Encoding.UTF8.GetBytes("ABCDE"));
// Result: "ABCDE" (correct)

Note: The reported issue of "no files extracted" could not be reproduced. The fix addresses the file truncation bug which could manifest as apparent extraction failures in applications that validate extracted content.

Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug][0.41.0] ZipArchive.ExtractToDirectory method not work</issue_title>
<issue_description>Recently,I update SharpComporess from 0.40.0 to 0.41.0 in my project.
I found ZipArchive.ExtractToDirectory method not work.
No file extracted and no exception throwed,nothing happen.

Test Zip file: https://builds.dotnet.microsoft.com/dotnet/aspnetcore/Runtime/10.0.0/aspnetcore-runtime-10.0.0-win-x64.zip
Test code:

using (var archive = SharpCompress.Archives.Zip.ZipArchive.Open("aspnetcore-runtime-10.0.0-win-x64.zip")) archive.ExtractToDirectory("Test");</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

**Original Pull Request:** https://github.com/adamhathcock/sharpcompress/pull/1018 **State:** closed **Merged:** No --- `ExtractToDirectory` uses `File.OpenWrite()` which doesn't truncate existing files. When extracting over a directory with existing files, if the new content is shorter, old data remains at the end causing corruption. ## Changes - **IArchiveExtensions.cs**: Changed `File.OpenWrite(path)` to `File.Create(path)` to ensure files are truncated before writing - **ZipArchiveTests.cs**: Added test `Zip_Deflate_ArchiveExtractToDirectory_Overwrite` that verifies correct behavior when extracting twice to the same directory ## Example ```csharp // Create a 20-byte file File.WriteAllText("test.txt", "12345678901234567890"); // File.OpenWrite leaves old data when writing 5 bytes using (var fs = File.OpenWrite("test.txt")) fs.Write(Encoding.UTF8.GetBytes("ABCDE")); // Result: "ABCDE678901234567890" (15 extra bytes) // File.Create properly truncates using (var fs = File.Create("test.txt")) fs.Write(Encoding.UTF8.GetBytes("ABCDE")); // Result: "ABCDE" (correct) ``` Note: The reported issue of "no files extracted" could not be reproduced. The fix addresses the file truncation bug which could manifest as apparent extraction failures in applications that validate extracted content. <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>[Bug][0.41.0] ZipArchive.ExtractToDirectory method not work</issue_title> > <issue_description>Recently,I update SharpComporess from 0.40.0 to 0.41.0 in my project. > I found ZipArchive.ExtractToDirectory method not work. > No file extracted and no exception throwed,nothing happen. > > Test Zip file: https://builds.dotnet.microsoft.com/dotnet/aspnetcore/Runtime/10.0.0/aspnetcore-runtime-10.0.0-win-x64.zip > Test code: > > `using (var archive = SharpCompress.Archives.Zip.ZipArchive.Open("aspnetcore-runtime-10.0.0-win-x64.zip")) > archive.ExtractToDirectory("Test");`</issue_description> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > </comments> > </details> - Fixes adamhathcock/sharpcompress#1016 <!-- START COPILOT CODING AGENT TIPS --> --- 💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs.
claunia added the pull-request label 2026-01-29 22:20:36 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#1444