SevenZipCompressorAsynchronous.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. namespace SevenZip
  2. {
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Threading.Tasks;
  6. partial class SevenZipCompressor
  7. {
  8. #region Delegates
  9. private delegate void CompressFiles1Delegate(string archiveName, string[] fileFullNames);
  10. private delegate void CompressFiles2Delegate(Stream archiveStream, string[] fileFullNames);
  11. private delegate void CompressFiles3Delegate(string archiveName, int commonRootLength, string[] fileFullNames);
  12. private delegate void CompressFiles4Delegate(Stream archiveStream, int commonRootLength, string[] fileFullNames);
  13. private delegate void CompressFilesEncrypted1Delegate(string archiveName, string password, string[] fileFullNames);
  14. private delegate void CompressFilesEncrypted2Delegate(Stream archiveStream, string password, string[] fileFullNames);
  15. private delegate void CompressFilesEncrypted3Delegate(string archiveName, int commonRootLength, string password, string[] fileFullNames);
  16. private delegate void CompressFilesEncrypted4Delegate(Stream archiveStream, int commonRootLength, string password, string[] fileFullNames);
  17. private delegate void CompressDirectoryDelegate(string directory, string archiveName, string password, string searchPattern, bool recursion);
  18. private delegate void CompressDirectory2Delegate(string directory, Stream archiveStream, string password, string searchPattern, bool recursion);
  19. private delegate void CompressStreamDelegate(Stream inStream, Stream outStream, string password);
  20. private delegate void ModifyArchiveDelegate(string archiveName, IDictionary<int, string> newFileNames, string password);
  21. #endregion
  22. #region BeginCompressFiles overloads
  23. /// <summary>
  24. /// Packs files into the archive asynchronously.
  25. /// </summary>
  26. /// <param name="fileFullNames">Array of file names to pack.</param>
  27. /// <param name="archiveName">The archive file name.</param>
  28. public void BeginCompressFiles(string archiveName, params string[] fileFullNames)
  29. {
  30. SaveContext();
  31. Task.Run(() => new CompressFiles1Delegate(CompressFiles).Invoke(archiveName, fileFullNames))
  32. .ContinueWith(_ => ReleaseContext());
  33. }
  34. /// <summary>
  35. /// Packs files into the archive asynchronously.
  36. /// </summary>
  37. /// <param name="fileFullNames">Array of file names to pack.</param>
  38. /// <param name="archiveStream">The archive output stream.
  39. /// Use CompressFiles(string archiveName ... ) overloads for archiving to disk.</param>
  40. public void BeginCompressFiles(Stream archiveStream, params string[] fileFullNames)
  41. {
  42. SaveContext();
  43. Task.Run(() => new CompressFiles2Delegate(CompressFiles).Invoke(archiveStream, fileFullNames))
  44. .ContinueWith(_ => ReleaseContext());
  45. }
  46. /// <summary>
  47. /// Packs files into the archive asynchronously.
  48. /// </summary>
  49. /// <param name="fileFullNames">Array of file names to pack.</param>
  50. /// <param name="commonRootLength">The length of the common root of the file names.</param>
  51. /// <param name="archiveName">The archive file name.</param>
  52. public void BeginCompressFiles(string archiveName, int commonRootLength, params string[] fileFullNames)
  53. {
  54. SaveContext();
  55. Task.Run(() => new CompressFiles3Delegate(CompressFiles).Invoke(archiveName, commonRootLength, fileFullNames))
  56. .ContinueWith(_ => ReleaseContext());
  57. }
  58. /// <summary>
  59. /// Packs files into the archive asynchronously.
  60. /// </summary>
  61. /// <param name="fileFullNames">Array of file names to pack.</param>
  62. /// <param name="commonRootLength">The length of the common root of the file names.</param>
  63. /// <param name="archiveStream">The archive output stream.
  64. /// Use CompressFiles(string archiveName, ... ) overloads for archiving to disk.</param>
  65. public void BeginCompressFiles(Stream archiveStream, int commonRootLength, params string[] fileFullNames)
  66. {
  67. SaveContext();
  68. Task.Run(() => new CompressFiles4Delegate(CompressFiles).Invoke(archiveStream, commonRootLength, fileFullNames))
  69. .ContinueWith(_ => ReleaseContext());
  70. }
  71. /// <summary>
  72. /// Packs files into the archive asynchronously.
  73. /// </summary>
  74. /// <param name="fileFullNames">Array of file names to pack.</param>
  75. /// <param name="archiveName">The archive file name</param>
  76. /// <param name="password">The archive password.</param>
  77. public void BeginCompressFilesEncrypted(string archiveName, string password, params string[] fileFullNames )
  78. {
  79. SaveContext();
  80. Task.Run(() => new CompressFilesEncrypted1Delegate(CompressFilesEncrypted).Invoke(archiveName, password, fileFullNames))
  81. .ContinueWith(_ => ReleaseContext());
  82. }
  83. /// <summary>
  84. /// Packs files into the archive asynchronously.
  85. /// </summary>
  86. /// <param name="fileFullNames">Array of file names to pack.</param>
  87. /// <param name="archiveStream">The archive output stream.
  88. /// Use CompressFiles( ... string archiveName ... ) overloads for archiving to disk.</param>
  89. /// <param name="password">The archive password.</param>
  90. public void BeginCompressFilesEncrypted(Stream archiveStream, string password, params string[] fileFullNames)
  91. {
  92. SaveContext();
  93. Task.Run(() => new CompressFilesEncrypted2Delegate(CompressFilesEncrypted).Invoke(archiveStream, password, fileFullNames))
  94. .ContinueWith(_ => ReleaseContext());
  95. }
  96. /// <summary>
  97. /// Packs files into the archive asynchronously.
  98. /// </summary>
  99. /// <param name="fileFullNames">Array of file names to pack.</param>
  100. /// <param name="archiveName">The archive file name</param>
  101. /// <param name="password">The archive password.</param>
  102. /// <param name="commonRootLength">The length of the common root of the file names.</param>
  103. public void BeginCompressFilesEncrypted(string archiveName, int commonRootLength, string password, params string[] fileFullNames)
  104. {
  105. SaveContext();
  106. Task.Run(() => new CompressFilesEncrypted3Delegate(CompressFilesEncrypted).Invoke(archiveName, commonRootLength, password, fileFullNames))
  107. .ContinueWith(_ => ReleaseContext());
  108. }
  109. /// <summary>
  110. /// Packs files into the archive asynchronously.
  111. /// </summary>
  112. /// <param name="fileFullNames">Array of file names to pack.</param>
  113. /// <param name="archiveStream">The archive output stream.
  114. /// Use CompressFiles( ... string archiveName ... ) overloads for archiving to disk.</param>
  115. /// <param name="password">The archive password.</param>
  116. /// <param name="commonRootLength">The length of the common root of the file names.</param>
  117. public void BeginCompressFilesEncrypted(Stream archiveStream, int commonRootLength, string password, params string[] fileFullNames)
  118. {
  119. SaveContext();
  120. Task.Run(() => new CompressFilesEncrypted4Delegate(CompressFilesEncrypted).Invoke(archiveStream, commonRootLength, password, fileFullNames))
  121. .ContinueWith(_ => ReleaseContext());
  122. }
  123. #endregion
  124. #region CompressFilesAsync overloads
  125. /// <summary>
  126. /// Packs files into the archive asynchronously.
  127. /// </summary>
  128. /// <param name="fileFullNames">Array of file names to pack.</param>
  129. /// <param name="archiveName">The archive file name.</param>
  130. public async Task CompressFilesAsync(string archiveName, params string[] fileFullNames)
  131. {
  132. try
  133. {
  134. SaveContext();
  135. await Task.Run(() => new CompressFiles1Delegate(CompressFiles).Invoke(archiveName, fileFullNames));
  136. }
  137. finally
  138. {
  139. ReleaseContext();
  140. }
  141. }
  142. /// <summary>
  143. /// Packs files into the archive asynchronously.
  144. /// </summary>
  145. /// <param name="fileFullNames">Array of file names to pack.</param>
  146. /// <param name="archiveStream">The archive output stream.
  147. /// Use CompressFiles(string archiveName ... ) overloads for archiving to disk.</param>
  148. public async Task CompressFilesAsync(Stream archiveStream, params string[] fileFullNames)
  149. {
  150. try
  151. {
  152. SaveContext();
  153. await Task.Run(() => new CompressFiles2Delegate(CompressFiles).Invoke(archiveStream, fileFullNames));
  154. }
  155. finally
  156. {
  157. ReleaseContext();
  158. }
  159. }
  160. /// <summary>
  161. /// Packs files into the archive asynchronously.
  162. /// </summary>
  163. /// <param name="fileFullNames">Array of file names to pack.</param>
  164. /// <param name="commonRootLength">The length of the common root of the file names.</param>
  165. /// <param name="archiveName">The archive file name.</param>
  166. public async Task CompressFilesAsync(string archiveName, int commonRootLength, params string[] fileFullNames)
  167. {
  168. try
  169. {
  170. SaveContext();
  171. await Task.Run(() => new CompressFiles3Delegate(CompressFiles).Invoke(archiveName, commonRootLength, fileFullNames));
  172. }
  173. finally
  174. {
  175. ReleaseContext();
  176. }
  177. }
  178. /// <summary>
  179. /// Packs files into the archive asynchronously.
  180. /// </summary>
  181. /// <param name="fileFullNames">Array of file names to pack.</param>
  182. /// <param name="commonRootLength">The length of the common root of the file names.</param>
  183. /// <param name="archiveStream">The archive output stream.
  184. /// Use CompressFiles(string archiveName, ... ) overloads for archiving to disk.</param>
  185. public async Task CompressFilesAsync(Stream archiveStream, int commonRootLength, params string[] fileFullNames)
  186. {
  187. try
  188. {
  189. SaveContext();
  190. await Task.Run(() => new CompressFiles4Delegate(CompressFiles).Invoke(archiveStream, commonRootLength, fileFullNames));
  191. }
  192. finally
  193. {
  194. ReleaseContext();
  195. }
  196. }
  197. /// <summary>
  198. /// Packs files into the archive asynchronously.
  199. /// </summary>
  200. /// <param name="fileFullNames">Array of file names to pack.</param>
  201. /// <param name="archiveName">The archive file name</param>
  202. /// <param name="password">The archive password.</param>
  203. public async Task CompressFilesEncryptedAsync(string archiveName, string password, params string[] fileFullNames)
  204. {
  205. try
  206. {
  207. SaveContext();
  208. await Task.Run(() => new CompressFilesEncrypted1Delegate(CompressFilesEncrypted).Invoke(archiveName, password, fileFullNames));
  209. }
  210. finally
  211. {
  212. ReleaseContext();
  213. }
  214. }
  215. /// <summary>
  216. /// Packs files into the archive asynchronously.
  217. /// </summary>
  218. /// <param name="fileFullNames">Array of file names to pack.</param>
  219. /// <param name="archiveStream">The archive output stream.
  220. /// Use CompressFiles( ... string archiveName ... ) overloads for archiving to disk.</param>
  221. /// <param name="password">The archive password.</param>
  222. public async Task CompressFilesEncryptedAsync(Stream archiveStream, string password, params string[] fileFullNames)
  223. {
  224. try
  225. {
  226. SaveContext();
  227. await Task.Run(() => new CompressFilesEncrypted2Delegate(CompressFilesEncrypted).Invoke(archiveStream, password, fileFullNames));
  228. }
  229. finally
  230. {
  231. ReleaseContext();
  232. }
  233. }
  234. /// <summary>
  235. /// Packs files into the archive asynchronously.
  236. /// </summary>
  237. /// <param name="fileFullNames">Array of file names to pack.</param>
  238. /// <param name="archiveName">The archive file name</param>
  239. /// <param name="password">The archive password.</param>
  240. /// <param name="commonRootLength">The length of the common root of the file names.</param>
  241. public async Task CompressFilesEncryptedAsync(string archiveName, int commonRootLength, string password, params string[] fileFullNames)
  242. {
  243. try
  244. {
  245. SaveContext();
  246. await Task.Run(() => new CompressFilesEncrypted3Delegate(CompressFilesEncrypted).Invoke(archiveName, commonRootLength, password, fileFullNames));
  247. }
  248. finally
  249. {
  250. ReleaseContext();
  251. }
  252. }
  253. /// <summary>
  254. /// Packs files into the archive asynchronously.
  255. /// </summary>
  256. /// <param name="fileFullNames">Array of file names to pack.</param>
  257. /// <param name="archiveStream">The archive output stream.
  258. /// Use CompressFiles( ... string archiveName ... ) overloads for archiving to disk.</param>
  259. /// <param name="password">The archive password.</param>
  260. /// <param name="commonRootLength">The length of the common root of the file names.</param>
  261. public async Task CompressFilesEncryptedAsync(Stream archiveStream, int commonRootLength, string password, params string[] fileFullNames)
  262. {
  263. try
  264. {
  265. SaveContext();
  266. await Task.Run(() => new CompressFilesEncrypted4Delegate(CompressFilesEncrypted).Invoke(archiveStream, commonRootLength, password, fileFullNames));
  267. }
  268. finally
  269. {
  270. ReleaseContext();
  271. }
  272. }
  273. #endregion
  274. #region BeginCompressDirectory overloads
  275. /// <summary>
  276. /// Packs all files in the specified directory asynchronously.
  277. /// </summary>
  278. /// <param name="directory">The directory to compress.</param>
  279. /// <param name="archiveName">The archive file name.</param>
  280. /// <param name="password">The archive password.</param>
  281. /// <param name="searchPattern">Search string, such as "*.txt".</param>
  282. /// <param name="recursion">If true, files will be searched for recursively; otherwise, not.</param>
  283. public void BeginCompressDirectory(string directory, string archiveName, string password = "", string searchPattern = "*", bool recursion = true)
  284. {
  285. SaveContext();
  286. Task.Run(() => new CompressDirectoryDelegate(CompressDirectory).Invoke(directory, archiveName, password, searchPattern, recursion))
  287. .ContinueWith(_ => ReleaseContext());
  288. }
  289. /// <summary>
  290. /// Packs all files in the specified directory asynchronously.
  291. /// </summary>
  292. /// <param name="directory">The directory to compress.</param>
  293. /// <param name="archiveStream">The archive output stream.
  294. /// Use CompressDirectory( ... string archiveName ... ) overloads for archiving to disk.</param>
  295. /// <param name="password">The archive password.</param>
  296. /// <param name="searchPattern">Search string, such as "*.txt".</param>
  297. /// <param name="recursion">If true, files will be searched for recursively; otherwise, not.</param>
  298. public void BeginCompressDirectory(string directory, Stream archiveStream, string password , string searchPattern = "*", bool recursion = true)
  299. {
  300. SaveContext();
  301. Task.Run(() => new CompressDirectory2Delegate(CompressDirectory).Invoke(directory, archiveStream, password, searchPattern, recursion))
  302. .ContinueWith(_ => ReleaseContext());
  303. }
  304. #endregion
  305. #region CompressDirectoryAsync overloads
  306. /// <summary>
  307. /// Packs all files in the specified directory asynchronously.
  308. /// </summary>
  309. /// <param name="directory">The directory to compress.</param>
  310. /// <param name="archiveName">The archive file name.</param>
  311. /// <param name="password">The archive password.</param>
  312. /// <param name="searchPattern">Search string, such as "*.txt".</param>
  313. /// <param name="recursion">If true, files will be searched for recursively; otherwise, not.</param>
  314. public async Task CompressDirectoryAsync(string directory, string archiveName, string password = "", string searchPattern = "*", bool recursion = true)
  315. {
  316. try
  317. {
  318. SaveContext();
  319. await Task.Run(() => new CompressDirectoryDelegate(CompressDirectory).Invoke(directory, archiveName, password, searchPattern, recursion));
  320. }
  321. finally
  322. {
  323. ReleaseContext();
  324. }
  325. }
  326. /// <summary>
  327. /// Packs all files in the specified directory asynchronously.
  328. /// </summary>
  329. /// <param name="directory">The directory to compress.</param>
  330. /// <param name="archiveStream">The archive output stream.
  331. /// Use CompressDirectory( ... string archiveName ... ) overloads for archiving to disk.</param>
  332. /// <param name="password">The archive password.</param>
  333. /// <param name="searchPattern">Search string, such as "*.txt".</param>
  334. /// <param name="recursion">If true, files will be searched for recursively; otherwise, not.</param>
  335. public async Task CompressDirectoryAsync(string directory, Stream archiveStream, string password, string searchPattern = "*", bool recursion = true)
  336. {
  337. try
  338. {
  339. SaveContext();
  340. await Task.Run(() => new CompressDirectory2Delegate(CompressDirectory).Invoke(directory, archiveStream, password, searchPattern, recursion));
  341. }
  342. finally
  343. {
  344. ReleaseContext();
  345. }
  346. }
  347. #endregion
  348. #region BeginCompressStream overloads
  349. /// <summary>
  350. /// Compresses the specified stream.
  351. /// </summary>
  352. /// <param name="inStream">The source uncompressed stream.</param>
  353. /// <param name="outStream">The destination compressed stream.</param>
  354. /// <param name="password">The archive password.</param>
  355. /// <exception cref="System.ArgumentException">ArgumentException: at least one of the specified streams is invalid.</exception>
  356. public void BeginCompressStream(Stream inStream, Stream outStream, string password = "")
  357. {
  358. SaveContext();
  359. Task.Run(() => new CompressStreamDelegate(CompressStream).Invoke(inStream, outStream, password))
  360. .ContinueWith(_ => ReleaseContext());
  361. }
  362. #endregion
  363. #region CompressStreamAsync overloads
  364. /// <summary>
  365. /// Compresses the specified stream.
  366. /// </summary>
  367. /// <param name="inStream">The source uncompressed stream.</param>
  368. /// <param name="outStream">The destination compressed stream.</param>
  369. /// <param name="password">The archive password.</param>
  370. /// <exception cref="System.ArgumentException">ArgumentException: at least one of the specified streams is invalid.</exception>
  371. public async Task CompressStreamAsync(Stream inStream, Stream outStream, string password = "")
  372. {
  373. try
  374. {
  375. SaveContext();
  376. await Task.Run(() => new CompressStreamDelegate(CompressStream).Invoke(inStream, outStream, password));
  377. }
  378. finally
  379. {
  380. ReleaseContext();
  381. }
  382. }
  383. #endregion
  384. #region BeginModifyArchive overloads
  385. /// <summary>
  386. /// Modifies the existing archive asynchronously (renames files or deletes them).
  387. /// </summary>
  388. /// <param name="archiveName">The archive file name.</param>
  389. /// <param name="newFileNames">New file names. Null value to delete the corresponding index.</param>
  390. /// <param name="password">The archive password.</param>
  391. public void BeginModifyArchive(string archiveName, IDictionary<int, string> newFileNames, string password = "")
  392. {
  393. SaveContext();
  394. Task.Run(() => new ModifyArchiveDelegate(ModifyArchive).Invoke(archiveName, newFileNames, password))
  395. .ContinueWith(_ => ReleaseContext());
  396. }
  397. #endregion
  398. #region ModifyArchiveAsync overloads
  399. /// <summary>
  400. /// Modifies the existing archive asynchronously (renames files or deletes them).
  401. /// </summary>
  402. /// <param name="archiveName">The archive file name.</param>
  403. /// <param name="newFileNames">New file names. Null value to delete the corresponding index.</param>
  404. /// <param name="password">The archive password.</param>
  405. public async Task ModifyArchiveAsync(string archiveName, IDictionary<int, string> newFileNames, string password = "")
  406. {
  407. try
  408. {
  409. SaveContext();
  410. await Task.Run(() => new ModifyArchiveDelegate(ModifyArchive).Invoke(archiveName, newFileNames, password));
  411. }
  412. finally
  413. {
  414. ReleaseContext();
  415. }
  416. }
  417. #endregion
  418. }
  419. }