Techblox Mod Manager / Launcher
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ZipArchiveExtensions.cs 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace GCMM
  9. {
  10. public static class ZipArchiveExtensions
  11. { //https://stackoverflow.com/a/14795752/2703239
  12. public static void ExtractToDirectory(this ZipArchive archive, string destinationDirectoryName, bool overwrite)
  13. {
  14. if (!overwrite)
  15. {
  16. archive.ExtractToDirectory(destinationDirectoryName);
  17. return;
  18. }
  19. DirectoryInfo di = Directory.CreateDirectory(destinationDirectoryName);
  20. string destinationDirectoryFullPath = di.FullName;
  21. foreach (ZipArchiveEntry file in archive.Entries)
  22. {
  23. string completeFileName = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, file.FullName));
  24. if (!completeFileName.StartsWith(destinationDirectoryFullPath, StringComparison.OrdinalIgnoreCase))
  25. {
  26. throw new IOException("Trying to extract file outside of destination directory. See this link for more info: https://snyk.io/research/zip-slip-vulnerability");
  27. }
  28. if (file.Name == "")
  29. {// Assuming Empty for Directory
  30. Directory.CreateDirectory(Path.GetDirectoryName(completeFileName));
  31. continue;
  32. }
  33. file.ExtractToFile(completeFileName, true);
  34. }
  35. }
  36. }
  37. }