private void Manifest(EnvelopeType ovfEnv, string pathToOvf, string ovfFileName) { var manifestFiles = new List <string> { ovfFileName }; var files = ovfEnv?.References?.File; if (files != null) { manifestFiles.AddRange(files.Select(f => f.href).ToList()); } var fileDigests = new List <FileDigest>(); foreach (var mf in manifestFiles) { CheckForCancellation(); var mfPath = Path.Combine(pathToOvf, mf); if (!File.Exists(mfPath)) { continue; } Description = string.Format(Messages.CREATING_MANIFEST_CHECKSUM, mf); log.Info($"Calculating checksum for file {mf}"); using (FileStream stream = new FileStream(mfPath, FileMode.Open, FileAccess.Read)) { var hash = StreamUtilities.ComputeHash(stream, out var hashAlgorithm); fileDigests.Add(new FileDigest(Path.GetFileName(mf), hash, hashAlgorithm)); } } CheckForCancellation(); Description = Messages.CREATING_MANIFEST; string manifest = Path.Combine(pathToOvf, $"{Path.GetFileNameWithoutExtension(ovfFileName)}{Package.MANIFEST_EXT}"); using (var stream = new FileStream(manifest, FileMode.Create, FileAccess.Write)) using (var sw = new StreamWriter(stream)) { foreach (var fileDigest in fileDigests) { sw.WriteLine(fileDigest.ToManifestLine()); } sw.Flush(); } log.Info($"Created manifest for package {ovfFileName}"); }
internal static TaskSequence ComputeMD5(this Stream stream, Action <string> setResult) { int readCount; MD5 md5 = MD5.Create(); do { byte[] buffer = new byte[BufferSize]; var readTask = stream.ReadAsync(buffer, 0, buffer.Length); yield return(readTask); readCount = readTask.Result; StreamUtilities.ComputeHash(buffer, 0, readCount, md5); }while (readCount != 0); setResult(StreamUtilities.GetHashValue(md5)); }