-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileHandler.cs
62 lines (56 loc) · 1.81 KB
/
FileHandler.cs
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
using System.IO.Compression;
namespace KnxFileTransferClient;
internal class FileHandler
{
public static long GetBytes(MemoryStream result, string path)
{
string extension = path.Substring(path.LastIndexOf("."));
switch(extension)
{
case ".bin":
{
int size = 0;
using(GZipStream compressionStream = new GZipStream(result, CompressionMode.Compress, true))
{
using(FileStream fs = System.IO.File.Open(path, FileMode.Open))
{
size = (int)fs.Length;
fs.CopyTo(compressionStream);
fs.Flush();
}
}
result.Position = 0;
return size;
}
case ".gz":
{
result.Write(System.IO.File.ReadAllBytes(path));
result.Position = 0;
return result.Length;
}
case ".uf2":
{
int size = 0;
using(GZipStream compressionStream = new GZipStream(result, CompressionMode.Compress, true))
{
byte[] data = Converter.ToBin(path);
size = data.Length;
foreach(byte d in data)
compressionStream.WriteByte(d);
compressionStream.Flush();
}
result.Position = 0;
return size;
}
}
throw new Exception("Nicht unterstütztes Dateiformat: " + extension);
}
public static byte[] GetBytes(string path)
{
using(MemoryStream ms = new MemoryStream())
{
GetBytes(ms, path);
return ms.ToArray();
}
}
}