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.
dotnet add package RecursiveAI.OneArchiveRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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 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.
Only this entry is copied.The full program
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;
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:
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
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.
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.