Skip to content

Add Compression best practices guide#52968

Open
alinpahontu2912 wants to merge 8 commits intodotnet:mainfrom
alinpahontu2912:zip_tar_bestpractices
Open

Add Compression best practices guide#52968
alinpahontu2912 wants to merge 8 commits intodotnet:mainfrom
alinpahontu2912:zip_tar_bestpractices

Conversation

@alinpahontu2912
Copy link
Copy Markdown
Member

@alinpahontu2912 alinpahontu2912 commented Apr 10, 2026

Summary

Add a guide explaining how to best work with Zip and Tar archives in .NET.


Internal previews

📄 File 🔗 Preview link
docs/fundamentals/toc.yml docs/fundamentals/toc
docs/standard/io/zip-tar-best-practices.md Best practices for working with ZIP and TAR archives in .NET

Comment thread docs/standard/io/zip-tar-best-practices.md
Comment thread docs/standard/io/zip-tar-best-practices.md Outdated
Comment thread docs/standard/io/snippets/zip-tar-best-practices/csharp/Program.cs Outdated
Comment thread docs/standard/io/snippets/zip-tar-best-practices/csharp/Program.cs Outdated
Comment thread docs/standard/io/snippets/zip-tar-best-practices/csharp/Program.cs Outdated
Comment thread docs/standard/io/zip-tar-best-practices.md Outdated
Comment thread docs/standard/io/zip-tar-best-practices.md Outdated
Comment thread docs/standard/io/zip-tar-best-practices.md Outdated
Comment thread docs/standard/io/zip-tar-best-practices.md Outdated
Comment thread docs/standard/io/zip-tar-best-practices.md Outdated
Copy link
Copy Markdown
Member

@rzikm rzikm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's getting better, few additional comments.

Comment thread docs/standard/io/snippets/zip-tar-best-practices/csharp/Program.cs
Comment thread docs/standard/io/snippets/zip-tar-best-practices/csharp/Program.cs Outdated
Comment thread docs/standard/io/snippets/zip-tar-best-practices/csharp/Program.cs Outdated
Comment thread docs/standard/io/zip-tar-best-practices.md
Comment thread docs/standard/io/zip-tar-best-practices.md Outdated
Comment thread docs/standard/io/zip-tar-best-practices.md Outdated
Comment thread docs/standard/io/zip-tar-best-practices.md Outdated
Comment thread docs/standard/io/zip-tar-best-practices.md Outdated

## Data integrity

ZIP entries include a CRC-32 checksum that you can use to verify data hasn't been corrupted or tampered with.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should also mention TAR CRC in the first paragraph, now users might assume that this section is Zip-only and skip the rest of it.

@alinpahontu2912 alinpahontu2912 requested a review from rzikm April 16, 2026 13:13
@alinpahontu2912 alinpahontu2912 marked this pull request as ready for review April 23, 2026 08:26
@alinpahontu2912 alinpahontu2912 requested a review from adegeo as a code owner April 23, 2026 08:26
Copilot AI review requested due to automatic review settings April 23, 2026 08:26
@alinpahontu2912 alinpahontu2912 requested a review from a team as a code owner April 23, 2026 08:26
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new guidance article under File and stream I/O that explains how to work with ZIP and TAR archives in .NET, with a focus on API selection, safe extraction patterns, and operational considerations.

Changes:

  • Adds a new best-practices article for ZIP and TAR archives, including security guidance for untrusted input.
  • Adds a new C# snippet project and a consolidated Program.cs containing the referenced code regions.
  • Links the new article from docs/fundamentals/toc.yml.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
docs/standard/io/zip-tar-best-practices.md New best-practices guide covering API choice, trusted vs. untrusted extraction, memory/perf, platform differences, and encryption notes.
docs/standard/io/snippets/zip-tar-best-practices/csharp/Project.csproj New snippet project targeting net11.0 for compiling the article snippets.
docs/standard/io/snippets/zip-tar-best-practices/csharp/Program.cs Adds the C# snippet implementations referenced by the article.
docs/fundamentals/toc.yml Adds a TOC entry pointing to the new best-practices article.

Comment thread docs/standard/io/zip-tar-best-practices.md Outdated
Comment thread docs/standard/io/snippets/zip-tar-best-practices/csharp/Program.cs Outdated
Comment thread docs/standard/io/snippets/zip-tar-best-practices/csharp/Program.cs
@rzikm
Copy link
Copy Markdown
Member

rzikm commented Apr 24, 2026

cc also @GrabYourPitchforks and @blowdart for wording

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Comment thread docs/standard/io/snippets/zip-tar-best-practices/csharp/Program.cs Outdated
Copy link
Copy Markdown
Member

@MihaZupan MihaZupan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's great we're documenting this, thank you!

It'd be good if we were also able to provide better ways of getting these things right in the first place though.

Comment thread docs/standard/io/snippets/zip-tar-best-practices/csharp/Program.cs Outdated
Comment on lines +45 to +53
string fullDestDir = Path.GetFullPath(destinationDir);
if (!fullDestDir.EndsWith(Path.DirectorySeparatorChar))
fullDestDir += Path.DirectorySeparatorChar;

foreach (ZipArchiveEntry entry in archive.Entries)
{
string destPath = Path.GetFullPath(Path.Join(fullDestDir, entry.FullName));

if (!destPath.StartsWith(fullDestDir, StringComparison.Ordinal))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any plans to make this easier to do correctly in the future?
E.g. the missing trailing separator on the destination dir is trivial to miss.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have the trailing separator check for both zips and tars, but for the higher-level, convenience ExtractToDirectory methods. However, these are less secure and we recommend iterating through the archives and extracting entries one by one. The disadvantage is that some extra manual checks are necessary this way.

Copy link
Copy Markdown
Member

@svick svick May 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MihaZupan There is this proposal for Path.IsSubdirectory: dotnet/runtime#87581. But it doesn't seem to be very active. File system case sensitivity makes that more difficult as well.

Comment thread docs/standard/io/snippets/zip-tar-best-practices/csharp/Program.cs Outdated
Comment thread docs/standard/io/snippets/zip-tar-best-practices/csharp/Program.cs Outdated
// </VulnerablePattern>

// <SafeExtractZip>
void SafeExtractZip(string archivePath, string destinationDir,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the safe way to use our API is to copy-paste such a helper into your code, we should consider exposing better APIs

E.g. bool TryResolvePath(string destinationDirectory, out string path) on TarEntry/ZipArchiveEntry, and/or adding maxTotalSize/maxEntrySize/maxEntryCount as options to ExtractToDirectory.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've got a point, maybe we should consider creating per-entry safe extraction methods or add some options to let user decide how to interact with these apis

Comment thread docs/standard/io/zip-tar-best-practices.md
Comment thread docs/standard/io/zip-tar-best-practices.md Outdated
Comment thread docs/standard/io/zip-tar-best-practices.md
Comment thread docs/standard/io/zip-tar-best-practices.md Outdated
Comment thread docs/standard/io/snippets/zip-tar-best-practices/csharp/Program.cs Outdated
Comment thread docs/standard/io/zip-tar-best-practices.md Outdated
Comment thread docs/standard/io/zip-tar-best-practices.md Outdated
Comment thread docs/standard/io/zip-tar-best-practices.md Outdated
Comment thread docs/standard/io/zip-tar-best-practices.md Outdated
Comment thread docs/standard/io/zip-tar-best-practices.md Outdated
Comment thread docs/standard/io/zip-tar-best-practices.md Outdated
Comment thread docs/standard/io/zip-tar-best-practices.md Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants