Deterministic tar file #448

Closed
opened 2026-01-29 22:12:11 +00:00 by claunia · 2 comments
Owner

Originally created by @hamarb123 on GitHub (Apr 7, 2021).

Hi, I'm trying to make it so that the tar file is the same every time given the same Dictionary<string, byte[]> files.
Here's my current code, but I'm getting a different output each time.

using MemoryStream ms = new();
var archive = TarArchive.Create();
var x = archive.PauseEntryRebuilding();
foreach (var item in files.OrderBy((x) => x.Key.GetHashCode())) //any ordering will do as long as it's not different
{
    MemoryStream file_ms = new(item.Value);
    archive.AddEntry(item.Key, file_ms, item.Value.LongLength, DateTime.UnixEpoch);
}
x.Dispose();
archive.SaveTo(ms, new(SharpCompress.Common.CompressionType.None));
File.WriteAllBytes("/path/to/test.tar", ms.ToArray());

I cannot work out how to make sure it's the same each time as when I open the tar file in something like notepad++, a different file is at the top every time.
(I know that I should be disposing those streams after they're not in use, but I wanted to get it to work first)
Could you please help me, thanks!

Originally created by @hamarb123 on GitHub (Apr 7, 2021). Hi, I'm trying to make it so that the tar file is the same every time given the same `Dictionary<string, byte[]> files`. Here's my current code, but I'm getting a different output each time. ```cs using MemoryStream ms = new(); var archive = TarArchive.Create(); var x = archive.PauseEntryRebuilding(); foreach (var item in files.OrderBy((x) => x.Key.GetHashCode())) //any ordering will do as long as it's not different { MemoryStream file_ms = new(item.Value); archive.AddEntry(item.Key, file_ms, item.Value.LongLength, DateTime.UnixEpoch); } x.Dispose(); archive.SaveTo(ms, new(SharpCompress.Common.CompressionType.None)); File.WriteAllBytes("/path/to/test.tar", ms.ToArray()); ``` I cannot work out how to make sure it's the same each time as when I open the tar file in something like notepad++, a different file is at the top every time. (I know that I should be disposing those streams after they're not in use, but I wanted to get it to work first) Could you please help me, thanks!
Author
Owner

@smuellener commented on GitHub (Dec 7, 2021):

Did you find a solution for this?

@smuellener commented on GitHub (Dec 7, 2021): Did you find a solution for this?
Author
Owner

@hamarb123 commented on GitHub (Dec 7, 2021):

My solution is here: 3a14fb517f/Automation/Compressor/Program.cs. I think the main issue was my sorting code. Note: that code is copyright, but I'm happy for you to copy the relevant parts to solve this.

@hamarb123 commented on GitHub (Dec 7, 2021): My solution is here: https://github.com/hamarb123/StoreMetadata/blob/3a14fb517f651f76922333948d7d69a9d5aa3d7e/Automation/Compressor/Program.cs. I think the main issue was my sorting code. Note: that code is copyright, but I'm happy for you to copy the relevant parts to solve this.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#448