CheckUpdateWindow.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 static bool Zipped = false;
  11. public CheckUpdateWindow()
  12. {
  13. InitializeComponent();
  14. StartPosition = FormStartPosition.CenterScreen;
  15. }
  16. private void CheckUpdateWindow_Load(object sender, EventArgs e)
  17. {
  18. //使用WebClient从指定位置下载文件,然后进行解压缩覆盖
  19. WebClient wc = new WebClient();
  20. wc.DownloadProgressChanged += Wc_DownloadProgressChanged;
  21. wc.DownloadFileAsync(new Uri("http://172.16.11.99/UAS_MES.zip"), "UAS_MES.zip");
  22. }
  23. private void Wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  24. {
  25. Action act = () =>
  26. {
  27. _progressbar.Value = e.ProgressPercentage;
  28. _processrate.Text = e.ProgressPercentage + "%";
  29. };
  30. if (IsHandleCreated)
  31. this.Invoke(act);
  32. if (e.ProgressPercentage == 100)
  33. {
  34. if (!Zipped)
  35. {
  36. ZipHelper.UnZip(Application.StartupPath + @"\UAS_MES.zip", Application.StartupPath);
  37. }
  38. else
  39. {
  40. Close();
  41. }
  42. }
  43. }
  44. }
  45. public class ZipHelper
  46. {
  47. /// <summary>
  48. /// 用于解压缩Zip文件
  49. /// </summary>
  50. /// <param name="ZipFilePath"></param>
  51. /// <param name="UnZipPath"></param>
  52. public static void UnZip(string ZipFilePath, string UnZipPath)
  53. {
  54. if (!File.Exists(ZipFilePath))
  55. {
  56. throw new FileNotFoundException(string.Format("未能找到文件 '{0}' ", ZipFilePath));
  57. }
  58. if (!Directory.Exists(UnZipPath))
  59. {
  60. Directory.CreateDirectory(UnZipPath);
  61. }
  62. using (var s = new ZipInputStream(File.OpenRead(ZipFilePath)))
  63. {
  64. ZipEntry theEntry;
  65. while ((theEntry = s.GetNextEntry()) != null)
  66. {
  67. if (theEntry.IsDirectory)
  68. {
  69. continue;
  70. }
  71. string directorName = Path.Combine(UnZipPath, Path.GetDirectoryName(theEntry.Name));
  72. string fileName = Path.Combine(directorName, Path.GetFileName(theEntry.Name));
  73. if (!Directory.Exists(directorName))
  74. {
  75. Directory.CreateDirectory(directorName);
  76. }
  77. if (!string.IsNullOrEmpty(fileName))
  78. {
  79. try
  80. {
  81. //防止文件正在使用中报错
  82. using (FileStream streamWriter = File.Create(fileName))
  83. {
  84. int size = 4096;
  85. byte[] data = new byte[size];
  86. while (size > 0)
  87. {
  88. size = s.Read(data, 0, data.Length);
  89. streamWriter.Write(data, 0, size);
  90. }
  91. }
  92. }
  93. catch (Exception)
  94. {
  95. }
  96. }
  97. }
  98. File.Delete(Application.StartupPath + @"\UAS_MES.zip");
  99. CheckUpdateWindow.Zipped = true;
  100. }
  101. }
  102. }
  103. }