Rust 如何使用streams解压Reqwest/Hyper响应?

Rust 如何使用streams解压Reqwest/Hyper响应?,rust,reqwest,Rust,Reqwest,我需要下载一个60MB的ZIP文件并解压缩其中唯一的文件。我想下载它,并提取它使用流。我如何使用锈迹来实现这一点 fn main () { let mut res = reqwest::get("myfile.zip").unwrap(); // extract the response body to myfile.txt } 在Node.js中,我将执行以下操作: http.get('myfile.zip', response => { response.pipe

我需要下载一个60MB的ZIP文件并解压缩其中唯一的文件。我想下载它,并提取它使用流。我如何使用锈迹来实现这一点

fn main () {
    let mut res = reqwest::get("myfile.zip").unwrap();
    // extract the response body to myfile.txt
}
在Node.js中,我将执行以下操作:

http.get('myfile.zip', response => {
  response.pipe(unzip.Parse())
  .on('entry', entry => {
    if (entry.path.endsWith('.txt')) {
      entry.pipe(fs.createWriteStream('myfile.txt'))
    }
  })
})

这就是我如何从位于本地服务器上的存档hello.zip中读取内容为
hello world
的hello.txt文件的方法:

extern crate reqwest;
extern crate zip;

use std::io::Read;

fn main() {
    let mut res = reqwest::get("http://localhost:8000/hello.zip").unwrap();

    let mut buf: Vec<u8> = Vec::new();
    let _ = res.read_to_end(&mut buf);

    let reader = std::io::Cursor::new(buf);
    let mut zip = zip::ZipArchive::new(reader).unwrap();

    let mut file_zip = zip.by_name("hello.txt").unwrap();
    let mut file_buf: Vec<u8> = Vec::new();
    let _ = file_zip.read_to_end(&mut file_buf);

    let content = String::from_utf8(file_buf).unwrap();

    println!("{}", content);
}
外部板条箱要求;
外部板条箱拉链;
使用std::io::Read;
fn main(){
让mut res=reqwest::get(“http://localhost:8000/hello.zip)展开();
让mut buf:Vec=Vec::new();
让u=res.read_至u结束(&mut buf);
让reader=std::io::Cursor::new(buf);
让mut-zip=zip::ZipArchive::new(reader).unwrap();
让mut file_zip=zip.by_name(“hello.txt”).unwrap();
让mut file_buf:Vec=Vec::new();
让u=file\u zip.read\u结束(&mut file\u buf);
让content=String::from_utf8(file_buf).unwrap();
println!(“{}”,内容);
}
这将输出
hello world

,您可以获得
.zip
文件:

reqwest::get("myfile.zip")
由于
reqwest
只能用于检索文件,因此从板条箱中取出的文件可用于解包。无法将
.zip
文件流式传输到
ZipArchive
,因为需要
R
来实现(由
reqwest
实现),而不是由
响应实现

作为解决方法,您可以使用临时文件:

copy_to(&mut tmpfile)
As实现了
Seek
Read
,可在此处使用:

zip::ZipArchive::new(tmpfile)
这是所述方法的工作示例:

extern crate reqwest;
extern crate tempfile;
extern crate zip;

use std::io::Read;

fn main() {
    let mut tmpfile = tempfile::tempfile().unwrap();
    reqwest::get("myfile.zip").unwrap().copy_to(&mut tmpfile);
    let mut zip = zip::ZipArchive::new(tmpfile).unwrap();
    println!("{:#?}", zip);
}

是一个方便的板条箱,它可以让您创建一个临时文件,这样您就不必想名字了。

您看过板条箱的拉链了吗?是的,但我刚刚开始使用rust和reqwest+zip示例,这真的很有用。您是否考虑过接受这个问题的答案或开始悬赏?这个示例是否将整个文件读入缓冲区?请记住,这是一个大文件。您可以使用
read
和固定大小的缓冲区逐块读取。有关阅读的更多信息,请参见本页: