FtpHelper.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. namespace vCloud.GeneratePackages.Tool
  7. {
  8. class FtpHelper
  9. {
  10. private const int bufferSize = 8192;
  11. /* FTP Get */
  12. public static void Get(string remoteFile, string localFile, Action<long> procressCallback = null, string user = "anonymous", string pass = null)
  13. {
  14. try
  15. {
  16. /* Create an FTP Request */
  17. var ftpRequest = (FtpWebRequest)FtpWebRequest.Create(remoteFile);
  18. /* Log in to the FTP Server with the User Name and Password Provided */
  19. ftpRequest.Credentials = new NetworkCredential(user, pass);
  20. /* When in doubt, use these options */
  21. ftpRequest.UseBinary = true;
  22. ftpRequest.UsePassive = true;
  23. ftpRequest.KeepAlive = true;
  24. /* Specify the Type of FTP Request */
  25. ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
  26. /* Establish Return Communication with the FTP Server */
  27. var ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  28. /* Get the FTP Server's Response Stream */
  29. var ftpStream = ftpResponse.GetResponseStream();
  30. /* Open a File Stream to Write the Downloaded File */
  31. FileStream localFileStream = new FileStream(localFile, FileMode.Create);
  32. /* Buffer for the Downloaded Data */
  33. byte[] byteBuffer = new byte[bufferSize];
  34. int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
  35. /* Download the File by Writing the Buffered Data Until the Transfer is Complete */
  36. try
  37. {
  38. while (bytesRead > 0)
  39. {
  40. localFileStream.Write(byteBuffer, 0, bytesRead);
  41. bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
  42. procressCallback?.Invoke(localFileStream.Length);
  43. }
  44. }
  45. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  46. /* Resource Cleanup */
  47. localFileStream.Close();
  48. ftpStream.Close();
  49. ftpResponse.Close();
  50. ftpRequest = null;
  51. }
  52. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  53. return;
  54. }
  55. /// <summary>
  56. /// get file list
  57. /// </summary>
  58. /// <param name="url"></param>
  59. /// <returns></returns>
  60. public static IList<string> GetFileList(string url, string user = "anonymous", string pass = null)
  61. {
  62. FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(url);
  63. ftpRequest.Credentials = new NetworkCredential(user, pass);
  64. ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  65. FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
  66. StreamReader streamReader = new StreamReader(response.GetResponseStream());
  67. List<string> directories = new List<string>();
  68. string line = streamReader.ReadLine();
  69. while (!string.IsNullOrEmpty(line))
  70. {
  71. directories.Add(line);
  72. line = streamReader.ReadLine();
  73. }
  74. streamReader.Close();
  75. return directories;
  76. }
  77. }
  78. }