Сравните файлы BLOB-объектов с байтами в C # - PullRequest
0 голосов
/ 26 октября 2018

Ранее логика сравнивает файлы, которые хранятся в папке Windows. Пожалуйста, смотрите код ниже для сравнения 2 файлов на основе байтов

const int BYTES_TO_READ = 1024;

public static bool filesAreDifferent(string file1, string file2) {
    FileInfo fi1 = new FileInfo(file1);
    FileInfo fi2 = new FileInfo(file2);

    if (!fi1.Exists || !fi2.Exists) return true;
    if (fi1.Length != fi2.Length) return true;

    int iterations = (int)Math.Ceiling((double)fi1.Length / BYTES_TO_READ);

    using (FileStream fs1 = fi1.OpenRead())
    using (FileStream fs2 = fi2.OpenRead()) {
        byte[] one = new byte[BYTES_TO_READ];
        byte[] two = new byte[BYTES_TO_READ];

        for (int i = 0; i < iterations; i++) {
            fs1.Read(one, 0, BYTES_TO_READ);
            fs2.Read(two, 0, BYTES_TO_READ);
           if (!one.SequenceEqual(two)) return true;            
        }
    }

    return false;
}

Теперь я хочу объединить 2 файла, которые хранятся в контейнере BLOB-объектов. Ниже то, что я пробовал до сих пор (настройка старой логики),

public static CloudBlobContainer GetStorageAccount(bool IsCreateIfNotExists)
{
    var ff = ConfigurationManager.AppSettings["AzureWebJobsStorage"];
    string configvalue1 = ConfigurationManager.AppSettings["AzureWebJobsStorage"];
    string configvalue2 = ConfigurationManager.AppSettings["AzureWebJobsStorage"];

    CloudBlobContainer blob = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureWebJobsStorage"])
                       .CreateCloudBlobClient()
                       .GetContainerReference(ConfigurationManager.AppSettings["BlobCotainer"]);

    if(IsCreateIfNotExists)
       blob.CreateIfNotExistsAsync();

    return blob;

}

public static bool filesAreDifferentBlob(string file1, string file2)
{
    CloudBlockBlob fil1 = GetStorageAccount(true).GetBlockBlobReference(file1);
    CloudBlockBlob fil2 = GetStorageAccount(true).GetBlockBlobReference(file2);
    fil1.FetchAttributes();
    fil2.FetchAttributes();

    if (!fil1.Exists() || !fil2.Exists()) return true;
    if (fil1.Properties.Length != fil1.Properties.Length) return true;

    int iterations = (int)Math.Ceiling((double)fil1.Properties.Length / BYTES_TO_READ);
    using (StreamReader fsf1 = new StreamReader(fil1.OpenRead()))
    using (StreamReader fsf2 = new StreamReader(fil2.OpenRead()))
    {
       byte[] one = new byte[BYTES_TO_READ];
       byte[] two = new byte[BYTES_TO_READ];

       for (int i = 0; i < iterations; i++)
       {
         fsf1.Read(one, 0, BYTES_TO_READ);
         fsf2.Read(two, 0, BYTES_TO_READ);
         if (!one.SequenceEqual(two)) return true;
        }
    }
    return false;
}

Но я получаю сообщение об ошибке "Не удалось преобразовать из байта [] в символ []". Есть ли способ сравнить 2 файла из блоба?

1 Ответ

0 голосов
/ 30 октября 2018

Есть ли способ сравнить 2 файла из BLOB-объекта?

Как сказал Томас, мы можем сравнивать хэш ContentMD5 ваших BLOB-объектов вместо сравнения всехбайт.Мы можем легко получить ContentMD5 хэшей из ваших больших двоичных объектов.

fil1.FetchAttributes();
fil2.FetchAttributes();
if (!fil1.Exists() || !fil2.Exists()) return true;
if (fil1.Properties.ContentMD5!= fil2.Properties.ContentMD5) return true;
return false;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...