Home > OS >  Compare file with archived file
Compare file with archived file

Time:06-13

I want to check if file and its archive version is the same. I created something like this:

public static class FileUtils
{
    public static bool SameAsArchive(this FileInfo file, string archivedFile)
    {
        using (var ms = new MemoryStream())
        {
            GZip.Decompress(File.OpenRead(archivedFile), ms, true);
            return File.ReadAllBytes(file.FullName).SequenceEqual(ms.ToArray());
        }
    }
}

Is there any faster way of checking that instead of reading all bytes?

CodePudding user response:

First check length, if they differ return false.

Then compare a chunk at a time e.g. 32Kb. Return false on first different chunk. Allocate 2 byte arrays for the chunks and reuse these arrays. So you implementation only has 2 chunks in memory at a time.

Use GZipStream to decompress a chunck at a time

CodePudding user response:

Yes, you can compare checksums of these files. It involves keeping somewhere (I guess IMemoryCache will be nice place for it) these checksums.

  •  Tags:  
  • c#
  • Related