-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
328 lines (281 loc) · 11.9 KB
/
MainWindow.xaml.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using Newtonsoft.Json;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Path = System.IO.Path;
namespace CloudDownload
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private const string UpdateUrl = "https://example.com/update/update.zip";
private const string LocalUpdatePath = "update.zip";
private const string LocalFilePath = "E:\\230524_095505.s_temp"; // 本地文件路径
private const string LocalZipFilePath = "https://tipscope-app-image.oss-cn-hangzhou.aliyuncs.com/public/scan/update/MultiScan.zip"; // 云端下载地址
private const string ExtractedFolderPath = "extracted"; // 解压缩后的文件夹路径
private CancellationTokenSource cancellationTokenSource;
public MainWindow()
{
InitializeComponent();
}
private bool HttpFileExist(string httpFileUrl)
{
WebResponse response = null;
bool result = false;
try
{
response = WebRequest.Create(httpFileUrl).GetResponse();
result = response != null;
}
catch (Exception)
{
result = false;
}
finally
{
response?.Close();
}
return result;
}
private void UpdateProgressText(int progressPercentage)
{
progressText.Text = $"{progressPercentage}%";
}
private void ExtractZipFile(string zipFilePath, string targetDirectory)
{
try
{
using (ZipArchive archive = ZipFile.OpenRead(zipFilePath))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
string entryPath = Path.Combine(targetDirectory, entry.FullName);
entryPath = Path.GetFullPath(entryPath);
if (!entryPath.StartsWith(targetDirectory, StringComparison.OrdinalIgnoreCase))
{
throw new IOException("目录遍历攻击检测!");
}
if (File.Exists(entryPath))
{
File.Delete(entryPath);
}
Directory.CreateDirectory(Path.GetDirectoryName(entryPath));
entry.ExtractToFile(entryPath);
}
}
}
catch (Exception ex)
{
MessageBox.Show($"解压缩文件时出错:{ex.Message}");
}
}
private void CancelDownloadButton_Click(object sender, RoutedEventArgs e)
{
try
{
// 取消下载任务
if (cancellationTokenSource != null)
{
cancellationTokenSource.Cancel();
}
Task.Run(() =>
{
Process[] p = Process.GetProcessesByName(LocalUpdatePath);
foreach (var item in p)
{
item.Kill();
}
Thread.Sleep(200);
File.Delete(LocalUpdatePath);
});
}
catch (Exception ex)
{
MessageBox.Show($"取消下载时出错:{ex.Message}");
}
}
private async Task DownloadFileWithProgress(string url, string savePath, ProgressBar progressBar, CancellationToken cancellationToken)
{
using (var httpClient = new HttpClient())
{
try
{
// 发起 HTTP 请求获取流
using (var response = await httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, cancellationToken))
{
response.EnsureSuccessStatusCode();
var contentLength = response.Content.Headers.ContentLength;
using (var remoteStream = await response.Content.ReadAsStreamAsync())
{
// 执行下载
using (var fileStream = new FileStream(savePath, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true))
{
var buffer = new byte[4096];
var bytesRead = 0;
var totalBytes = 0;
do
{
// 检查是否取消
cancellationToken.ThrowIfCancellationRequested();
bytesRead = await remoteStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);
if (bytesRead > 0)
{
await fileStream.WriteAsync(buffer, 0, bytesRead, cancellationToken);
totalBytes += bytesRead;
// 更新进度条
if (contentLength.HasValue && contentLength > 0)
{
var progressPercentage = (int)((double)totalBytes / contentLength.Value * 100);
progressBar.Value = progressPercentage;
if(progressPercentage<1) progressPercentage = 1;
UpdateProgressText(progressPercentage - 1);
}
}
} while (bytesRead > 0);
}
}
}
// 下载完成后的其他操作(解压缩、删除等)
if (!cancellationToken.IsCancellationRequested)
{
string str= CalculateMD5(savePath);
ExtractZipFile(savePath, AppDomain.CurrentDomain.BaseDirectory);
File.Delete(savePath);
// 下载完成后更新进度条和文本
await Dispatcher.InvokeAsync(() =>
{
progressBar.Value = progressBar.Maximum;
UpdateProgressText(100);
MessageBox.Show("压缩包下载并解压成功,保存的 ZIP 文件已删除!");
});
}
else
{
MessageBox.Show("下载被取消!");
}
}
catch (OperationCanceledException)
{
MessageBox.Show("下载被取消!");
}
catch (Exception ex)
{
MessageBox.Show($"下载文件时出错:{ex.Message}");
}
}
}
private async void DownloadAndExtractButton_Click(object sender, RoutedEventArgs e)
{
try
{
// 取消之前的下载任务
if (cancellationTokenSource != null)
{
cancellationTokenSource.Cancel();
}
// 创建新的 CancellationTokenSource
cancellationTokenSource = new CancellationTokenSource();
if (HttpFileExist(LocalZipFilePath))
{
// 下载文件并显示进度
await DownloadFileWithProgress(LocalZipFilePath, LocalUpdatePath, progressBar, cancellationTokenSource.Token);
}
else
{
MessageBox.Show("远程文件不存在!");
}
}
catch (Exception ex)
{
MessageBox.Show($"下载和解压缩文件时出错:{ex.Message}");
}
}
private string CalculateMD5(string filePath)
{
using (MD5 md5 = MD5.Create())
{
using (FileStream stream = File.OpenRead(filePath))
{
byte[] hashBytes = md5.ComputeHash(stream);
return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
}
}
}
private bool CheckInternetConnection()
{
try
{
// 尝试通过 Ping 命令检测互联网连接
using (Ping ping = new Ping())
{
// 选择一个可以被访问的互联网地址,例如谷歌的 DNS 服务器
string host = "8.8.8.8";
// 设置超时时间
int timeout = 1000; // 1 秒
// 发送 Ping 请求
PingReply reply = ping.Send(host, timeout);
// 判断 Ping 请求是否成功
return reply.Status == IPStatus.Success;
}
}
catch
{
// 发生异常表示互联网连接失败
return false;
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
string jsonString = "{ \"code\": 0, \"data\": { \"serialNumber\":\"asdfasdfas\", \"firewareVer\": \"v1.2\", \"downloadUrl\": \"https://...\", \"fileMd5\": \"asfk3r232113\", \"fileSize\": 13412348932 ,\"MinSupportVer\": \"v1.1\"}, \"msg\": \"success\" }";
ApiResponse apiResponse = ParseJson(jsonString);
if (apiResponse.Code == 0)
{
// 访问解析后的数据
Console.WriteLine($"Serial Number: {apiResponse.Data.SerialNumber}");
Console.WriteLine($"Firmware Version: {apiResponse.Data.FirewareVer}");
Console.WriteLine($"Download URL: {apiResponse.Data.DownloadUrl}");
Console.WriteLine($"File MD5: {apiResponse.Data.FileMd5}");
Console.WriteLine($"File Size: {apiResponse.Data.FileSize}");
Console.WriteLine($"MinSupportVer:{apiResponse.Data.MinSupportVer}");
}
else
{
// 解析失败
Console.WriteLine($"Failed with message: {apiResponse.Msg}");
}
}
public static ApiResponse ParseJson(string jsonString)
{
return JsonConvert.DeserializeObject<ApiResponse>(jsonString);
}
}
public class ApiResponse
{
public int Code { get; set; }
public Data Data { get; set; }
public string Msg { get; set; }
}
public class Data
{
public string SerialNumber { get; set; }
public string FirewareVer { get; set; }
public string DownloadUrl { get; set; }
public string FileMd5 { get; set; }
public long FileSize { get; set; }
public string? MinSupportVer { get; set; }
}
}