---
title: "Read the gzip original filename and timestamp in C#"
url: https://recursiveai.org/onearchive/cookbook/read-gzip-original-filename-and-timestamp/
description: "Read the original filename and modification time from a gzip header in C#. See how to handle a missing name or timestamp with OneCompress."
version: "1.2.0"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneCompress) · [GitHub](https://github.com/recursive-ai-oss/onearchive-csharp) · [Docs](https://recursiveai.org/onearchive/docs/) · [Download example (.zip)](https://recursiveai.org/onearchive/cookbook/read-gzip-original-filename-and-timestamp/read-gzip-original-filename-and-timestamp.zip)

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

```shell
dotnet add package RecursiveAI.OneCompress
```

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

```csharp
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](https://recursiveai.org/onearchive/cookbook/read-gzip-original-filename-and-timestamp/read-gzip-original-filename-and-timestamp.zip), 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](https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.gzipstream?view=net-10.0) 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

```csharp
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`.

## Related recipes

- [Read and write gzip data in C#](https://recursiveai.org/onearchive/cookbook/gzip-interop/)
- [Extract a tar.gz archive in C#](https://recursiveai.org/onearchive/cookbook/extract-tar-gz/)

<!-- article -->

## 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](https://www.rfc-editor.org/rfc/rfc1952#section-2.3.1). 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.
