CheckUpdateWindow.cs 3.1 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. MessageBox.Show(Application.StartupPath);
  18. WebClient wc = new WebClient();
  19. wc.DownloadProgressChanged += Wc_DownloadProgressChanged;
  20. wc.DownloadFileAsync(new Uri("http://218.17.158.219:8888/Debug.zip"), "Debug.zip");
  21. }
  22. private void Wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  23. {
  24. Action act = () =>
  25. {
  26. _progressbar.Value = e.ProgressPercentage;
  27. _processrate.Text = e.ProgressPercentage + "%";
  28. };
  29. if (IsHandleCreated)
  30. this.Invoke(act);
  31. if (e.ProgressPercentage == 100)
  32. {
  33. ZipHelper.UnZip(Application.StartupPath + @"\Debug.zip", @"F:\TEST");
  34. }
  35. //Close();
  36. }
  37. }
  38. public class ZipHelper
  39. {
  40. /// <summary>
  41. /// 用于解压缩Zip文件
  42. /// </summary>
  43. /// <param name="ZipFilePath"></param>
  44. /// <param name="UnZipPath"></param>
  45. public static void UnZip(string ZipFilePath, string UnZipPath)
  46. {
  47. if (!File.Exists(ZipFilePath))
  48. {
  49. throw new FileNotFoundException(string.Format("未能找到文件 '{0}' ", ZipFilePath));
  50. }
  51. if (!Directory.Exists(UnZipPath))
  52. {
  53. Directory.CreateDirectory(UnZipPath);
  54. }
  55. using (var s = new ZipInputStream(File.OpenRead(ZipFilePath)))
  56. {
  57. ZipEntry theEntry;
  58. while ((theEntry = s.GetNextEntry()) != null)
  59. {
  60. if (theEntry.IsDirectory)
  61. {
  62. continue;
  63. }
  64. string directorName = Path.Combine(UnZipPath, Path.GetDirectoryName(theEntry.Name));
  65. string fileName = Path.Combine(directorName, Path.GetFileName(theEntry.Name));
  66. if (!Directory.Exists(directorName))
  67. {
  68. Directory.CreateDirectory(directorName);
  69. }
  70. if (!string.IsNullOrEmpty(fileName))
  71. {
  72. using (FileStream streamWriter = File.Create(fileName))
  73. {
  74. int size = 4096;
  75. byte[] data = new byte[size];
  76. while (size > 0)
  77. {
  78. streamWriter.Write(data, 0, size);
  79. size = s.Read(data, 0, data.Length);
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }