123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Text;
- namespace vCloud.GeneratePackages.Tool
- {
- class FtpHelper
- {
- private const int bufferSize = 8192;
-
-
- public static void Get(string remoteFile, string localFile, Action<long> procressCallback = null, string user = "anonymous", string pass = null)
- {
- try
- {
-
- var ftpRequest = (FtpWebRequest)FtpWebRequest.Create(remoteFile);
-
- ftpRequest.Credentials = new NetworkCredential(user, pass);
-
- ftpRequest.UseBinary = true;
- ftpRequest.UsePassive = true;
- ftpRequest.KeepAlive = true;
-
- ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
-
- var ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
-
- var ftpStream = ftpResponse.GetResponseStream();
-
- FileStream localFileStream = new FileStream(localFile, FileMode.Create);
-
- byte[] byteBuffer = new byte[bufferSize];
- int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
-
- try
- {
- while (bytesRead > 0)
- {
- localFileStream.Write(byteBuffer, 0, bytesRead);
- bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
- procressCallback?.Invoke(localFileStream.Length);
- }
- }
- catch (Exception ex) { Console.WriteLine(ex.ToString()); }
-
- localFileStream.Close();
- ftpStream.Close();
- ftpResponse.Close();
- ftpRequest = null;
- }
- catch (Exception ex) { Console.WriteLine(ex.ToString()); }
- return;
- }
-
-
-
-
-
- public static IList<string> GetFileList(string url, string user = "anonymous", string pass = null)
- {
- FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(url);
- ftpRequest.Credentials = new NetworkCredential(user, pass);
- ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
- FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
- StreamReader streamReader = new StreamReader(response.GetResponseStream());
- List<string> directories = new List<string>();
- string line = streamReader.ReadLine();
- while (!string.IsNullOrEmpty(line))
- {
- directories.Add(line);
- line = streamReader.ReadLine();
- }
- streamReader.Close();
- return directories;
- }
- }
- }
|