-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
53 lines (43 loc) · 1.57 KB
/
Program.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
using MessagePack;
if (args.Length != 1)
{
Console.WriteLine("Usage: MsgPacker <filepath>");
Environment.Exit(1);
}
var path = args[0];
var dir = Path.GetDirectoryName(path);
if (string.IsNullOrWhiteSpace(dir))
dir = Environment.CurrentDirectory;
var files = Directory.GetFiles(dir, Path.GetFileName(path));
if (files.Length == 0)
{
Console.WriteLine("No files found");
Environment.Exit(1);
}
var outputDir = Path.Combine(Environment.CurrentDirectory, "output");
if (!Directory.Exists(outputDir)) Directory.CreateDirectory(outputDir);
foreach (var file in files)
{
var (name, ext) = (Path.GetFileNameWithoutExtension(file), Path.GetExtension(file).ToLower());
string outputPath;
switch (ext)
{
case ".msg":
outputPath = Path.Combine(outputDir, $"{name}.json");
var bytes = await File.ReadAllBytesAsync(file);
await File.WriteAllTextAsync(outputPath, MessagePackSerializer.ConvertToJson(bytes));
Console.WriteLine($"Converted {name}.msg to {outputPath} ");
break;
case ".json":
outputPath = Path.Combine(outputDir, $"{name}.msg");
var text = await File.ReadAllTextAsync(file);
File.WriteAllBytes(outputPath, MessagePackSerializer.ConvertFromJson(text));
Console.WriteLine($"Converted {name}.json to {outputPath} ");
break;
default:
Console.WriteLine("Invalid file extension");
Environment.Exit(1);
break;
}
}
Console.WriteLine("All files have been converted successfully.");