Advertisement
kyrathasoft

zipall.cs

May 27th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.IO.Compression;
  4.  
  5. namespace ConsoleApplication {
  6.  
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. string targetDir = string.Empty;
  12.  
  13. if(args.Length == 1){
  14. targetDir = args[0];
  15. }
  16.  
  17. if(IsValidPath(targetDir, false)){
  18. string fileOut = new DirectoryInfo(targetDir).Name + ".zip";
  19.  
  20. try{
  21. string _fileout = fileOut;
  22. ZipFile.CreateFromDirectory(targetDir, fileOut);
  23. Console.WriteLine("\n Zip archive was successfully created.");
  24. }catch(Exception exWhileZipping){
  25. Console.Write("\n {0}", exWhileZipping.Message);
  26. }
  27. }else{
  28. Console.Write("\n Invalid path: {0}", targetDir);
  29. }
  30. }
  31.  
  32. public static bool IsValidPath(string path, bool allowRelativePaths = false)
  33. {
  34. bool isValid = true;
  35.  
  36. try
  37. {
  38. string fullPath = Path.GetFullPath(path);
  39.  
  40. if (allowRelativePaths)
  41. {
  42. isValid = Path.IsPathRooted(path);
  43. }
  44. else
  45. {
  46. string root = Path.GetPathRoot(path);
  47. isValid = string.IsNullOrEmpty(root.Trim(new char[] { '\\', '/' })) == false;
  48. }
  49. }
  50. catch(Exception ex)
  51. {
  52. isValid = false;
  53. Console.WriteLine("\n " + ex.Message);
  54. }
  55.  
  56. return isValid;
  57. }
  58.  
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement