损坏的 .gif 下载文件使用 rust 但没有 rust 使用 curl 命令它可以工作

Corrupted .gif download file using rust but without rust using curl command it works

您好,我尝试使用
在 Linux fedora 36 中使用 curl 命令下载 .gif 文件 curl https://static.wikia.nocookie.net/minecraft_gamepedia/images/d/d1/Magma_Block_JE2_BE2.gif/revision/latest?cb=20200915183320 --output magma.gif
有效

但后来我尝试用这个函数和 curl 库在 Rust 中做同样的事情

pub fn download_file(url: &str, path: String) -> Result<(), Box<dyn std::error::Error>> {
    let mut curl = Easy::new();
    curl.http_content_decoding(true)?;
    curl.get(true)?;
    curl.verbose(true)?;
    curl.connect_timeout(Duration::from_secs(3))?;
    curl.dns_cache_timeout(Duration::from_secs(3))?;
    curl.url(url)?;
    curl.write_function(move |data| {
        if fs::write(&path, data).is_err() {
            eprint!(
                "{}: Failed to write to file -> check permission",
                "error".red().bold()
            );
            process::exit(-1);
        }
        Ok(data.len())
    })?;
    curl.perform()?;
    Ok(())
}

对于我使用的参数 https://static.wikia.nocookie.net/minecraft_gamepedia/images/d/d1/Magma_Block_JE2_BE2.gif/revision/latest?cb=20200915183320 和 magma.gif 但是 不起作用 它下载了一些东西但是它的大小比应该的小得多是,它 已损坏 ,我无法打开它

完整来源:https://github.com/NiiightmareXD/minecraft_block_downloader
工作文件:https://i.stack.imgur.com/zpClR.gif
损坏的文件:https://www.mediafire.com/file/amlj2vrd4thtkb8/magma.gif/file
工作详细:https://www.mediafire.com/file/hsb4k8jmam5bmoh/working_verbose.txt/file
损坏的详细信息:https://www.mediafire.com/file/ouu0h88f2dcgiu9/corrupted_verbose.txt/file
感谢阅读

问题是写入函数将以前的数据覆盖到文件中,因此它被损坏了,所以新代码是这样的,它可以工作

pub fn download_file(url: &str, path: String) -> Result<(), Box<dyn std::error::Error>> {
    let mut curl = Easy::new();
    curl.url(url)?;
    File::create(&path)?;
    let mut file = File::options().write(true).append(true).open(&path)?;
    curl.write_function(move |data| {
        if let Err(e) = file.write_all(data) {
            eprint!("{}: {}", "error".red().bold(), &e);
            process::exit(-1);
        }
        Ok(data.len())
    })?;
    curl.perform()?;
    Ok(())
}