IWriterExtensions.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #if !NO_FILE
  2. using System;
  3. #endif
  4. using System.IO;
  5. namespace SharpCompress.Writers
  6. {
  7. public static class IWriterExtensions
  8. {
  9. public static void Write(this IWriter writer, string entryPath, Stream source)
  10. {
  11. writer.Write(entryPath, source, null);
  12. }
  13. #if !NO_FILE
  14. public static void Write(this IWriter writer, string entryPath, FileInfo source)
  15. {
  16. if (!source.Exists)
  17. {
  18. throw new ArgumentException("Source does not exist: " + source.FullName);
  19. }
  20. using (var stream = source.OpenRead())
  21. {
  22. writer.Write(entryPath, stream, source.LastWriteTime);
  23. }
  24. }
  25. public static void Write(this IWriter writer, string entryPath, string source)
  26. {
  27. writer.Write(entryPath, new FileInfo(source));
  28. }
  29. public static void WriteAll(this IWriter writer, string directory, string searchPattern = "*",
  30. SearchOption option = SearchOption.TopDirectoryOnly)
  31. {
  32. if (!Directory.Exists(directory))
  33. {
  34. throw new ArgumentException("Directory does not exist: " + directory);
  35. }
  36. #if NET35
  37. foreach (var file in Directory.GetDirectories(directory, searchPattern, option))
  38. #else
  39. foreach (var file in Directory.EnumerateFiles(directory, searchPattern, option))
  40. #endif
  41. {
  42. writer.Write(file.Substring(directory.Length), file);
  43. }
  44. }
  45. #endif
  46. }
  47. }