UpdateInstaller.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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;
  11. public UpdateInstaller(string archiveFileName)
  12. {
  13. ArchiveFileName = archiveFileName;
  14. }
  15. public event EventHandler<UpdateProgressChangedEventArgs> ProgressChanged;
  16. public float Progress
  17. {
  18. get => progress;
  19. set
  20. {
  21. progress = value;
  22. ProgressChanged?.Invoke(this, new UpdateProgressChangedEventArgs(value));
  23. }
  24. }
  25. public string ArchiveFileName { get; set; }
  26. public void Install()
  27. {
  28. var processes = Process.GetProcessesByName("PixiEditor");
  29. if (processes.Length > 0)
  30. {
  31. processes[0].WaitForExit();
  32. }
  33. ZipFile.ExtractToDirectory(ArchiveFileName, TargetDirectoryName, true);
  34. Progress = 25; // 25% for unzip
  35. var dirWithFiles = Directory.GetDirectories(TargetDirectoryName)[0];
  36. var files = Directory.GetFiles(dirWithFiles);
  37. CopyFilesToDestination(files);
  38. DeleteArchive();
  39. Progress = 100;
  40. }
  41. private void DeleteArchive()
  42. {
  43. File.Delete(ArchiveFileName);
  44. }
  45. private void CopyFilesToDestination(string[] files)
  46. {
  47. var fileCopiedVal = 74f / files.Length; // 74% is reserved for copying
  48. var destinationDir = Path.GetDirectoryName(ArchiveFileName);
  49. foreach (var file in files)
  50. {
  51. var targetFileName = Path.GetFileName(file);
  52. File.Copy(file, Path.Join(destinationDir, targetFileName), true);
  53. Progress += fileCopiedVal;
  54. }
  55. }
  56. }
  57. }