You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Utils.cs 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.IO;
  3. using System.IO.Compression;
  4. using System.Collections.Generic;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace Tensorflow.Hub
  10. {
  11. public static class Utils
  12. {
  13. public static async Task DownloadAsync<TDataSet>(this IModelLoader<TDataSet> modelLoader, string url, string saveTo)
  14. where TDataSet : IDataSet
  15. {
  16. var dir = Path.GetDirectoryName(saveTo);
  17. var fileName = Path.GetFileName(saveTo);
  18. await modelLoader.DownloadAsync(url, dir, fileName);
  19. }
  20. public static async Task DownloadAsync<TDataSet>(this IModelLoader<TDataSet> modelLoader, string url, string dirSaveTo, string fileName, bool showProgressInConsole = false)
  21. where TDataSet : IDataSet
  22. {
  23. if (!Path.IsPathRooted(dirSaveTo))
  24. dirSaveTo = Path.Combine(AppContext.BaseDirectory, dirSaveTo);
  25. var fileSaveTo = Path.Combine(dirSaveTo, fileName);
  26. if (showProgressInConsole)
  27. {
  28. Console.WriteLine($"Downloading {fileName}");
  29. }
  30. if (File.Exists(fileSaveTo))
  31. {
  32. if (showProgressInConsole)
  33. {
  34. Console.WriteLine($"The file {fileName} already exists");
  35. }
  36. return;
  37. }
  38. Directory.CreateDirectory(dirSaveTo);
  39. using (var wc = new WebClient())
  40. {
  41. await wc.DownloadFileTaskAsync(url, fileSaveTo).ConfigureAwait(false);
  42. }
  43. }
  44. public static async Task UnzipAsync<TDataSet>(this IModelLoader<TDataSet> modelLoader, string zipFile, string saveTo, bool showProgressInConsole = false)
  45. where TDataSet : IDataSet
  46. {
  47. if (!Path.IsPathRooted(saveTo))
  48. saveTo = Path.Combine(AppContext.BaseDirectory, saveTo);
  49. Directory.CreateDirectory(saveTo);
  50. if (!Path.IsPathRooted(zipFile))
  51. zipFile = Path.Combine(AppContext.BaseDirectory, zipFile);
  52. var destFileName = Path.GetFileNameWithoutExtension(zipFile);
  53. var destFilePath = Path.Combine(saveTo, destFileName);
  54. if (showProgressInConsole)
  55. Console.WriteLine($"Unzippinng {Path.GetFileName(zipFile)}");
  56. if (File.Exists(destFilePath))
  57. {
  58. if (showProgressInConsole)
  59. Console.WriteLine($"The file {destFileName} already exists");
  60. }
  61. using (GZipStream unzipStream = new GZipStream(File.OpenRead(zipFile), CompressionMode.Decompress))
  62. {
  63. using (var destStream = File.Create(destFilePath))
  64. {
  65. await unzipStream.CopyToAsync(destStream).ConfigureAwait(false);
  66. await destStream.FlushAsync().ConfigureAwait(false);
  67. destStream.Close();
  68. }
  69. unzipStream.Close();
  70. }
  71. }
  72. public static async Task ShowProgressInConsole(this Task task, bool enable)
  73. {
  74. if (!enable)
  75. {
  76. await task;
  77. return;
  78. }
  79. var cts = new CancellationTokenSource();
  80. var showProgressTask = ShowProgressInConsole(cts);
  81. try
  82. {
  83. await task;
  84. }
  85. finally
  86. {
  87. cts.Cancel();
  88. }
  89. await showProgressTask;
  90. Console.WriteLine("Done.");
  91. }
  92. private static async Task ShowProgressInConsole(CancellationTokenSource cts)
  93. {
  94. var cols = 0;
  95. await Task.Delay(1000);
  96. while (!cts.IsCancellationRequested)
  97. {
  98. await Task.Delay(1000);
  99. Console.Write(".");
  100. cols++;
  101. if (cols % 50 == 0)
  102. {
  103. Console.WriteLine();
  104. }
  105. }
  106. if (cols > 0)
  107. Console.WriteLine();
  108. }
  109. }
  110. }