UpdateInstaller.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.IO.Compression;
  5. namespace PixiEditor.UpdateModule
  6. {
  7. public class UpdateInstaller
  8. {
  9. public const string TargetDirectoryName = "UpdateFiles";
  10. private float progress = 0;
  11. public UpdateInstaller(string archiveFileName, string targetDirectory)
  12. {
  13. ArchiveFileName = archiveFileName;
  14. TargetDirectory = targetDirectory;
  15. }
  16. public event EventHandler<UpdateProgressChangedEventArgs> ProgressChanged;
  17. public static string UpdateFilesPath { get; set; } = Path.Join(UpdateDownloader.DownloadLocation, TargetDirectoryName);
  18. public float Progress
  19. {
  20. get => progress;
  21. set
  22. {
  23. progress = value;
  24. ProgressChanged?.Invoke(this, new UpdateProgressChangedEventArgs(value));
  25. }
  26. }
  27. public string ArchiveFileName { get; set; }
  28. public string TargetDirectory { get; set; }
  29. public void Install()
  30. {
  31. var processes = Process.GetProcessesByName("PixiEditor");
  32. if (processes.Length > 0)
  33. {
  34. processes[0].WaitForExit();
  35. }
  36. ZipFile.ExtractToDirectory(ArchiveFileName, UpdateFilesPath, true);
  37. Progress = 25; // 25% for unzip
  38. string dirWithFiles = Directory.GetDirectories(UpdateFilesPath)[0];
  39. string[] files = Directory.GetFiles(dirWithFiles);
  40. CopyFilesToDestination(files);
  41. DeleteArchive();
  42. Progress = 100;
  43. }
  44. private void DeleteArchive()
  45. {
  46. File.Delete(ArchiveFileName);
  47. Directory.Delete(UpdateFilesPath, true);
  48. }
  49. private void CopyFilesToDestination(string[] files)
  50. {
  51. float fileCopiedVal = 74f / files.Length; // 74% is reserved for copying
  52. string destinationDir = TargetDirectory;
  53. foreach (string file in files)
  54. {
  55. string targetFileName = Path.GetFileName(file);
  56. File.Copy(file, Path.Join(destinationDir, targetFileName), true);
  57. Progress += fileCopiedVal;
  58. }
  59. }
  60. }
  61. }