title: How can I write a ssh2::Stream to a file without reading the data into memory?
body: <p>I am trying to download a large binary file via SCP using <a href="https://crates.io/crates/ssh2" rel="nofollow noreferrer"><code>ssh2</code></a> crate <code>v0.3.3</code>.</p>
  
  <pre class="lang-rust prettyprint-override"><code>extern crate ssh2;
  
  use std::net::TcpStream;
  use ssh2::Session;
  use std::path::Path;
  use std::fs::File;
  use indicatif::ProgressBar;
  
  fn main() -&gt; Result&lt;(), Box&lt;std::error::Error&gt;&gt; {
      let ssh_host_port = "prokerala.com:22";
      let ssh_user = "prokeral";
      let remote_temp_file = "/tmp/dbimport_Rk6Iwwm5.sql.bz2";
  
      let tcp = TcpStream::connect(&amp;ssh_host_port).unwrap();
      let mut sess = Session::new().unwrap();
      sess.handshake(&amp;tcp).expect("SSH handshake failed");
      let _ = sess.userauth_agent(ssh_user);
  
      let path = Path::new(&amp;remote_temp_file);
      let (mut remote_file, stat) = sess.scp_recv(path).unwrap();
  
      let stream:ssh2::Stream = remote_file.stream(1);
  
      // Update: solved by using io::copy as suggested by @apemanzilla below
      // let mut target = File::create("/tmp/done.txt").unwrap();
      // let pb = ProgressBar::new(stat.size());
      // std::io::copy(&amp;mut pb.wrap_read(remote_file), &amp;mut target)?;
      Ok(())
  }
  </code></pre>
  
  <p>I want to write <code>stream</code> to a file, and show a progress bar. Is it possible to write <code>stream</code> to a file directly, without reading the data into memory with <code>stream.read()</code> within a loop?</p>