forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_utils.cs
More file actions
74 lines (64 loc) · 2.35 KB
/
file_utils.cs
File metadata and controls
74 lines (64 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using SharpCompress.Common;
using SharpCompress.Readers;
using System;
using System.IO;
namespace Tensorflow.Hub
{
internal static class file_utils
{
//public static void extract_file(TarInputStream tgz, TarEntry tarInfo, string dstPath, uint bufferSize = 10 << 20, Action<long> logFunction = null)
//{
// using (var src = tgz.GetNextEntry() == tarInfo ? tgz : null)
// {
// if (src is null)
// {
// return;
// }
// using (var dst = File.Create(dstPath))
// {
// var buffer = new byte[bufferSize];
// int count;
// while ((count = src.Read(buffer, 0, buffer.Length)) > 0)
// {
// dst.Write(buffer, 0, count);
// logFunction?.Invoke(count);
// }
// }
// }
//}
public static void extract_tarfile_to_destination(Stream fileobj, string dst_path, Action<long> logFunction = null)
{
using (IReader reader = ReaderFactory.Open(fileobj))
{
while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory)
{
reader.WriteEntryToDirectory(
dst_path,
new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }
);
}
}
}
}
public static string merge_relative_path(string dstPath, string relPath)
{
var cleanRelPath = Path.GetFullPath(relPath).TrimStart('/', '\\');
if (cleanRelPath == ".")
{
return dstPath;
}
if (cleanRelPath.StartsWith("..") || Path.IsPathRooted(cleanRelPath))
{
throw new InvalidDataException($"Relative path '{relPath}' is invalid.");
}
var merged = Path.Combine(dstPath, cleanRelPath);
if (!merged.StartsWith(dstPath))
{
throw new InvalidDataException($"Relative path '{relPath}' is invalid. Failed to merge with '{dstPath}'.");
}
return merged;
}
}
}