Skip to content

[API Proposal]: Add APIs to System.IO.Compression that can open compressed tar archives like tar.gz #70517

Description

@carlossanlop

The APIs we added in System.Formats.Tar are meant for handling the tar format only. No compression handling was added there.

Currently, to handle compressed tar.gz files with those APIs, we can use these two methods:

We can extract the whole archive in one call:

using FileStream compressedStream = File.OpenRead("/home/dotnet/SourceDirectory/compressed.tar.gz");
using GZipStream decompressor = new(compressedStream, CompressionMode.Decompress);
TarFile.ExtractToDirectory(source: decompressor, destinationDirectoryName: "/home/dotnet/DestinationDirectory/", overwriteFiles: false);

Or we can iterate through each individual entry and decide what to do with each:

using FileStream compressedStream = File.OpenRead(@"D:\SourceDirectory\compressed.tar.gz");
using GZipStream decompressor = new(compressedStream, CompressionMode.Decompress);
using TarReader reader = new(decompressor, leaveOpen: true);
TarEntry? entry;

while ((entry = GetNextEntry(copyData: true)) != null)
{
    Console.WriteLine($"Entry type: {entry.EntryType}, entry name: {entry.Name}");
}

If we want to add APIs that can handle archives compressed with gzip (or any compression algorithm), we should add them to System.IO.Compression. I am opening this issue to initiate the discussion.

Note: We have an open issue that is trying to standardize the way we use all our compression algorithms: Standardize pattern for exposing advanced configuration for compression streams. It's possible this proposal depends on solving that one first.

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jun 9, 2022
  2. removed
    untriagedNew issue has not been triaged by the area owner
    on Jun 23, 2022
  3. added this to the 8.0.0 milestone on Jun 23, 2022
  4. iSazonov commented on Aug 18, 2022

    @iSazonov
    Contributor

    Example of code to extract an archive:

            TarEntry? entry = null;
    
            using (var sourceStream = File.Open(Source, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                using var reader = new TarReader(sourceStream);
                try
                {
                    entry = reader.GetNextEntry();
                }
                catch
                {
                    // It is not tar archive. Go to next format detection.
                }
            }
    
            if (entry is not null)
            {
                TarFile.ExtractToDirectory(Source, ToDirectory, Otherwrite);
                return;
            }
    
            using (var sourceStream = File.Open(Source, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using var decompressorStream = new GZipStream(sourceStream, CompressionMode.Decompress);
                using var reader = new TarReader(decompressorStream);
                try
                {
                    entry = reader.GetNextEntry();
                }
                catch
                {
                    // It is not tar archive. Go to next format detection.
                }
            }
    
            if (entry is not null)
            {
                using var sourceStream = File.Open(Source, FileMode.Open, FileAccess.Read, FileShare.Read);
                using var decompressorStream = new GZipStream(sourceStream, CompressionMode.Decompress);
                TarFile.ExtractToDirectory(decompressorStream, ToDirectory, Otherwrite);
    
                return;
            }
    
            ExtractZipToDirectory(this, Source, ToDirectory, Encoding, Otherwrite);

    Notice, we have to put Zip detection on last place - after .gz

  5. modified the milestones: 8.0.0, Future on Jul 5, 2023
  6. changed the title [-][API Proposal]: Add APIs to `System.IO.Compression` that can compressed `tar` archives like `tar.gz`[/-] [+][API Proposal]: Add APIs to `System.IO.Compression` that can open compressed `tar` archives like `tar.gz`[/+] on Oct 15, 2025
  7. added
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    on Oct 16, 2025
  8. added theissue type on Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions