IReaderExtensions.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #if !NO_FILE
  2. using System.IO;
  3. using SharpCompress.Common;
  4. #endif
  5. namespace SharpCompress.Readers
  6. {
  7. public static class IReaderExtensions
  8. {
  9. #if !NO_FILE
  10. public static void WriteEntryTo(this IReader reader, string filePath)
  11. {
  12. using (Stream stream = File.Open(filePath, FileMode.Create, FileAccess.Write))
  13. {
  14. reader.WriteEntryTo(stream);
  15. }
  16. }
  17. public static void WriteEntryTo(this IReader reader, FileInfo filePath)
  18. {
  19. using (Stream stream = filePath.Open(FileMode.Create))
  20. {
  21. reader.WriteEntryTo(stream);
  22. }
  23. }
  24. /// <summary>
  25. /// Extract all remaining unread entries to specific directory, retaining filename
  26. /// </summary>
  27. public static void WriteAllToDirectory(this IReader reader, string destinationDirectory,
  28. ExtractionOptions options = null)
  29. {
  30. while (reader.MoveToNextEntry())
  31. {
  32. reader.WriteEntryToDirectory(destinationDirectory, options);
  33. }
  34. }
  35. /// <summary>
  36. /// Extract to specific directory, retaining filename
  37. /// </summary>
  38. public static void WriteEntryToDirectory(this IReader reader, string destinationDirectory,
  39. ExtractionOptions options = null)
  40. {
  41. ExtractionMethods.WriteEntryToDirectory(reader.Entry, destinationDirectory, options,
  42. reader.WriteEntryToFile);
  43. }
  44. /// <summary>
  45. /// Extract to specific file
  46. /// </summary>
  47. public static void WriteEntryToFile(this IReader reader, string destinationFileName,
  48. ExtractionOptions options = null)
  49. {
  50. ExtractionMethods.WriteEntryToFile(reader.Entry, destinationFileName, options,
  51. (x, fm) =>
  52. {
  53. using (FileStream fs = File.Open(destinationFileName, fm))
  54. {
  55. reader.WriteEntryTo(fs);
  56. }
  57. });
  58. }
  59. #endif
  60. }
  61. }