private static ManifestEntry CreateEntryForFile(string parentDirectory, string filePath) { string hash; long fileSize; using (FileStream fileStream = File.OpenRead(filePath)) { // Calculate the hash of the file hash = MD5Handler.GetStreamHash(fileStream); // Get the disk size of the file fileSize = fileStream.Length; } // Get the relative path of the file string relativeFilePath = filePath.Substring(parentDirectory.Length); // Write the entry to the manifest ManifestEntry newEntry = new ManifestEntry { RelativePath = relativeFilePath, Hash = hash, Size = fileSize }; return(newEntry); }
/// <summary> /// The asynchronous implementation of the GenerateManifest function. /// </summary> /// <param name="targetPath">The root path of the directory the manifest should represent.</param> /// <param name="manifestType">The type of manifest that should be generated.</param> private void GenerateManifest_Implementation(string targetPath, EManifestType manifestType) { string parentDirectory = Directory.GetParent(targetPath).ToString(); string manifestPath = $@"{parentDirectory}{Path.DirectorySeparatorChar}{manifestType}Manifest.txt"; string manifestChecksumPath = $@"{parentDirectory}{Path.DirectorySeparatorChar}{manifestType}Manifest.checksum"; List <string> manifestFilePaths = new List <string>(Directory .EnumerateFiles(targetPath, "*", SearchOption.AllDirectories) .Where(s => !IsPathABlacklistedFile(s))); using (TextWriter tw = new StreamWriter(File.Create(manifestPath))) { int completedFiles = 0; foreach (string filePath in manifestFilePaths) { ManifestEntry newEntry = CreateEntryForFile(targetPath, filePath); tw.WriteLine(newEntry); tw.Flush(); completedFiles++; GenerationProgressArgs.TotalFiles = manifestFilePaths.Count; GenerationProgressArgs.CompletedFiles = completedFiles; GenerationProgressArgs.Filepath = newEntry.RelativePath; GenerationProgressArgs.Hash = newEntry.Hash; GenerationProgressArgs.Filesize = newEntry.Size; OnManifestGenerationProgressChanged(); } } // Create a checksum file for the manifest. using (Stream manifestStream = File.OpenRead(manifestPath)) { string manifestHash = MD5Handler.GetStreamHash(manifestStream); using (FileStream checksumStream = File.Create(manifestChecksumPath)) { using (TextWriter tw = new StreamWriter(checksumStream)) { tw.WriteLine(manifestHash); tw.Close(); } } } OnManifestGenerationFinished(); }
private ManifestEntry CreateEntryForFile(string parentDirectory, string filePath) { string hash; long fileSize; using (var fileStream = File.OpenRead(filePath)) { hash = MD5Handler.GetStreamHash(fileStream); fileSize = fileStream.Length; } var relativeFilePath = filePath.Substring(parentDirectory.Length).TrimStart(Path.DirectorySeparatorChar); var newEntry = new ManifestEntry { RelativePath = relativeFilePath, Hash = hash, Size = fileSize }; return(newEntry); }
/// <summary> /// The asynchronous implementation of the GenerateManifest function. /// </summary> private void GenerateManifest_Implementation() { string parentDirectory = Directory.GetParent(TargetPath).ToString(); string manifestPath = String.Format(@"{0}{1}{2}Manifest.txt", parentDirectory, Path.DirectorySeparatorChar, ManifestType); string manifestChecksumPath = String.Format(@"{0}{1}{2}Manifest.checksum", parentDirectory, Path.DirectorySeparatorChar, ManifestType); List <string> Files = new List <string>(Directory .EnumerateFiles(TargetPath, "*", SearchOption.AllDirectories)); using (TextWriter tw = new StreamWriter(File.Create(manifestPath))) { int completedFiles = 0; foreach (string file in Files) { // Calculate the MD5 hash of the file string hash; using (FileStream fileStream = File.OpenRead(file)) { hash = MD5Handler.GetStreamHash(fileStream); } // Get the file size on disk FileInfo Info = new FileInfo(file); long fileSize = Info.Length; // Get the relative path of the file string relativeFilePath = file.Substring(TargetPath.Length); // Write the entry to the manifest ManifestEntry Entry = new ManifestEntry(); Entry.RelativePath = relativeFilePath; Entry.Hash = hash; Entry.Size = fileSize; tw.WriteLine(Entry); completedFiles++; ProgressArgs.Filepath = relativeFilePath; ProgressArgs.TotalFiles = Files.Count; ProgressArgs.CompletedFiles = completedFiles; ProgressArgs.MD5 = hash; ProgressArgs.Filesize = fileSize; OnManifestGenerationProgressChanged(); } } // Create a checksum file for the manifest. using (Stream manifestStream = File.OpenRead(manifestPath)) { string manifestHash = MD5Handler.GetStreamHash(manifestStream); using (FileStream checksumStream = File.Create(manifestChecksumPath)) { using (TextWriter tw = new StreamWriter(checksumStream)) { tw.WriteLine(manifestHash); tw.Close(); } } } OnManifestGenerationFinished(); }
/// <summary> /// The asynchronous implementation of the GenerateManifest function. /// </summary> /// <param name="targetPath">The root path of the directory the manifest should represent.</param> /// <param name="manifestType">The type of manifest that should be generated.</param> private void GenerateManifest_Implementation(string targetPath, EManifestType manifestType) { string parentDirectory = Directory.GetParent(targetPath).ToString(); string manifestPath = $@"{parentDirectory}{Path.DirectorySeparatorChar}{manifestType}Manifest.txt"; string manifestChecksumPath = $@"{parentDirectory}{Path.DirectorySeparatorChar}{manifestType}Manifest.checksum"; List <string> manifestFilePaths = new List <string>(Directory .EnumerateFiles(targetPath, "*", SearchOption.AllDirectories) .Where(s => !s.EndsWith(".install") && !s.EndsWith(".update"))); using (TextWriter tw = new StreamWriter(File.Create(manifestPath))) { int completedFiles = 0; foreach (string filePath in manifestFilePaths) { // Calculate the MD5 hash of the file string hash; using (FileStream fileStream = File.OpenRead(filePath)) { hash = MD5Handler.GetStreamHash(fileStream); } // Get the file size on disk FileInfo fileInfo = new FileInfo(filePath); long fileSize = fileInfo.Length; // Get the relative path of the file string relativeFilePath = filePath.Substring(targetPath.Length); // Write the entry to the manifest ManifestEntry newEntry = new ManifestEntry { RelativePath = relativeFilePath, Hash = hash, Size = fileSize }; tw.WriteLine(newEntry); completedFiles++; GenerationProgressArgs.Filepath = relativeFilePath; GenerationProgressArgs.TotalFiles = manifestFilePaths.Count; GenerationProgressArgs.CompletedFiles = completedFiles; GenerationProgressArgs.Hash = hash; GenerationProgressArgs.Filesize = fileSize; OnManifestGenerationProgressChanged(); } } // Create a checksum file for the manifest. using (Stream manifestStream = File.OpenRead(manifestPath)) { string manifestHash = MD5Handler.GetStreamHash(manifestStream); using (FileStream checksumStream = File.Create(manifestChecksumPath)) { using (TextWriter tw = new StreamWriter(checksumStream)) { tw.WriteLine(manifestHash); tw.Close(); } } } OnManifestGenerationFinished(); }