public void Download(string filename, Stream destination)
{
if (filename.IsNullOrWhiteSpace())
throw new ArgumentException("filename");
if (destination == null)
throw new ArgumentNullException("destination");
using (var input = ServiceFactory.CreatePipeStream())
using (var channel = Session.CreateChannelSession())
{
channel.DataReceived += (sender, e) => input.Write(e.Data, 0, e.Data.Length);
channel.Open();
// Send channel command request
channel.SendExecRequest(string.Format("scp -f \"{0}\"", filename));
SendConfirmation(channel); // Send reply
var message = ReadString(input);
var match = FileInfoRe.Match(message);
if (match.Success)
{
// Read file
SendConfirmation(channel); // Send reply
var mode = match.Result("${mode}");
var length = long.Parse(match.Result("${length}"));
var fileName = match.Result("${filename}");
InternalDownload(channel, input, destination, fileName, length);
}
else
{
SendConfirmation(channel, 1, string.Format("\"{0}\" is not valid protocol message.", message));
}
}
}