본문 바로가기
C#

FTP파일 이어받기(FTPClient)

by 리틀홍콩 2017. 12. 27.
728x90
  1. using System;
  2. using System.IO;
  3. using System.Net.FtpClient;
  4.  
  5. namespace CR.Common {
  6.     public static class FtpClientExtensions {
  7.         public static void Download(this FtpClient ftp, string remoteFilePath, string localFilePath) {
  8.             using (var fs = new FileStream(filePath: localFilePath, fileMode: FileMode.Create, fileAccess: FileAccess.Write, fileShare:FileShare.None)) {
  9.                 // istream.Position is incremented accordingly to the reads you perform
  10.                 // istream.Length == file size if the server supports getting the file size
  11.                 // also note that file size for the same file can vary between ASCII and Binary
  12.                 // modes and some servers won't even give a file size for ASCII files! It is
  13.                 // recommended that you stick with Binary and worry about character encodings
  14.                 // on your end of the connection.
  15.                 var buffer = new byte[8192];
  16.                 var offset = 0;
  17.                 var remoteStream = ftp.OpenRead(remoteFilePath);
  18.                 try {
  19.                     while (offset < remoteStream.Length) {
  20.                         try {
  21.                             int len;
  22.                             while ((len = remoteStream.Read(buffer, 0, buffer.Length)) > 0) {
  23.                                 fs.Write(buffer, 0, len);
  24.                                 offset += len;
  25.                             }
  26.                         }
  27.                         catch (IOException ex) {
  28.                             if (ex.InnerException != null) {
  29.                                 var iex = ex.InnerException as System.Net.Sockets.SocketException;
  30.                                 if (iex != null && iex.ErrorCode == 10054) {
  31.                                     remoteStream.Close();
  32.                                     remoteStream = ftp.OpenRead(remoteFilePath, restart: offset);
  33.                                 }
  34.                                 else throw;
  35.                             }
  36.                             else throw;
  37.                         }
  38.                     }
  39.                 }
  40.                 finally {
  41.                     remoteStream.Close();
  42.                 }
  43.             }
  44.         }
  45.  
  46.         public static void Upload(this FtpClient ftp, string localFilePath, string remoteFilePath) {
  47.             using (var fs = new FileStream(filePath: localFilePath, fileMode: FileMode.Open, fileAccess: FileAccess.Read, fileShare:FileShare.Read)) {
  48.                 Upload(ftp: ftp, stream: fs, remoteFilePath: remoteFilePath);
  49.             }
  50.         }
  51.  
  52.         public static void Upload(this FtpClient ftp, Stream stream, string remoteFilePath) {
  53.             var buffer = new byte[8192];
  54.             var offset = 0;
  55.             var remoteStream = ftp.OpenWrite(remoteFilePath);
  56.             try {
  57.                 while (offset < stream.Length) {
  58.                     try {
  59.                         int len;
  60.                         while ((len = stream.Read(buffer, 0, buffer.Length)) > 0) {
  61.                             remoteStream.Write(buffer, 0, len);
  62.                             offset += len;
  63.                         }
  64.                     }
  65.                     catch (IOException ex) {
  66.                         if (ex.InnerException != null) {
  67.                             var iex = ex.InnerException as System.Net.Sockets.SocketException;
  68.                             if (iex != null && iex.ErrorCode == 10054) {
  69.                                 remoteStream.Close();
  70.                                 remoteStream = ftp.OpenAppend(remoteFilePath);
  71.                                 remoteStream.Position = offset;
  72.                             }
  73.                             else throw;
  74.                         }
  75.                         else throw;
  76.                     }
  77.                 }
  78.             }
  79.             finally {
  80.                 remoteStream.Close();
  81.             }
  82.         }
  83.     }
  84. }



출처 : https://pastebin.com/FiKMrH76





'C#' 카테고리의 다른 글

renci.sshnet PrivateKeyFile .ppk 파일읽기 오류  (0) 2023.01.27
FTPClient Passive/Active  (0) 2017.01.23

댓글