-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathArchiveHandler.cs
66 lines (56 loc) · 2.53 KB
/
ArchiveHandler.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
63
64
65
66
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
namespace UDGB
{
internal static class ArchiveHandler
{
private static AutoResetEvent ResetEvent_Output = new AutoResetEvent(false);
private static AutoResetEvent ResetEvent_Error = new AutoResetEvent(false);
internal static void CreateZip(string input_folder, string output_file)
{
if (File.Exists(output_file))
File.Delete(output_file);
ZipFile.CreateFromDirectory(input_folder, output_file);
}
internal static bool ExtractFiles(string output_path, string archive_path, string internal_path, bool keep_file_path = false)
{
ResetEvent_Output.Reset();
ResetEvent_Error.Reset();
Process process = new Process()
{
StartInfo = new ProcessStartInfo("7zip")
{
FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "7z/7z.exe"),
WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory,
Arguments = string.Join(" ", new string[] {
keep_file_path ? "x" : "e",
$"\"{archive_path}\"",
"-y",
$"-o\"{output_path}\"",
$"\"{internal_path}\"",
}.Where(s => !string.IsNullOrEmpty(s)).Select(it => Regex.Replace(it, @"(\\+)$", @"$1$1"))),
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
}
};
Logger.DebugMsg("\"" + process.StartInfo.FileName + "\" " + process.StartInfo.Arguments);
process.OutputDataReceived += OutputStream;
process.ErrorDataReceived += ErrorStream;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
ResetEvent_Output.WaitOne();
ResetEvent_Error.WaitOne();
return (process.ExitCode == 0);
}
private static void OutputStream(object sender, DataReceivedEventArgs e) { if (e.Data == null) ResetEvent_Output.Set(); else Logger.Msg(e.Data); }
private static void ErrorStream(object sender, DataReceivedEventArgs e) { if (e.Data == null) ResetEvent_Error.Set(); else Logger.Error(e.Data); }
}
}