Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore timestamps when creating Zip File?

Tags:

c#

I have multiple servers making same task of generating some file/folder data, and need to pack it before callback, then diff it, but stumble across problem in which two identical data folders creating two different archives. Im using this function:

ZipFile.CreateFromDirectory(...) 

And this is applied to same data in different folders:

C:\a\data\* 
C:\b\data\*

Which creates two different zip archives, and only difference is their header and footer. But if I create two archives in from single folder - it is identical. I suppose some timestamps (creation data, modification data, etc) is written to archive.

How one can specify that archives created from same data should be identical? (Ignoring global meta information)

like image 876
eocron Avatar asked Jan 31 '26 05:01

eocron


1 Answers

Well, problem were solver pretty easly by creating archive from folder manually, and setting last modification time of entries:

    public static void ZipDirectory(string input, string output, CompressionLevel level)
    {

        input = Path.GetFullPath(input);
        using (var fs = File.OpenWrite(output))
        using (var za = new ZipArchive(fs, ZipArchiveMode.Create))
        {
            foreach (var filePath in Directory.GetFiles(input, "*", SearchOption.AllDirectories).OrderBy(x => x))
            {
                var name = filePath.Replace(input, "").TrimStart('\\', '/');
                var e = za.CreateEntry(name, level);
                using (var zes = e.Open())
                {
                    using (var fes = File.OpenRead(filePath))
                    {
                        fes.CopyTo(zes);
                    }
                }
            }
        }

        using (var za = ZipFile.Open(output, ZipArchiveMode.Update))
        {
            foreach (var e in za.Entries)
            {
                e.LastWriteTime = new DateTimeOffset(1980, 1, 1, 0, 0, 0, 0, TimeSpan.Zero);
            }
        }
    }

This way my zip archives ignores any unrelevant meta information about files.

like image 148
eocron Avatar answered Feb 01 '26 20:02

eocron