SevenZipExtractorAsynchronous.cs 11 KB

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