[PR #1018] [CLOSED] Fix ExtractToDirectory file corruption on overwrite #1439

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

📋 Pull Request Information

Original PR: https://github.com/adamhathcock/sharpcompress/pull/1018
Author: @Copilot
Created: 11/14/2025
Status: Closed

Base: masterHead: copilot/fix-ziparchive-extraction-issue


📝 Commits (2)

  • 0ecbe02 Initial plan
  • dd242cb Fix ExtractToDirectory to properly truncate existing files

📊 Changes

2 files changed (+23 additions, -1 deletions)

View changed files

📝 src/SharpCompress/Archives/IArchiveExtensions.cs (+1 -1)
📝 tests/SharpCompress.Test/Zip/ZipArchiveTests.cs (+22 -0)

📄 Description

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.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/adamhathcock/sharpcompress/pull/1018 **Author:** [@Copilot](https://github.com/apps/copilot-swe-agent) **Created:** 11/14/2025 **Status:** ❌ Closed **Base:** `master` ← **Head:** `copilot/fix-ziparchive-extraction-issue` --- ### 📝 Commits (2) - [`0ecbe02`](https://github.com/adamhathcock/sharpcompress/commit/0ecbe02edeae7db9ecedb9fb3d45e70509511dad) Initial plan - [`dd242cb`](https://github.com/adamhathcock/sharpcompress/commit/dd242cb1c5c5eedaf289774e916ee144625b6f7d) Fix ExtractToDirectory to properly truncate existing files ### 📊 Changes **2 files changed** (+23 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `src/SharpCompress/Archives/IArchiveExtensions.cs` (+1 -1) 📝 `tests/SharpCompress.Test/Zip/ZipArchiveTests.cs` (+22 -0) </details> ### 📄 Description `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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-29 22:20:35 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#1439