8e08ab1b211d8ab2db35657c7b34b5668c6dea90.svn-base 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. using System.Collections;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Windows.Forms;
  6. using System.Net;
  7. using System;
  8. using System.Text;
  9. namespace Mes.AutoUpdate
  10. {
  11. public delegate void Action<T1, T2>(T1 _t1, T2 _t2);
  12. public static class DownManager
  13. {
  14. private static string main_progress = "";
  15. private static string sub_progress = "";
  16. private static int total_updated = 0;
  17. private static int total_files = 0;
  18. private static Action<string, string> _sProgress = null;
  19. public static void DownLoad(ArrayList files, string HTTP_Address, string FTP_Address, Action<int, int> m_Progress, Action<string, string> s_Progress, out ArrayList Finished)
  20. {
  21. string fileName = Process.GetCurrentProcess().MainModule.FileName;
  22. string errMsg = "";
  23. _sProgress = s_Progress;
  24. total_updated = 0;
  25. Finished = new ArrayList();
  26. total_files = files.Count;
  27. try
  28. {
  29. foreach (object file in files)
  30. {
  31. if (m_Progress != null)
  32. m_Progress(total_updated, files.Count);
  33. string startupPath = Application.StartupPath;
  34. string fileName2 = Path.GetFileName(file.ToString());
  35. string text = startupPath + "\\" + fileName2;
  36. if (Path.GetFullPath(text).ToUpper() == Path.GetFullPath(fileName).ToUpper())
  37. {
  38. total_updated++;
  39. }
  40. else
  41. {
  42. bool flag = false;
  43. main_progress = fileName2;
  44. for (int i = 0; i < 4; i++)
  45. {
  46. if (i > 0)
  47. {
  48. main_progress = string.Format("{0} 更新出错,尝试重新下载[{i}/3]", fileName2);
  49. sub_progress = "";
  50. inner_Progress(0, 1);
  51. Thread.Sleep(1500);
  52. }
  53. if (HTTP_Helper.DownFile(Path.Combine(HTTP_Address, file.ToString()), text, out errMsg, inner_Progress))
  54. {
  55. flag = true;
  56. Finished.Add(fileName2);
  57. total_updated++;
  58. break;
  59. }
  60. }
  61. if (!flag)
  62. {
  63. for (int i = 0; i < 4; i++)
  64. {
  65. main_progress = string.Format("{0} 更新出错,按FTP方式重新下载[{1}/3]", fileName2, i + 1);
  66. sub_progress = "";
  67. inner_Progress(0, 1);
  68. Thread.Sleep(100);
  69. if (FTP_Helper.FtpDownload(Path.Combine(FTP_Address, file.ToString()), text, false, inner_Progress))
  70. {
  71. flag = true;
  72. Finished.Add(fileName2);
  73. total_updated++;
  74. break;
  75. }
  76. }
  77. }
  78. if (!flag)
  79. {
  80. throw new Exception("连续3次更新失败, 请稍后重试!\n" + errMsg);
  81. }
  82. }
  83. }
  84. }
  85. catch (Exception ex)
  86. {
  87. throw new Exception(ex.Message);
  88. }
  89. }
  90. private static void inner_Progress(int a, int b)
  91. {
  92. sub_progress = string.Format("{0,4}/{1,-4} {2,3}% ", total_updated + 1, total_files, a * 100 / b);
  93. if (_sProgress != null)
  94. {
  95. _sProgress(sub_progress, main_progress);
  96. }
  97. }
  98. }
  99. public static class FTP_Helper
  100. {
  101. public static string FtpUserID = string.Empty;
  102. public static string FtpPassword = string.Empty;
  103. public static bool FtpDownload(string remoteFileName, string localFileName, bool ifCredential, Action<int, int> updateProgress)
  104. {
  105. Stream stream = null;
  106. FtpWebResponse ftpWebResponse = null;
  107. FileStream fileStream = null;
  108. try
  109. {
  110. fileStream = new FileStream(localFileName, FileMode.Create);
  111. Uri requestUri = new Uri(remoteFileName);
  112. FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(requestUri);
  113. ftpWebRequest.UseBinary = true;
  114. FtpWebRequest ftpWebRequest2 = (FtpWebRequest)WebRequest.Create(requestUri);
  115. ftpWebRequest2.UseBinary = true;
  116. ftpWebRequest2.KeepAlive = false;
  117. if (ifCredential)
  118. {
  119. ftpWebRequest.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  120. ftpWebRequest2.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  121. }
  122. ftpWebRequest.Method = "SIZE";
  123. FtpWebResponse ftpWebResponse2 = (FtpWebResponse)ftpWebRequest.GetResponse();
  124. long contentLength = ftpWebResponse2.ContentLength;
  125. ftpWebResponse2.Close();
  126. ftpWebRequest2.Method = "RETR";
  127. ftpWebResponse = (FtpWebResponse)ftpWebRequest2.GetResponse();
  128. stream = ftpWebResponse.GetResponseStream();
  129. if (updateProgress != null)
  130. updateProgress(0, (int)contentLength);
  131. long num = 0L;
  132. int num2 = 2048;
  133. byte[] buffer = new byte[num2];
  134. for (int num3 = stream.Read(buffer, 0, num2); num3 > 0; num3 = stream.Read(buffer, 0, num2))
  135. {
  136. num = num3 + num;
  137. fileStream.Write(buffer, 0, num3);
  138. if (updateProgress != null)
  139. updateProgress((int)num, (int)contentLength);
  140. }
  141. stream.Close();
  142. fileStream.Close();
  143. ftpWebResponse.Close();
  144. return true;
  145. }
  146. catch (Exception)
  147. {
  148. return false;
  149. }
  150. finally
  151. {
  152. stream.Close();
  153. fileStream.Close();
  154. ftpWebResponse.Close();
  155. }
  156. }
  157. public static bool FtpBrokenDownload(string remoteFileName, string localFileName, bool ifCredential, long size, Action<int, int> updateProgress)
  158. {
  159. Stream stream = null;
  160. FtpWebResponse ftpWebResponse = null;
  161. FileStream fileStream = null;
  162. try
  163. {
  164. fileStream = new FileStream(localFileName, FileMode.Append);
  165. Uri requestUri = new Uri(remoteFileName);
  166. FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(requestUri);
  167. ftpWebRequest.UseBinary = true;
  168. ftpWebRequest.ContentOffset = size;
  169. FtpWebRequest ftpWebRequest2 = (FtpWebRequest)WebRequest.Create(requestUri);
  170. ftpWebRequest2.UseBinary = true;
  171. ftpWebRequest2.KeepAlive = false;
  172. ftpWebRequest2.ContentOffset = size;
  173. if (ifCredential)
  174. {
  175. ftpWebRequest.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  176. ftpWebRequest2.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  177. }
  178. ftpWebRequest.Method = "SIZE";
  179. FtpWebResponse ftpWebResponse2 = (FtpWebResponse)ftpWebRequest.GetResponse();
  180. long contentLength = ftpWebResponse2.ContentLength;
  181. ftpWebResponse2.Close();
  182. ftpWebRequest2.Method = "RETR";
  183. ftpWebResponse = (FtpWebResponse)ftpWebRequest2.GetResponse();
  184. stream = ftpWebResponse.GetResponseStream();
  185. if (updateProgress != null)
  186. updateProgress(0, (int)contentLength);
  187. long num = 0L;
  188. int num2 = 2048;
  189. byte[] buffer = new byte[num2];
  190. for (int num3 = stream.Read(buffer, 0, num2); num3 > 0; num3 = stream.Read(buffer, 0, num2))
  191. {
  192. num = num3 + num;
  193. fileStream.Write(buffer, 0, num3);
  194. if (updateProgress != null)
  195. updateProgress((int)num, (int)contentLength);
  196. }
  197. stream.Close();
  198. fileStream.Close();
  199. ftpWebResponse.Close();
  200. return true;
  201. }
  202. catch (Exception)
  203. {
  204. return false;
  205. }
  206. finally
  207. {
  208. stream.Close();
  209. fileStream.Close();
  210. ftpWebResponse.Close();
  211. }
  212. }
  213. public static bool FtpDownload(string remoteFileName, string localFileName, bool ifCredential, bool brokenOpen, Action<int, int> updateProgress)
  214. {
  215. if (brokenOpen)
  216. {
  217. try
  218. {
  219. long size = 0L;
  220. if (File.Exists(localFileName))
  221. {
  222. using (FileStream fileStream = new FileStream(localFileName, FileMode.Open))
  223. {
  224. size = fileStream.Length;
  225. }
  226. }
  227. return FtpBrokenDownload(remoteFileName, localFileName, ifCredential, size, updateProgress);
  228. }
  229. catch
  230. {
  231. throw;
  232. }
  233. }
  234. return FtpDownload(remoteFileName, localFileName, ifCredential, updateProgress);
  235. }
  236. public static bool FtpUploadFile(string localFullPathName, string remotePath, Action<int, int> updateProgress)
  237. {
  238. Stream stream = null;
  239. FtpWebResponse ftpWebResponse = null;
  240. FileStream fileStream = null;
  241. try
  242. {
  243. FileInfo fileInfo = new FileInfo(localFullPathName);
  244. Uri requestUri = new Uri(remotePath);
  245. FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(requestUri);
  246. ftpWebRequest.KeepAlive = false;
  247. ftpWebRequest.UseBinary = true;
  248. ftpWebRequest.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  249. ftpWebRequest.Method = "STOR";
  250. ftpWebRequest.ContentLength = fileInfo.Length;
  251. ftpWebResponse = (ftpWebRequest.GetResponse() as FtpWebResponse);
  252. ftpWebRequest.ContentLength = fileInfo.Length;
  253. int num = 1024;
  254. byte[] buffer = new byte[num];
  255. fileStream = fileInfo.OpenRead();
  256. stream = ftpWebRequest.GetRequestStream();
  257. int num2 = fileStream.Read(buffer, 0, num);
  258. int num3 = (int)fileInfo.Length;
  259. if (updateProgress != null)
  260. updateProgress(0, num3);
  261. int num4 = 0;
  262. while (num2 != 0)
  263. {
  264. num4 = num2 + num4;
  265. stream.Write(buffer, 0, num2);
  266. if (updateProgress != null)
  267. updateProgress(num4, num3);
  268. num2 = fileStream.Read(buffer, 0, num);
  269. }
  270. stream.Close();
  271. fileStream.Close();
  272. ftpWebResponse.Close();
  273. return true;
  274. }
  275. catch (Exception)
  276. {
  277. return false;
  278. }
  279. finally
  280. {
  281. fileStream.Close();
  282. stream.Close();
  283. ftpWebResponse.Close();
  284. }
  285. }
  286. public static bool FtpUploadBroken(string localFullPath, string remoteFilepath, Action<int, int> updateProgress)
  287. {
  288. if (remoteFilepath == null)
  289. {
  290. remoteFilepath = "";
  291. }
  292. string empty = string.Empty;
  293. bool result = true;
  294. FileInfo fileInfo = new FileInfo(localFullPath);
  295. long length = fileInfo.Length;
  296. if (fileInfo.Name.IndexOf("#") == -1)
  297. {
  298. empty = RemoveSpaces(fileInfo.Name);
  299. }
  300. else
  301. {
  302. empty = fileInfo.Name.Replace("#", "#");
  303. empty = RemoveSpaces(empty);
  304. }
  305. long fileSize = GetFileSize(empty, remoteFilepath);
  306. if (fileSize >= length)
  307. {
  308. return false;
  309. }
  310. long num = fileSize;
  311. if (updateProgress != null)
  312. updateProgress((int)fileSize, (int)length);
  313. string uriString = (remoteFilepath.Length != 0) ? (remoteFilepath + "/" + remoteFilepath + "/" + empty) : (remoteFilepath + "/" + empty);
  314. FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(new Uri(uriString));
  315. ftpWebRequest.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  316. ftpWebRequest.KeepAlive = false;
  317. ftpWebRequest.Method = "APPE";
  318. ftpWebRequest.UseBinary = true;
  319. ftpWebRequest.ContentLength = fileInfo.Length;
  320. int num2 = 2048;
  321. byte[] buffer = new byte[num2];
  322. FileStream fileStream = fileInfo.OpenRead();
  323. Stream stream = null;
  324. try
  325. {
  326. stream = ftpWebRequest.GetRequestStream();
  327. fileStream.Seek(fileSize, SeekOrigin.Begin);
  328. int num3 = fileStream.Read(buffer, 0, num2);
  329. while (num3 != 0)
  330. {
  331. stream.Write(buffer, 0, num3);
  332. num3 = fileStream.Read(buffer, 0, num2);
  333. num += num3;
  334. if (updateProgress != null)
  335. updateProgress((int)num, (int)length);
  336. }
  337. stream.Close();
  338. fileStream.Close();
  339. }
  340. catch
  341. {
  342. result = false;
  343. throw;
  344. }
  345. finally
  346. {
  347. fileStream.Close();
  348. stream.Close();
  349. }
  350. return result;
  351. }
  352. private static string RemoveSpaces(string str)
  353. {
  354. string text = "";
  355. CharEnumerator enumerator = str.GetEnumerator();
  356. while (enumerator.MoveNext())
  357. {
  358. byte[] array = new byte[1];
  359. array = Encoding.ASCII.GetBytes(enumerator.Current.ToString());
  360. int num = array[0];
  361. if (num != 32)
  362. {
  363. text += enumerator.Current.ToString();
  364. }
  365. }
  366. string text2 = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
  367. return text.Split('.')[text.Split('.').Length - 2] + "." + text.Split('.')[text.Split('.').Length - 1];
  368. }
  369. public static long GetFileSize(string filename, string remoteFilepath)
  370. {
  371. try
  372. {
  373. if (remoteFilepath.Length == 0)
  374. {
  375. return 0L;
  376. }
  377. FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(remoteFilepath);
  378. ftpWebRequest.KeepAlive = false;
  379. ftpWebRequest.UseBinary = true;
  380. ftpWebRequest.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  381. ftpWebRequest.Method = "SIZE";
  382. FtpWebResponse ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
  383. return ftpWebResponse.ContentLength;
  384. }
  385. catch
  386. {
  387. return 0L;
  388. }
  389. }
  390. }
  391. public static class HTTP_Helper
  392. {
  393. public static bool DownFile(string URLAddress, string LocalAddress, out string errMsg, Action<int, int> updateProgress)
  394. {
  395. errMsg = "";
  396. FileStream fileStream = null;
  397. Stream stream = null;
  398. bool result = false;
  399. try
  400. {
  401. string startupPath = Application.StartupPath;
  402. string fileName = Path.GetFileName(URLAddress);
  403. string path = startupPath + "\\" + fileName;
  404. string fileName2 = Process.GetCurrentProcess().MainModule.FileName;
  405. if (Path.GetFullPath(path).ToUpper() == Path.GetFullPath(fileName2).ToUpper())
  406. {
  407. return true;
  408. }
  409. fileStream = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None);
  410. WebClient webClient = new WebClient();
  411. stream = webClient.OpenRead(URLAddress);
  412. int t = Convert.ToInt32(webClient.ResponseHeaders["Content-Length"]);
  413. byte[] array = new byte[10240];
  414. int count = array.Length;
  415. int num = 0;
  416. int num2 = 0;
  417. do
  418. {
  419. num = stream.Read(array, 0, count);
  420. num2 += num;
  421. if (num != 0)
  422. {
  423. fileStream.Write(array, 0, num);
  424. }
  425. if (updateProgress != null)
  426. updateProgress((int)num2, (int)t);
  427. }
  428. while (num != 0);
  429. fileStream.Flush();
  430. result = true;
  431. return result;
  432. }
  433. catch (Exception ex)
  434. {
  435. errMsg = ex.Message;
  436. return result;
  437. }
  438. finally
  439. {
  440. fileStream.Close();
  441. stream.Dispose();
  442. }
  443. }
  444. }
  445. }