Browse Source

added unzip and showprogress in console in the utils

tags/v0.10
Kerry Jiang 6 years ago
parent
commit
e8e02437ae
2 changed files with 72 additions and 0 deletions
  1. +1
    -0
      src/TensorFlowHub/TensorFlowHub.csproj
  2. +71
    -0
      src/TensorFlowHub/Utils.cs

+ 1
- 0
src/TensorFlowHub/TensorFlowHub.csproj View File

@@ -9,5 +9,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="NumSharp" Version="0.10.4" />
<PackageReference Include="sharpcompress" Version="0.23.0" />
</ItemGroup>
</Project>

+ 71
- 0
src/TensorFlowHub/Utils.cs View File

@@ -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<TDataSet>(this IModelLoader<TDataSet> 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<TDataSet>(this IModelLoader<TDataSet> 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();
}
}
}

Loading…
Cancel
Save