123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- using ICSharpCode.SharpZipLib.Zip;
- using System;
- using System.IO;
- using System.Net;
- using System.Windows.Forms;
- namespace UAS_AutoUpdate
- {
- public partial class CheckUpdateWindow : Form
- {
- public static bool Zipped = false;
- public CheckUpdateWindow()
- {
- InitializeComponent();
- StartPosition = FormStartPosition.CenterScreen;
- }
- private void CheckUpdateWindow_Load(object sender, EventArgs e)
- {
-
- WebClient wc = new WebClient();
- wc.DownloadProgressChanged += Wc_DownloadProgressChanged;
- wc.DownloadFileAsync(new Uri("http://172.16.11.99/UAS_MES.zip"), "UAS_MES.zip");
- }
- private void Wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
- {
- Action act = () =>
- {
- _progressbar.Value = e.ProgressPercentage;
- _processrate.Text = e.ProgressPercentage + "%";
- };
- if (IsHandleCreated)
- this.Invoke(act);
- if (e.ProgressPercentage == 100)
- {
- if (!Zipped)
- {
- ZipHelper.UnZip(Application.StartupPath + @"\UAS_MES.zip", Application.StartupPath);
- }
- else
- {
- Close();
- }
- }
- }
- }
- public class ZipHelper
- {
-
-
-
-
-
- public static void UnZip(string ZipFilePath, string UnZipPath)
- {
- if (!File.Exists(ZipFilePath))
- {
- throw new FileNotFoundException(string.Format("未能找到文件 '{0}' ", ZipFilePath));
- }
- if (!Directory.Exists(UnZipPath))
- {
- Directory.CreateDirectory(UnZipPath);
- }
- using (var s = new ZipInputStream(File.OpenRead(ZipFilePath)))
- {
- ZipEntry theEntry;
- while ((theEntry = s.GetNextEntry()) != null)
- {
- if (theEntry.IsDirectory)
- {
- continue;
- }
- string directorName = Path.Combine(UnZipPath, Path.GetDirectoryName(theEntry.Name));
- string fileName = Path.Combine(directorName, Path.GetFileName(theEntry.Name));
- if (!Directory.Exists(directorName))
- {
- Directory.CreateDirectory(directorName);
- }
- if (!string.IsNullOrEmpty(fileName))
- {
- try
- {
-
- using (FileStream streamWriter = File.Create(fileName))
- {
- int size = 4096;
- byte[] data = new byte[size];
- while (size > 0)
- {
- size = s.Read(data, 0, data.Length);
- streamWriter.Write(data, 0, size);
- }
- }
- }
- catch (Exception)
- {
- }
- }
- }
- File.Delete(Application.StartupPath + @"\UAS_MES.zip");
- CheckUpdateWindow.Zipped = true;
- }
- }
- }
- }
|