ArchiveExtraction.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Windows;
  5. using System.Windows.Threading;
  6. using static PicView.lib.FileFunctions;
  7. using static PicView.lib.Variables;
  8. namespace PicView.lib
  9. {
  10. internal static class ArchiveExtraction
  11. {
  12. /// <summary>
  13. /// Attemps to extract folder
  14. /// </summary>
  15. /// <param name="path">The path to the archived file</param>
  16. /// <returns></returns>
  17. internal static bool Extract(string path)
  18. {
  19. if (!string.IsNullOrWhiteSpace(TempZipPath))
  20. DeleteTempFiles();
  21. var Winrar = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\WinRAR\\WinRAR.exe";
  22. if (!File.Exists(Winrar))
  23. Winrar = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + "\\WinRAR\\WinRAR.exe";
  24. if (File.Exists(Winrar))
  25. {
  26. Extract(path, Winrar, true);
  27. return true;
  28. }
  29. var sevenZip = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\7-Zip\\7z.exe";
  30. if (!File.Exists(sevenZip))
  31. sevenZip = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + "\\7-Zip\\7z.exe";
  32. if (File.Exists(sevenZip))
  33. {
  34. Extract(path, sevenZip, false);
  35. return true;
  36. }
  37. return false;
  38. }
  39. /// <summary>
  40. /// Attemps to extract folder
  41. /// </summary>
  42. /// <param name="path">The path to the archived file</param>
  43. /// <param name="exe">Full path of the executeable</param>
  44. /// <param name="winrar">If WinRar or 7-Zip</param>
  45. private static void Extract(string path, string exe, bool winrar)
  46. {
  47. TempZipPath = Path.GetTempPath() + Path.GetRandomFileName();
  48. Directory.CreateDirectory(TempZipPath);
  49. var arguments = winrar ?
  50. // Add WinRAR specifics
  51. "x -o- \"" + path + "\" "
  52. :
  53. // Add 7-Zip specifics
  54. "x \"" + path + "\" -o";
  55. arguments += TempZipPath + SupportedFiles + " -r -aou";
  56. var x = Process.Start(new ProcessStartInfo
  57. {
  58. FileName = exe,
  59. Arguments = arguments,
  60. #if DEBUG
  61. WindowStyle = ProcessWindowStyle.Normal
  62. #else
  63. WindowStyle = ProcessWindowStyle.Hidden
  64. #endif
  65. });
  66. if (x == null) return;
  67. x.WaitForExit(750);
  68. }
  69. }
  70. }