'Resume file transfer using SSH.NET [duplicate]
Is there a built-in way to resume interrupted file transfers using SSH.NET? I've found such an option in the WinSCP .NET API, but can't find it in SSH.NET.
Solution 1:[1]
SftpFileStream looks like it supports FileMode.Append. I am experimenting with this approach to resume a file transfer:
private static void WriteOrResumeRemoteFile(SftpClient remoteHost, string remoteFileName, FileStream localFileStream)
{
using (SftpFileStream remoteStream = remoteHost.Open(remoteFileName, FileMode.Append, FileAccess.Write))
{
byte[] buffer = new byte[409600];
int bytesRead;
if (remoteStream.Length > 0)
{
localFileStream.Seek(remoteStream.Length, SeekOrigin.Begin);
}
bytesRead = 1;
while (bytesRead > 0)
{
DateTime timeBeforeRead = DateTime.Now;
bytesRead = localFileStream.Read(buffer, 0, 409600);
if (bytesRead > 0)
{
remoteStream.Write(buffer, 0, bytesRead);
long estimatedMsRemain = Convert.ToInt64(Convert.ToDouble(localFileStream.Length - localFileStream.Position) * Convert.ToDouble((DateTime.Now - timeBeforeRead).Ticks) / Convert.ToDouble(bytesRead));
Debug.WriteLine(localFileStream.Position + " total bytes written (" + (Convert.ToDouble(localFileStream.Position) / Convert.ToDouble(localFileStream.Length)).ToString("p") +
", ETR: " + (new TimeSpan(estimatedMsRemain)).ToString() + ")");
}
}
remoteStream.Close();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ??? |
