Guest User

Untitled

a guest
Oct 22nd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. /// <summary>
  2. /// Archive file or folder from target path
  3. /// </summary>
  4. /// <param name="path"></param>
  5. /// <returns></returns>
  6. public static bool CreateZipFile(string path)
  7. {
  8.  
  9. string temp = string.Empty;
  10. bool success = false;
  11.  
  12. FileAttributes attributes = File.GetAttributes(path);
  13. if ((attributes & FileAttributes.Directory) != FileAttributes.Directory)
  14. {
  15. try
  16. {
  17. string pathToZip = Path.GetFileNameWithoutExtension(path);
  18. pathToZip = Path.GetDirectoryName(path) + "\" + pathToZip + ".zip";
  19.  
  20. using (FileStream fs = File.Create(pathToZip))
  21. {
  22. using (ZipArchive zip = new ZipArchive(fs, ZipArchiveMode.Create))
  23. {
  24. ZipArchiveEntry entry = zip.CreateEntryFromFile(path, Path.GetFileName(path));
  25. }
  26. }
  27. success = true;
  28. }
  29. catch (Exception error)
  30. {
  31. success = false;
  32. LogWrite("ErrorZip.log", error.Message);
  33. }
  34. }
  35. else
  36. {
  37. try
  38. {
  39. temp = path + ".zip";
  40. ZipFile.CreateFromDirectory(path, temp);
  41. success = true;
  42. }
  43. catch (Exception error)
  44. {
  45. success = false;
  46. LogWrite("ErrorZip.log", error.Message);
  47. }
  48. }
  49. return success;
  50. }
Add Comment
Please, Sign In to add comment