Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 28, 2025

Add support for empty directory entries in archives

Summary

Successfully implemented support for adding empty directory entries to archives, addressing the feature request in issue #831.

Completed Tasks

  • Add WriteDirectory methods to IWriter interface for adding empty directory entries
  • Implement WriteDirectory in TarWriter (using EntryType.Directory)
  • Implement WriteDirectory in ZipWriter (using trailing '/' with zero size)
  • Handle GZipWriter (throw NotSupportedException as GZip doesn't support directories)
  • Add AddDirectoryEntry methods to IWritableArchive interface
  • Implement in writable archive implementations (Tar, Zip, GZip)
  • Add extension methods for convenience
  • Create comprehensive tests for both writers and writable archives
  • Verify all tests pass (24 new tests + all existing tests passing)
  • Run code formatter (CSharpier)
  • Request and address code review feedback
  • Run security scan (CodeQL) - No vulnerabilities found
  • Address formatting feedback

Implementation Details

Public API Additions:

  1. IWriter.WriteDirectory(string directoryName, DateTime? modificationTime) - synchronous
  2. IWriter.WriteDirectoryAsync(string directoryName, DateTime? modificationTime, CancellationToken) - asynchronous
  3. IWritableArchive.AddDirectoryEntry(string key, DateTime? modified) - add directory to archive

Format-Specific Behavior:

  • Tar: Uses EntryType.Directory with size 0, directories end with '/'
  • Zip: Uses trailing '/' with zero size and ZipCompressionMethod.None
  • GZip: Throws NotSupportedException (format limitation)

Backward Compatibility:
All existing functionality preserved. Changes are additive only.

Testing

  • 24 new tests covering directory creation, normalization, and edge cases
  • All existing tests pass (236+ tests total)
  • No security vulnerabilities detected by CodeQL
  • Code formatted per CSharpier standards

Usage Example

// Using Writers
using var writer = new TarWriter(stream, options);
writer.WriteDirectory("config", DateTime.Now);
writer.Write("config/app.json", fileStream, DateTime.Now);

// Using Writable Archives
using var archive = ZipArchive.Create();
archive.AddDirectoryEntry("logs", DateTime.Now);
archive.AddEntry("logs/app.log", logStream, true, logStream.Length, DateTime.Now);
archive.SaveTo(outputPath, CompressionType.Deflate);
Original prompt

This section details on the original issue you should resolve

<issue_title>Cannot Add Empty Directories to Archives</issue_title>
<issue_description>Description
Currently, SharpCompress lacks support for adding empty directories to archives. When using IWriter or IWritableArchive interfaces to add entries, there is no straightforward way to include empty directories, as the library appears to ignore directory entries that lack files. This limits use cases where the exact directory structure, including empty folders, is essential, such as preserving the file structure during compression or in deployment packages.

Motivation
Including empty directories is important in scenarios where the directory structure needs to be faithfully recreated after decompression. This is especially useful for:

  • Deployment packages where empty folders are placeholders for future files.
  • Compression of directories with empty folders that need to be preserved, ensuring consistency in archive content.
  • Development and build systems that rely on specific folder structures, including empty directories, to function correctly.

Proposed Solution
Consider adding a way to support empty directory entries in IWriter or IWritableArchive, allowing a Stream.Null or a specific method for empty directories, similar to:

archive.AddDirectoryEntry(relativePath);

or ensuring that calling AddEntry with Stream.Null and size: 0 explicitly adds an empty directory entry.

Alternative Workaround
Currently, the only workaround is to create a temporary file in each empty directory or use a marker file to ensure that the directory is not skipped, which is not ideal and requires post-extraction cleanup.

Thank you for considering this feature enhancement.</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.

Copilot AI and others added 5 commits October 28, 2025 10:22
Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for adding empty directories to archives Add support for empty directory entries in archives Oct 28, 2025
Copilot AI requested a review from adamhathcock October 28, 2025 10:34
@adamhathcock
Copy link
Owner

@copilot format this code

@adamhathcock adamhathcock marked this pull request as ready for review October 28, 2025 11:28
Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 28, 2025

@copilot format this code

Code has been formatted using CSharpier. Fixed formatting in GZipArchiveDirectoryTests.cs (commit b4352fe).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot Add Empty Directories to Archives

2 participants