ExtractFileCallbackArgs.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #if UNMANAGED
  2. namespace SevenZip
  3. {
  4. using System;
  5. using System.IO;
  6. /// <summary>
  7. /// The arguments passed to <see cref="ExtractFileCallback"/>.
  8. /// </summary>
  9. /// <remarks>
  10. /// For each file, <see cref="ExtractFileCallback"/> is first called with <see cref="Reason"/>
  11. /// set to <see cref="ExtractFileCallbackReason.Start"/>. If the callback chooses to extract the
  12. /// file data by setting <see cref="ExtractToFile"/> or <see cref="ExtractToStream"/>, the callback
  13. /// will be called a second time with <see cref="Reason"/> set to
  14. /// <see cref="ExtractFileCallbackReason.Done"/> or <see cref="ExtractFileCallbackReason.Failure"/>
  15. /// to allow for any cleanup task like closing the stream.
  16. /// </remarks>
  17. public class ExtractFileCallbackArgs : EventArgs
  18. {
  19. private readonly ArchiveFileInfo _archiveFileInfo;
  20. private Stream _extractToStream;
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="ExtractFileCallbackArgs"/> class.
  23. /// </summary>
  24. /// <param name="archiveFileInfo">The information about file in the archive.</param>
  25. public ExtractFileCallbackArgs(ArchiveFileInfo archiveFileInfo)
  26. {
  27. Reason = ExtractFileCallbackReason.Start;
  28. _archiveFileInfo = archiveFileInfo;
  29. }
  30. /// <summary>
  31. /// Information about file in the archive.
  32. /// </summary>
  33. /// <value>Information about file in the archive.</value>
  34. public ArchiveFileInfo ArchiveFileInfo => _archiveFileInfo;
  35. /// <summary>
  36. /// The reason for calling <see cref="ExtractFileCallback"/>.
  37. /// </summary>
  38. /// <remarks>
  39. /// If neither <see cref="ExtractToFile"/> nor <see cref="ExtractToStream"/> is set,
  40. /// <see cref="ExtractFileCallback"/> will not be called after <see cref="ExtractFileCallbackReason.Start"/>.
  41. /// </remarks>
  42. /// <value>The reason.</value>
  43. public ExtractFileCallbackReason Reason { get; internal set; }
  44. /// <summary>
  45. /// The exception that occurred during extraction.
  46. /// </summary>
  47. /// <value>The _Exception.</value>
  48. /// <remarks>
  49. /// If the callback is called with <see cref="Reason"/> set to <see cref="ExtractFileCallbackReason.Failure"/>,
  50. /// this member contains the _Exception that occurred.
  51. /// The default behavior is to rethrow the _Exception after return of the callback.
  52. /// However the callback can set <see cref="Exception"/> to <c>null</c> to swallow the _Exception
  53. /// and continue extraction with the next file.
  54. /// </remarks>
  55. public Exception Exception { get; set; }
  56. /// <summary>
  57. /// Gets or sets a value indicating whether to cancel the extraction.
  58. /// </summary>
  59. /// <value><c>true</c> to cancel the extraction; <c>false</c> to continue. The default is <c>false</c>.</value>
  60. public bool CancelExtraction { get; set; }
  61. /// <summary>
  62. /// Gets or sets whether and where to extract the file.
  63. /// </summary>
  64. /// <value>The path where to extract the file to.</value>
  65. /// <remarks>
  66. /// If <see cref="ExtractToStream"/> is set, this mmember will be ignored.
  67. /// </remarks>
  68. public string ExtractToFile { get; set; }
  69. /// <summary>
  70. /// Gets or sets whether and where to extract the file.
  71. /// </summary>
  72. /// <value>The the extracted data is written to.</value>
  73. /// <remarks>
  74. /// If both this member and <see cref="ExtractToFile"/> are <c>null</c> (the defualt), the file
  75. /// will not be extracted and the callback will be be executed a second time with the <see cref="Reason"/>
  76. /// set to <see cref="ExtractFileCallbackReason.Done"/> or <see cref="ExtractFileCallbackReason.Failure"/>.
  77. /// </remarks>
  78. public Stream ExtractToStream
  79. {
  80. get => _extractToStream;
  81. set
  82. {
  83. if (_extractToStream != null && !_extractToStream.CanWrite)
  84. {
  85. throw new ExtractionFailedException("The specified stream is not writable!");
  86. }
  87. _extractToStream = value;
  88. }
  89. }
  90. /// <summary>
  91. /// Gets or sets any data that will be preserved between the <see cref="ExtractFileCallbackReason.Start"/> callback call
  92. /// and the <see cref="ExtractFileCallbackReason.Done"/> or <see cref="ExtractFileCallbackReason.Failure"/> calls.
  93. /// </summary>
  94. /// <value>The data.</value>
  95. public object ObjectData { get; set; }
  96. }
  97. }
  98. #endif