rust-ftp icon indicating copy to clipboard operation
rust-ftp copied to clipboard

Large download example

Open tracker1 opened this issue 3 years ago • 2 comments

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.

tracker1 avatar Nov 01 '22 11:11 tracker1

    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

thewon86 avatar Jun 26 '23 01:06 thewon86

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();

thewon86 avatar Jun 26 '23 07:06 thewon86