rust-ftp
rust-ftp copied to clipboard
Large download example
Would be nice to have an example of how to properly download/upload a large file, avoiding the control stream timeout (null command)... I'm relatively new to rust and just trying to get a grasp of this.
let cursor = ftp.simple_retr("84402.ISO").unwrap();
let mut bin = fs::File::create("84402.ISO").unwrap();
let wr = bin.write(&cursor.into_inner()).unwrap();
println!("{}", wr);
or
ftp.retr("84402.ISO", |stream| {
let mut buf = Vec::new();
// stream.read_to_end(&mut buf).map_err(|e| FtpError::ConnectionError(e))
let ret = stream.read(&mut buf);
match ret {
Ok(_sz) => {
println!("{}", buf.len());
if _sz > 0 {
let mut iso = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open("84402.ISO").unwrap();
iso.write(&buf).map_err(|e| FtpError::ConnectionError(e))
} else {
Ok(0)
}
},
Err(_e) => {
Err(FtpError::ConnectionError(_e))
}
}
}).unwrap();
But, they all take large memory. the bigger file the larger memory
Thanks to @veeso std::io::copy
ftp.retr("84402.ISO", |stream| {
let mut iso = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open("84402.ISO").unwrap();
std::io::copy(stream, &mut iso).map_err(|e| FtpError::ConnectionError(e))
}).unwrap();