3e654e1ec02c122c2fbbea343c5c93973bdb8ad2.svn-base 18 KB

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