流从SharpSSH文件 [英] Streaming a file from SharpSSH

查看:575
本文介绍了流从SharpSSH文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用SharpSSH来获得远程SFTP服务器的文件,我想读它作为一个数据流。

I'm trying to use SharpSSH to get a file from a remote SFTP server, and I want to read it out as a stream.

我发现:

  • SFTP ,其中有一个获取方法将其保存到本地文件 - 关闭

  • class Sftp, which has a Get method that saves it to a local file -- close

SshStream ,这可能会做什么样的我想要的,但似乎不相交从 SFTP 等等我可能要实现SFTP部分我自己(?)

class SshStream, which might do kind of what I want, but seems disjoint from Sftp so I might have to implement the SFTP part myself (??)

ChannelSftp ,它实现SFTP的方法,如获取(字符串的OutputStream),这似乎是完美的,但它是一个低级别的类,它不是在所有明显对我怎么样,甚至将它实例

class ChannelSftp, which implements SFTP methods like get(String, OutputStream), which seems perfect, except it's a low-level class and it's not at all obvious to me how to even instantiate it

看起来,如果 SFTP ChannelSftp SftpChannel 财产是不是私人的,我可以用它和一切将是完美的。我想,以避免黑客攻击了SharpSSH如果可能的话,虽然。

It looks like if Sftp's ChannelSftp SftpChannel property wasn't private, I could use that and everything would be perfect. I'd like to avoid hacking up SharpSSH if possible, though.

我缺少的东西?

推荐答案

我的工作出来的东西,并测试了它。给它一个尝试,并随时按摩API。

I worked something out, and tested it out. Give it a try, and feel free to massage the API.

首先,您需要浮出水面了一种方法,可以让你充分利用 ChannelSftp 方法要求的OutputStream 的S代替目标文件名。如果你不想使用反射来做到这一点,则此方法添加到SFTP类,并重新编译SharpSSH。

First off, you will need to surface up a method that allows you to take advantage of the ChannelSftp methods that call for OutputStreams instead of destination file names. If you don't want to use reflection to do it, then add this method to the Sftp class and recompile SharpSSH.

public void GetWithStream(string fromFilePath, Tamir.SharpSsh.java.io.OutputStream stream)
{
    cancelled = false;
    SftpChannel.get(fromFilePath, stream, m_monitor);
}

接下来,创建一个包装的类,它是与 Tamir.SharpSsh.java.io.OutputStream 如下面的一种:

Next, create a wrapper for the Stream class that is compatible with Tamir.SharpSsh.java.io.OutputStream, such as the one below:

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

With those ingredients, you can now use OpenSSH to stream its data to the stream of your choice, as demonstrated below with a FileStream.

使用System.IO; 使用Tamir.SharpSsh;

using System.IO; using 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();
    }
}

这篇关于流从SharpSSH文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆