SevenZipExtractorAsynchronous.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. namespace SevenZip
  2. {
  3. using System;
  4. using System.IO;
  5. #if DOTNET20
  6. using System.Threading;
  7. #else
  8. using System.Windows.Threading;
  9. #endif
  10. partial class SevenZipExtractor
  11. {
  12. #region Asynchronous core methods
  13. /// <summary>
  14. /// Recreates the instance of the SevenZipExtractor class.
  15. /// Used in asynchronous methods.
  16. /// </summary>
  17. private void RecreateInstanceIfNeeded()
  18. {
  19. if (NeedsToBeRecreated)
  20. {
  21. NeedsToBeRecreated = false;
  22. Stream backupStream = null;
  23. string backupFileName = null;
  24. if (String.IsNullOrEmpty(_fileName))
  25. {
  26. backupStream = _inStream;
  27. }
  28. else
  29. {
  30. backupFileName = _fileName;
  31. }
  32. CommonDispose();
  33. if (backupStream == null)
  34. {
  35. Init(backupFileName);
  36. }
  37. else
  38. {
  39. Init(backupStream);
  40. }
  41. }
  42. }
  43. internal override void SaveContext(
  44. #if !DOTNET20
  45. DispatcherPriority eventPriority
  46. #if CS4
  47. = DispatcherPriority.Normal
  48. #endif
  49. #endif
  50. )
  51. {
  52. DisposedCheck();
  53. base.SaveContext(
  54. #if !DOTNET20
  55. eventPriority
  56. #endif
  57. );
  58. }
  59. #endregion
  60. #region Delegates
  61. /// <summary>
  62. /// The delegate to use in BeginExtractArchive.
  63. /// </summary>
  64. /// <param name="directory">The directory where the files are to be unpacked.</param>
  65. private delegate void ExtractArchiveDelegate(string directory);
  66. /// <summary>
  67. /// The delegate to use in BeginExtractFile (by file name).
  68. /// </summary>
  69. /// <param name="fileName">The file full name in the archive file table.</param>
  70. /// <param name="stream">The stream where the file is to be unpacked.</param>
  71. private delegate void ExtractFileByFileNameDelegate(string fileName, Stream stream);
  72. /// <summary>
  73. /// The delegate to use in BeginExtractFile (by index).
  74. /// </summary>
  75. /// <param name="index">Index in the archive file table.</param>
  76. /// <param name="stream">The stream where the file is to be unpacked.</param>
  77. private delegate void ExtractFileByIndexDelegate(int index, Stream stream);
  78. /// <summary>
  79. /// The delegate to use in BeginExtractFiles(string directory, params int[] indexes).
  80. /// </summary>
  81. /// <param name="indexes">indexes of the files in the archive file table.</param>
  82. /// <param name="directory">Directory where the files are to be unpacked.</param>
  83. private delegate void ExtractFiles1Delegate(string directory, int[] indexes);
  84. /// <summary>
  85. /// The delegate to use in BeginExtractFiles(string directory, params string[] fileNames).
  86. /// </summary>
  87. /// <param name="fileNames">Full file names in the archive file table.</param>
  88. /// <param name="directory">Directory where the files are to be unpacked.</param>
  89. private delegate void ExtractFiles2Delegate(string directory, string[] fileNames);
  90. /// <summary>
  91. /// The delegate to use in BeginExtractFiles(ExtractFileCallback extractFileCallback).
  92. /// </summary>
  93. /// <param name="extractFileCallback">The callback to call for each file in the archive.</param>
  94. private delegate void ExtractFiles3Delegate(ExtractFileCallback extractFileCallback);
  95. #endregion
  96. #if !DOTNET20
  97. /// <summary>
  98. /// Unpacks the whole archive asynchronously to the specified directory name at the specified priority.
  99. /// </summary>
  100. /// <param name="directory">The directory where the files are to be unpacked.</param>
  101. /// <param name="eventPriority">The priority of events, relative to the other pending operations in the System.Windows.Threading.Dispatcher event queue, the specified method is invoked.</param>
  102. #else
  103. /// <summary>
  104. /// Unpacks the whole archive asynchronously to the specified directory name at the specified priority.
  105. /// </summary>
  106. /// <param name="directory">The directory where the files are to be unpacked.</param>
  107. #endif
  108. public void BeginExtractArchive(string directory
  109. #if !DOTNET20
  110. , DispatcherPriority eventPriority
  111. #if CS4
  112. = DispatcherPriority.Normal
  113. #endif
  114. #endif
  115. )
  116. {
  117. SaveContext(
  118. #if !DOTNET20
  119. eventPriority
  120. #endif
  121. );
  122. (new ExtractArchiveDelegate(ExtractArchive)).BeginInvoke(directory, AsyncCallbackImplementation, this);
  123. }
  124. #if !DOTNET20
  125. /// <summary>
  126. /// Unpacks the file asynchronously by its name to the specified stream.
  127. /// </summary>
  128. /// <param name="fileName">The file full name in the archive file table.</param>
  129. /// <param name="stream">The stream where the file is to be unpacked.</param>
  130. /// <param name="eventPriority">The priority of events, relative to the other pending operations in the System.Windows.Threading.Dispatcher event queue, the specified method is invoked.</param>
  131. #else
  132. /// <summary>
  133. /// Unpacks the file asynchronously by its name to the specified stream.
  134. /// </summary>
  135. /// <param name="fileName">The file full name in the archive file table.</param>
  136. /// <param name="stream">The stream where the file is to be unpacked.</param>
  137. #endif
  138. public void BeginExtractFile(string fileName, Stream stream
  139. #if !DOTNET20
  140. , DispatcherPriority eventPriority
  141. #if CS4
  142. = DispatcherPriority.Normal
  143. #endif
  144. #endif
  145. )
  146. {
  147. SaveContext(
  148. #if !DOTNET20
  149. eventPriority
  150. #endif
  151. );
  152. (new ExtractFileByFileNameDelegate(ExtractFile)).BeginInvoke(fileName, stream, AsyncCallbackImplementation, this);
  153. }
  154. #if !DOTNET20
  155. /// <summary>
  156. /// Unpacks the file asynchronously by its index to the specified stream.
  157. /// </summary>
  158. /// <param name="index">Index in the archive file table.</param>
  159. /// <param name="stream">The stream where the file is to be unpacked.</param>
  160. /// <param name="eventPriority">The priority of events, relative to the other pending operations in the System.Windows.Threading.Dispatcher event queue, the specified method is invoked.</param>
  161. #else
  162. /// <summary>
  163. /// Unpacks the file asynchronously by its index to the specified stream.
  164. /// </summary>
  165. /// <param name="index">Index in the archive file table.</param>
  166. /// <param name="stream">The stream where the file is to be unpacked.</param>
  167. #endif
  168. public void BeginExtractFile(int index, Stream stream
  169. #if !DOTNET20
  170. , DispatcherPriority eventPriority
  171. #if CS4
  172. = DispatcherPriority.Normal
  173. #endif
  174. #endif
  175. )
  176. {
  177. SaveContext(
  178. #if !DOTNET20
  179. eventPriority
  180. #endif
  181. );
  182. (new ExtractFileByIndexDelegate(ExtractFile)).BeginInvoke(index, stream, AsyncCallbackImplementation, this);
  183. }
  184. #if !DOTNET20
  185. /// <summary>
  186. /// Unpacks files asynchronously by their indices to the specified directory.
  187. /// </summary>
  188. /// <param name="indexes">indexes of the files in the archive file table.</param>
  189. /// <param name="directory">Directory where the files are to be unpacked.</param>
  190. /// <param name="eventPriority">The priority of events, relative to the other pending operations in the System.Windows.Threading.Dispatcher event queue, the specified method is invoked.</param>
  191. #else
  192. /// <summary>
  193. /// Unpacks files asynchronously by their indices to the specified directory.
  194. /// </summary>
  195. /// <param name="indexes">indexes of the files in the archive file table.</param>
  196. /// <param name="directory">Directory where the files are to be unpacked.</param>
  197. #endif
  198. public void BeginExtractFiles(string directory
  199. #if !DOTNET20
  200. , DispatcherPriority eventPriority
  201. #if CS4
  202. = DispatcherPriority.Normal
  203. #endif
  204. #endif
  205. , params int[] indexes)
  206. {
  207. SaveContext(
  208. #if !DOTNET20
  209. eventPriority
  210. #endif
  211. );
  212. (new ExtractFiles1Delegate(ExtractFiles)).BeginInvoke(directory, indexes, AsyncCallbackImplementation, this);
  213. }
  214. #if !DOTNET20
  215. /// <summary>
  216. /// Unpacks files asynchronously by their full names to the specified directory.
  217. /// </summary>
  218. /// <param name="fileNames">Full file names in the archive file table.</param>
  219. /// <param name="directory">Directory where the files are to be unpacked.</param>
  220. /// <param name="eventPriority">The priority of events, relative to the other pending operations in the System.Windows.Threading.Dispatcher event queue, the specified method is invoked.</param>
  221. #else
  222. /// <summary>
  223. /// Unpacks files asynchronously by their full names to the specified directory.
  224. /// </summary>
  225. /// <param name="fileNames">Full file names in the archive file table.</param>
  226. /// <param name="directory">Directory where the files are to be unpacked.</param>
  227. #endif
  228. public void BeginExtractFiles(string directory
  229. #if !DOTNET20
  230. , DispatcherPriority eventPriority
  231. #if CS4
  232. = DispatcherPriority.Normal
  233. #endif
  234. #endif
  235. , params string[] fileNames)
  236. {
  237. SaveContext(
  238. #if !DOTNET20
  239. eventPriority
  240. #endif
  241. );
  242. (new ExtractFiles2Delegate(ExtractFiles)).BeginInvoke(directory, fileNames, AsyncCallbackImplementation, this);
  243. }
  244. #if !DOTNET20
  245. /// <summary>
  246. /// Extracts files from the archive asynchronously, giving a callback the choice what
  247. /// to do with each file. The order of the files is given by the archive.
  248. /// 7-Zip (and any other solid) archives are NOT supported.
  249. /// </summary>
  250. /// <param name="extractFileCallback">The callback to call for each file in the archive.</param>
  251. /// <param name="eventPriority">The priority of events, relative to the other pending operations in the System.Windows.Threading.Dispatcher event queue, the specified method is invoked.</param>
  252. #else
  253. /// <summary>
  254. /// Extracts files from the archive asynchronously, giving a callback the choice what
  255. /// to do with each file. The order of the files is given by the archive.
  256. /// 7-Zip (and any other solid) archives are NOT supported.
  257. /// </summary>
  258. /// <param name="extractFileCallback">The callback to call for each file in the archive.</param>
  259. #endif
  260. public void BeginExtractFiles(ExtractFileCallback extractFileCallback
  261. #if !DOTNET20
  262. , DispatcherPriority eventPriority
  263. #if CS4
  264. = DispatcherPriority.Normal
  265. #endif
  266. #endif
  267. )
  268. {
  269. SaveContext(
  270. #if !DOTNET20
  271. eventPriority
  272. #endif
  273. );
  274. (new ExtractFiles3Delegate(ExtractFiles)).BeginInvoke(extractFileCallback, AsyncCallbackImplementation, this);
  275. }
  276. }
  277. }