1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How can I download a file in parts using a fixed-size array?

Discussion in 'Programming/Internet' started by k0shinus, Sep 12, 2018.

  1. k0shinus

    k0shinus Guest

    I want to upload a file to a URL. The code below works fine, but when I replace Vec with [u8; 4 * 1024] and read_to_end with read within the loop, I get infinite reading of the same information. How should I change my code to get the file read in parts using a fixed-size array?

    use std::env;
    use std::fs::{File, OpenOptions};
    use std::io::{BufRead, BufReader, BufWriter, Read, Result, Write};

    //reqwest = "0.8.0"
    extern crate reqwest;

    fn main() -> Result<()> {
    // All URL's are stored in file passed as a command line argument
    let args: Vec<String> = env::args().collect();
    let fin = File::eek:pen(&args[1])?;

    let buf: &mut Vec<u8> = &mut Vec::new();
    let mut readed: usize;
    for line in BufReader::new(fin).lines() {
    let unwrapped = &line.unwrap();
    let mut options = OpenOptions::new();

    // Create an output file with a name equal to the last domain
    let fout = options
    .open(&unwrapped[unwrapped.rfind('/').unwrap() + 1..])?;
    let mut writer = BufWriter::new(&fout);

    readed = reqwest::get(unwrapped).unwrap().read_to_end(buf).unwrap();
    println!("{}", readed); // Just to know how many read

    // Close the file and clear "buf" to ensure that
    // the subsequent reading is correct

    Login To add answer/comment

Share This Page