diff --git a/src/TensorFlowHub/TensorFlowHub.csproj b/src/TensorFlowHub/TensorFlowHub.csproj index b79578a4..ffd4e11f 100644 --- a/src/TensorFlowHub/TensorFlowHub.csproj +++ b/src/TensorFlowHub/TensorFlowHub.csproj @@ -9,5 +9,6 @@ + diff --git a/src/TensorFlowHub/Utils.cs b/src/TensorFlowHub/Utils.cs index a9b5a2ca..56251035 100644 --- a/src/TensorFlowHub/Utils.cs +++ b/src/TensorFlowHub/Utils.cs @@ -3,8 +3,12 @@ using System.IO; using System.Collections.Generic; using System.Net; using System.Text; +using System.Threading; using System.Threading.Tasks; using NumSharp; +using SharpCompress; +using SharpCompress.Common; +using SharpCompress.Readers; namespace Tensorflow.Hub { @@ -32,5 +36,72 @@ namespace Tensorflow.Hub await wc.DownloadFileTaskAsync(url, Path.Combine(dirSaveTo, fileName)); } } + + public static void Unzip(this IModelLoader modelLoader, string zipFile, string saveTo) + where TDataSet : IDataSet + { + if (!Path.IsPathRooted(saveTo)) + saveTo = Path.Combine(AppContext.BaseDirectory, saveTo); + + if (!Directory.Exists(saveTo)) + Directory.CreateDirectory(saveTo); + + using (var stream = File.OpenRead(zipFile)) + using (var reader = ReaderFactory.Open(stream)) + { + while (reader.MoveToNextEntry()) + { + if (!reader.Entry.IsDirectory) + { + reader.WriteEntryToDirectory(saveTo, new ExtractionOptions() + { + ExtractFullPath = true, + Overwrite = true + }); + } + } + } + } + + public static async Task UnzipAsync(this IModelLoader modelLoader, string zipFile, string saveTo) + where TDataSet : IDataSet + { + await Task.Run(() => modelLoader.Unzip(zipFile, saveTo)); + } + + public static async Task ShowProgressInConsole(this Task task) + { + var cts = new CancellationTokenSource(); + var showProgressTask = ShowProgressInConsole(cts); + + try + { + await task; + } + finally + { + cts.Cancel(); + } + } + + private static async Task ShowProgressInConsole(CancellationTokenSource cts) + { + var cols = 0; + + while (!cts.IsCancellationRequested) + { + await Task.Delay(1000); + Console.Write("."); + cols++; + + if (cols >= 50) + { + cols = 0; + Console.WriteLine(); + } + } + + Console.WriteLine(); + } } }