Browse Source

fixed a bug about unzip

tags/v0.12
Kerry Jiang 6 years ago
parent
commit
ee9aef8472
2 changed files with 14 additions and 23 deletions
  1. +0
    -1
      src/TensorFlowHub/TensorFlowHub.csproj
  2. +14
    -22
      src/TensorFlowHub/Utils.cs

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

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

+ 14
- 22
src/TensorFlowHub/Utils.cs View File

@@ -1,14 +1,11 @@
using System;
using System.IO;
using System.IO.Compression;
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
{
@@ -37,7 +34,7 @@ namespace Tensorflow.Hub
}
}

public static void Unzip<TDataSet>(this IModelLoader<TDataSet> modelLoader, string zipFile, string saveTo)
public static async Task UnzipAsync<TDataSet>(this IModelLoader<TDataSet> modelLoader, string zipFile, string saveTo)
where TDataSet : IDataSet
{
if (!Path.IsPathRooted(saveTo))
@@ -46,27 +43,22 @@ namespace Tensorflow.Hub
if (!Directory.Exists(saveTo))
Directory.CreateDirectory(saveTo);

using (var stream = File.OpenRead(zipFile))
using (var reader = ReaderFactory.Open(stream))
var destFilePath = Path.Combine(saveTo, Path.GetFileNameWithoutExtension(zipFile));

if (File.Exists(destFilePath))
File.Delete(destFilePath);

using (GZipStream unzipStream = new GZipStream(File.OpenRead(zipFile), CompressionMode.Decompress))
{
while (reader.MoveToNextEntry())
using (var destStream = File.Create(destFilePath))
{
if (!reader.Entry.IsDirectory)
{
reader.WriteEntryToDirectory(saveTo, new ExtractionOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
await unzipStream.CopyToAsync(destStream);
await destStream.FlushAsync();
destStream.Close();
}
}
}

public static async Task UnzipAsync<TDataSet>(this IModelLoader<TDataSet> modelLoader, string zipFile, string saveTo)
where TDataSet : IDataSet
{
await Task.Run(() => modelLoader.Unzip(zipFile, saveTo));
unzipStream.Close();
}
}

public static async Task ShowProgressInConsole(this Task task)


Loading…
Cancel
Save