CheckUpdateWindow.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using ICSharpCode.SharpZipLib.Zip;
  2. using System;
  3. using System.IO;
  4. using System.Net;
  5. using System.Windows.Forms;
  6. namespace UAS_AutoUpdate
  7. {
  8. public partial class CheckUpdateWindow : Form
  9. {
  10. public CheckUpdateWindow()
  11. {
  12. InitializeComponent();
  13. }
  14. private void CheckUpdateWindow_Load(object sender, EventArgs e)
  15. {
  16. //使用WebClient从指定位置下载文件,然后进行解压缩覆盖
  17. WebClient wc = new WebClient();
  18. wc.DownloadProgressChanged += Wc_DownloadProgressChanged;
  19. wc.DownloadFileAsync(new Uri("http://218.17.158.219:8888/UAS_MES.zip"), "UAS_MES.zip");
  20. }
  21. private void Wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  22. {
  23. Action act = () =>
  24. {
  25. _progressbar.Value = e.ProgressPercentage;
  26. _processrate.Text = e.ProgressPercentage + "%";
  27. };
  28. if (IsHandleCreated)
  29. this.Invoke(act);
  30. if (e.ProgressPercentage == 100)
  31. {
  32. ZipHelper.UnZip(Application.StartupPath + @"\UAS_MES.zip", Application.StartupPath);
  33. File.Delete(Application.StartupPath + @"\UAS_MES.zip");
  34. }
  35. }
  36. }
  37. public class ZipHelper
  38. {
  39. /// <summary>
  40. /// 用于解压缩Zip文件
  41. /// </summary>
  42. /// <param name="ZipFilePath"></param>
  43. /// <param name="UnZipPath"></param>
  44. public static void UnZip(string ZipFilePath, string UnZipPath)
  45. {
  46. if (!File.Exists(ZipFilePath))
  47. {
  48. throw new FileNotFoundException(string.Format("未能找到文件 '{0}' ", ZipFilePath));
  49. }
  50. if (!Directory.Exists(UnZipPath))
  51. {
  52. Directory.CreateDirectory(UnZipPath);
  53. }
  54. using (var s = new ZipInputStream(File.OpenRead(ZipFilePath)))
  55. {
  56. ZipEntry theEntry;
  57. while ((theEntry = s.GetNextEntry()) != null)
  58. {
  59. if (theEntry.IsDirectory)
  60. {
  61. continue;
  62. }
  63. string directorName = Path.Combine(UnZipPath, Path.GetDirectoryName(theEntry.Name));
  64. string fileName = Path.Combine(directorName, Path.GetFileName(theEntry.Name));
  65. if (!Directory.Exists(directorName))
  66. {
  67. Directory.CreateDirectory(directorName);
  68. }
  69. if (!string.IsNullOrEmpty(fileName))
  70. {
  71. using (FileStream streamWriter = File.Create(fileName))
  72. {
  73. int size = 4096;
  74. byte[] data = new byte[size];
  75. while (size > 0)
  76. {
  77. size = s.Read(data, 0, data.Length);
  78. streamWriter.Write(data, 0, size);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }