我解决了一些问题,并对其进行了测试。试一试,随意按摩 API。
首先,您需要提出一种方法,该方法允许您利用ChannelSftp
调用OutputStream
s 而不是目标文件名的方法。如果你不想使用反射来做,那么将这个方法添加到 Sftp 类并重新编译 SharpSSH。
public void GetWithStream(string fromFilePath, Tamir.SharpSsh.java.io.OutputStream stream)
{
cancelled = false;
SftpChannel.get(fromFilePath, stream, m_monitor);
}
Stream
接下来,为与兼容的类创建一个包装器,Tamir.SharpSsh.java.io.OutputStream
如下所示:
using System.IO;
using Tamir.SharpSsh.java.io;
public class GenericSftpOutputStream : OutputStream
{
Stream stream;
public GenericSftpOutputStream(Stream stream)
{
this.stream = stream;
}
public override void Write(byte[] buffer, int offset, int count)
{
stream.Write(buffer, offset, count);
}
public override void Flush()
{
stream.Flush();
}
public override void Close()
{
stream.Close();
}
public override bool CanSeek
{
get { return stream.CanSeek; }
}
public override long Seek(long offset, SeekOrigin origin)
{
return stream.Seek(offset, origin);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (this.stream != null)
{
this.stream.Dispose();
this.stream = null;
}
}
}
有了这些成分,您现在可以使用 OpenSSH 将其数据流式传输到您选择的流中,如下所示,使用FileStream
.
使用 System.IO;使用 Tamir.SharpSsh;
class Program
{
static void Main(string[] args)
{
var host = "hostname";
var user = "username";
var pass = "password";
var file = "/some/remote/path.txt";
var saveas = @"C:\some\local\path";
var client = new Sftp(host, user, pass);
client.Connect();
using (var target = new GenericSftpOutputStream(File.Open(saveas, FileMode.OpenOrCreate)))
{
client.GetWithStream(file, target);
}
client.Close();
}
}