-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathFileMgr.cs
More file actions
181 lines (157 loc) · 7.42 KB
/
Copy pathFileMgr.cs
File metadata and controls
181 lines (157 loc) · 7.42 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System.Collections.Generic;
using static RCRL.LauncherForm;
namespace RCRL
{
internal class FileMgr
{
public async Task DownloadEveryFile(String whereis, String whereitneed, List<string> list, ProgressBar pb, Label tb)
{
foreach (var item in list.Select((value, index) => new { Value = value, Index = index }))
{
using (var client = new HttpClientDownloadWithProgress(Path.Combine(whereis, item.Value), Path.Combine(whereitneed, item.Value)))
{
client.ProgressChanged += (totalFileSize, totalBytesDownloaded, progressPercentage) =>
{
pb.Value = Convert.ToInt32(((Convert.ToDouble(item.Index) / Convert.ToDouble(list.Count)) * 100) + (progressPercentage/ Convert.ToDouble(list.Count)));
tb.Text = $"[Загрузка {item.Value} ({item.Index}/{list.Count})]";
};
await client.StartDownload();
}
}
tb.Text = "";
}
public async Task DownloadAndUnpack(String whereis, String whereitneed)
{
using (var client = new HttpClientDownloadWithProgress(whereis, Path.Combine(GlobalPaths.datapath, Path.GetFileName(whereis))))
{
await client.StartDownload();
}
System.IO.Compression.ZipFile.ExtractToDirectory(Path.Combine(GlobalPaths.datapath, Path.GetFileName(whereis)), whereitneed, true);
File.Delete(Path.Combine(GlobalPaths.datapath, Path.GetFileName(whereis)));
}
public async Task DownloadAndUnpack(String whereis, String whereitneed, ProgressBar pb, Label tb)
{
using (var client = new HttpClientDownloadWithProgress(whereis, Path.Combine(GlobalPaths.datapath, Path.GetFileName(whereis))))
{
client.ProgressChanged += (totalFileSize, totalBytesDownloaded, progressPercentage) =>
{
pb.Value = Convert.ToInt32(progressPercentage);
tb.Text = $"[Загрузка {Path.GetFileName(whereis)}: {totalBytesDownloaded}/{totalFileSize}]";
};
await client.StartDownload();
}
tb.Text = "Распаковка файлов...";
System.IO.Compression.ZipFile.ExtractToDirectory(Path.Combine(GlobalPaths.datapath, Path.GetFileName(whereis)), whereitneed, true);
File.Delete(Path.Combine(GlobalPaths.datapath, Path.GetFileName(whereis)));
tb.Text = "";
}
public async Task cleanMcFolder(String mcpath)
{
try { Directory.Delete(Path.Combine(mcpath, "assets"), true); }
catch (DirectoryNotFoundException ex) { }
try { Directory.Delete(Path.Combine(mcpath, "libraries"), true); }
catch (DirectoryNotFoundException ex) { }
try { Directory.Delete(Path.Combine(mcpath, "runtime"), true); }
catch (DirectoryNotFoundException ex) { }
try { Directory.Delete(Path.Combine(mcpath, "versions"), true); }
catch (DirectoryNotFoundException ex) { }
}
public async Task removeAllBut(String pathTo, IEnumerable<string> whatNeeded)
{
if (Directory.Exists(pathTo))
{
foreach (var element in Directory.GetFiles(pathTo))
{
if (!whatNeeded.Contains(Path.GetFileName(element)))
{
try
{
File.Delete(element);
}
catch (Exception ex)
{
}
}
}
}
}
public List<string> getListOfDownloads(String path, List<string> list)
{
Directory.CreateDirectory(path);
List<string> todwn = new List<string>();
foreach (var element in list)
{
if (!Directory.GetFiles(path).Contains(Path.Combine(path, element)))
{
todwn.Add(element);
}
}
return todwn;
}
public class HttpClientDownloadWithProgress : IDisposable
{
private readonly string _downloadUrl;
private readonly string _destinationFilePath;
private HttpClient _httpClient;
public delegate void ProgressChangedHandler(long? totalFileSize, long totalBytesDownloaded, double? progressPercentage);
public event ProgressChangedHandler ProgressChanged;
public HttpClientDownloadWithProgress(string downloadUrl, string destinationFilePath)
{
_downloadUrl = downloadUrl;
_destinationFilePath = destinationFilePath;
}
public async Task StartDownload()
{
_httpClient = new HttpClient { Timeout = TimeSpan.FromDays(1) };
using (var response = await _httpClient.GetAsync(_downloadUrl, HttpCompletionOption.ResponseHeadersRead))
await DownloadFileFromHttpResponseMessage(response);
}
private async Task DownloadFileFromHttpResponseMessage(HttpResponseMessage response)
{
response.EnsureSuccessStatusCode();
var totalBytes = response.Content.Headers.ContentLength;
using (var contentStream = await response.Content.ReadAsStreamAsync())
await ProcessContentStream(totalBytes, contentStream);
}
private async Task ProcessContentStream(long? totalDownloadSize, Stream contentStream)
{
var totalBytesRead = 0L;
var readCount = 0L;
var buffer = new byte[8192];
var isMoreToRead = true;
using (var fileStream = new FileStream(_destinationFilePath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true))
{
do
{
var bytesRead = await contentStream.ReadAsync(buffer, 0, buffer.Length);
if (bytesRead == 0)
{
isMoreToRead = false;
TriggerProgressChanged(totalDownloadSize, totalBytesRead);
continue;
}
await fileStream.WriteAsync(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
readCount += 1;
if (readCount % 100 == 0)
TriggerProgressChanged(totalDownloadSize, totalBytesRead);
}
while (isMoreToRead);
}
}
private void TriggerProgressChanged(long? totalDownloadSize, long totalBytesRead)
{
if (ProgressChanged == null)
return;
double? progressPercentage = null;
if (totalDownloadSize.HasValue)
progressPercentage = Math.Round((double)totalBytesRead / totalDownloadSize.Value * 100, 2);
ProgressChanged(totalDownloadSize, totalBytesRead, progressPercentage);
}
public void Dispose()
{
_httpClient?.Dispose();
}
}
}
}