---
title: "Read tar.gz from a non-seekable stream in C#"
url: https://recursiveai.org/onearchive/cookbook/stream-tar-entries-without-seeking/
description: "Read tar.gz from a non-seekable HTTP response stream in C#. Walk entries once with OneArchive and copy just the selected file without saving the archive."
version: "1.2.0"
---

# Read tar.gz from a non-seekable stream in C#

**OneArchive** is a free, open-source archive and compression library for .NET from Recursive AI: zip, tar, 7z, RAR and more in RecursiveAI.OneArchive, with compression codecs in RecursiveAI.OneCompress. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneArchive) · [GitHub](https://github.com/recursive-ai-oss/onearchive-csharp) · [Docs](https://recursiveai.org/onearchive/docs/) · [Download example (.zip)](https://recursiveai.org/onearchive/cookbook/stream-tar-entries-without-seeking/stream-tar-entries-without-seeking.zip)

Read the tar.gz response in order and copy only `notes.txt`. There’s no need to save the archive or rewind the HTTP stream.

```shell
dotnet add package RecursiveAI.OneArchive
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneArchive;

using var http = new HttpClient();
// args[0] is the URL of a tar.gz file.
using var input =
    await http.GetStreamAsync(args[0]);
using var archive = Archive.Open(input);
using var reader = archive.OpenReader();
while (reader.GetNextEntry() is { } entry)
{
    if (entry.Name != "notes.txt" ||
        entry.Kind != ArchiveEntryKind.File)
        continue;

    using var data = reader.OpenEntryStream();
    using var output = Console.OpenStandardOutput();
    data.CopyTo(output);
    break;
}
```

[The example](https://recursiveai.org/onearchive/cookbook/stream-tar-entries-without-seeking/stream-tar-entries-without-seeking.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

With no arguments, the downloaded program reads the bundled `bundle.tgz`. To read a tar.gz from a URL, use `dotnet run -- <url>`. Both copy `notes.txt` to standard output. In the sample, `readme.txt` says “Example bundle” and `notes.txt` says “Only this entry is copied.” Both end with a newline.

```text
Only this entry is copied.
```

## The full program

```csharp
using RecursiveAI.OneArchive;

using var http = new HttpClient();
using Stream input = args.Length > 0
    ? await http.GetStreamAsync(args[0])
    : File.OpenRead("bundle.tgz");
using var archive = Archive.Open(input);
using var reader = archive.OpenReader();
while (reader.GetNextEntry() is { } entry)
{
    if (entry.Name != "notes.txt" || entry.Kind != ArchiveEntryKind.File)
        continue;

    using var content = reader.OpenEntryStream();
    using var output = Console.OpenStandardOutput();
    content.CopyTo(output);
    return 0;
}

Console.Error.WriteLine("notes.txt not found");
return 1;
```

```text
Only this entry is copied.
```

If the archive ends without a regular file named `notes.txt`, the program prints `notes.txt not found` to standard error and exits with code 1.

## Variations

**Copy to a chosen file instead of standard output.** Inside the full program’s loop, replace the `output` declaration with:

```csharp
using var output = File.Create("chosen.txt");
```

`chosen.txt` contains `Only this entry is copied.` followed by a newline. The destination name is chosen by the caller, not taken from the archive.

## Related recipes

- [Extract a tar.gz file in C#](https://recursiveai.org/onearchive/cookbook/extract-tar-gz/)
- [Read concatenated tar archives in C#](https://recursiveai.org/onearchive/cookbook/read-concatenated-tar-archives/)

## The short answer

Pass the HTTP body stream to `Archive.Open` and call `OpenReader` once. Advance with `GetNextEntry`. When the exact name matches and the entry is a regular file, open the current entry with `OpenEntryStream` and copy it to your destination. `HttpClient.GetStreamAsync` gives you the body as a stream [without reading and buffering it first](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.getstreamasync).

## Can you read a tar.gz without extracting the whole archive?

Yes. Gzip decompresses a stream; tar organizes that stream as entry headers followed by entry contents. OneArchive handles both layers when you pass the original tar.gz response body. The loop advances over earlier entries without writing them anywhere, copies the first regular file with the requested name, then stops. It still has to read and decompress the data before that file to get to it. Stopping after one match also means the program doesn’t inspect later entries or check the rest of the archive for damage.

## Why can’t a non-seekable stream use random access?

A non-seekable stream can read forward, but it can’t return to an earlier byte. On these sources, `OpenReader()` can only be called once, and `Entries` is for seekable sources. Keep each entry stream inside the loop and finish with it before advancing. If you need two files, match both names in the same pass instead of opening a second reader. If you need to revisit entries in an order different from the tar order, make a fresh request or use a seekable copy. See [OneArchive’s sequential reader documentation](https://recursiveai.org/onearchive/docs/archive/#happy-path).
