Recursive AI

Read the gzip original filename and timestamp 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.

Use the download’s report.gz, which contains daily report\n, or change the path to your .gz file.

Terminal
dotnet add package RecursiveAI.OneCompress

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using System.Buffers;
using RecursiveAI.OneCompress.Deflate;
 
using var file = File.OpenRead("report.gz");
var decoder = new DeflateDecoder(
    DeflateFormat.GZip);
var input = new byte[4096];
OperationStatus status;
do
{
    int count = file.Read(input);
    if (count == 0)
        throw new EndOfStreamException();
    status = decoder.Decompress(
        input.AsSpan(0, count),
        Span<byte>.Empty, out _, out _);
} while (status == OperationStatus.NeedMoreData);
 
var header = decoder.CurrentGZipHeader;
Console.WriteLine(header.HasName
    ? header.GetName() : "(no name)");
uint time = header.ModificationTime;
var utc = DateTimeOffset.FromUnixTimeSeconds(time);
Console.WriteLine(time == 0 ? "(no time)" : $"{utc:O}");

Unzip the example, then dotnet run.

Prints the sample’s original name and modification time in UTC:

Text
report.txt
2023-11-14T22:13:20.0000000+00:00

The built-in GZipStream in .NET 10 doesn’t expose the original filename or modification time (MTIME), so use a decoder that does.

The code reads the first member’s header without producing any decompressed bytes. The input buffer holds compressed bytes in chunks, so it doesn’t need to fit the payload. If the decoder needs more input and the file ends, the example throws EndOfStreamException; malformed data encountered by the decoder throws CompressionFormatException.

Variations

Write a gzip with a name and timestamp

C#
using RecursiveAI.OneCompress.Deflate;
 
var options = new DeflateOptions
{
    GZipFileName = "report.txt",
    GZipModificationTime = 1700000000u,
};
var gzip = GZip.Compress("daily report\n"u8, options);
File.WriteAllBytes("report.gz", gzip);

This writes report.gz; run the snippet at the top on it to print report.txt and 2023-11-14T22:13:20.0000000+00:00.

The short answer

Use a DeflateDecoder with DeflateFormat.GZip. Once the decoder has consumed the header, read CurrentGZipHeader.GetName() and CurrentGZipHeader.ModificationTime. An absent name returns null. A nonzero timestamp is seconds since the Unix epoch; convert it with DateTimeOffset.FromUnixTimeSeconds. Treat zero as “not available,” not as a timestamp to display.

How do you get the original filename from a .gz file?

Read FNAME, the optional original name in the gzip member header. It’s separate from the name of the .gz file: renaming report.gz doesn’t change the name stored inside it. OneCompress decodes the field as ISO 8859-1. HasName tells you whether the field is present, which is different from checking whether the decoded string is empty. If the file is a .tar.gz, the gzip name is for the compressed tar stream, not for the files inside the tar archive.

Each gzip member has its own header. The snippet reads the first one, even when several members are concatenated in one file. With an empty output span, DestinationTooSmall means the decoder has reached output it can’t write. The header has already been consumed at that point. Done covers a complete empty member. This reads metadata; it doesn’t check the payload’s trailer for a nonempty member.

How do you get the modification time from a gzip file?

Read MTIME from the gzip member, not the filesystem timestamp of the .gz file. MTIME can be unset, and it describes the modification time recorded by the producer, not when you downloaded the archive. FromUnixTimeSeconds gives a UTC value; the O format in the example shows the +00:00 offset. Convert to local time only if that’s what you want to display. If you generate the gzip yourself, use DeflateOptions.GZipModificationTime and GZipFileName to set those values explicitly.

All onearchive recipes