Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Large download example #111

Open
tracker1 opened this issue Nov 1, 2022 · 2 comments
Open

Large download example #111

tracker1 opened this issue Nov 1, 2022 · 2 comments

Comments

@tracker1
Copy link

tracker1 commented Nov 1, 2022

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.

@thewon86
Copy link

    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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants