public async Task <byte[]> ExportToZip() { using (var memoryStream = new MemoryStream()) { using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) { var files = (await LocalStorageRepository.GetKeys()).OrderBy(f => f).ToArray(); using (var hash = new MD5CompositeHash()) { foreach (var file in files) { var entry = archive.CreateEntry(file); using (var entryStream = entry.Open()) { using (var streamWriter = new StreamWriter(entryStream)) { var content = await LocalStorageRepository.GetItem(file); streamWriter.Write(content); hash.Add(file); hash.Add(content); } } } var hashEntry = archive.CreateEntry(SignatureName); using (var entryStream = hashEntry.Open()) { using (var streamWriter = new StreamWriter(entryStream)) { streamWriter.Write(hash.Hash.ToString()); } } } } return(memoryStream.ToArray()); } }
public static bool ValidateZip(byte[] zip) { if (zip == null) { throw new ArgumentNullException(nameof(zip)); } var signature = String.Empty; using (var hash = new MD5CompositeHash()) { using (var memoryStream = new MemoryStream(zip)) { using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Read, true)) { foreach (var entry in archive.Entries.OrderBy(n => n.Name)) { using (var entryStream = entry.Open()) { using (var streamWriter = new StreamReader(entryStream)) { var content = streamWriter.ReadToEnd(); if (entry.Name == SignatureName) { signature = content; } else { hash.Add(entry.Name); hash.Add(content); } } } } } } return(signature == hash.Hash.ToString()); } }