SevenZipHelper.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System.Diagnostics;
  2. using System.Threading;
  3. namespace Archives_Center
  4. {
  5. public class SevenZipHelper
  6. {
  7. public static void CreateEncryptedZip(string sourceDirectory, string destinationZipFile, string password)
  8. {
  9. string sevenZipPath = @"C:\Program Files\7-Zip\7z.exe";
  10. string arguments = $"a -tzip \"{destinationZipFile}\" \"{sourceDirectory}\\*\" -p{password} -mem=AES256";
  11. ProcessStartInfo startInfo = new ProcessStartInfo
  12. {
  13. FileName = sevenZipPath,
  14. Arguments = arguments,
  15. UseShellExecute = false,
  16. RedirectStandardOutput = true,
  17. RedirectStandardError = true,
  18. CreateNoWindow = true
  19. };
  20. using (Process process = Process.Start(startInfo))
  21. {
  22. process.WaitForExit();
  23. }
  24. }
  25. public static void ExtractEncryptedArchive(string archivePath, string outputPath, string password)
  26. {
  27. string zipPath = @"C:\Program Files\7-Zip\7z.exe";
  28. string arguments = $"x \"{archivePath}\" -o\"{outputPath}\" -p{password}";
  29. ProcessStartInfo psi = new ProcessStartInfo
  30. {
  31. FileName = zipPath,
  32. Arguments = arguments
  33. };
  34. using (Process process = new Process())
  35. {
  36. process.StartInfo = psi;
  37. process.Start();
  38. }
  39. Thread.Sleep(1000);
  40. }
  41. }
  42. }