1

如何实现actix-web响应发送文件数据流,我想通过解压zip文件发送到http响应体

use zip;
use std::fs::File;
use std::io::prelude::*;
use encoding_rs::Encoder;
use std::str;
use actix_web::{web, App, HttpRequest, HttpServer, Responder, HttpResponse};
use urlencoding::{decode};
use std::sync::Mutex;
use futures::stream::{Stream};

struct MyData {
    zip: zip::ZipArchive<File>,
}

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
    let paz_file = File::open("D:/test.zip").unwrap();
    let mut paz = zip::ZipArchive::new(paz_file).unwrap(); 
    let data = web::Data::new(Mutex::new(MyData { zip: paz }));
    HttpServer::new(move || {
        App::new()
        .app_data(data.clone())
        .route("/static/**", web::get().to(handle_res))
    })
    .bind("127.0.0.1:21607")?
    .run()
    .await?;
    Ok(())
}

async fn handle_res(data: web::Data<Mutex<MyData>>, req: HttpRequest) -> impl Responder {
    let mut data = data.lock().unwrap();
    let paths = req.path().to_string();
    let mut file_path = paths.replace("/static/", "");
    file_path = decode(&file_path).unwrap();
    {
        let mut zip_file = data.zip.by_name(&file_path);
        match zip_file {
            Ok(mut f) => {
                return HttpResponse::Ok()
                //.header("content-type", "text/plain;charset=utf-8")
                .streaming(f.bytes());
            },
            Err(e) => {
                println!("{}", e.to_string());
                return HttpResponse::NotFound().body("File Not Found");
            }
        }
    }
}
Error: 
the trait bound `std::io::Bytes<zip::read::ZipFile<'_>>: futures_core::stream::Stream` is not satisfied
      .streaming(f.bytes());
    | ^^^^^^^^^^ the trait `futures_core::stream::Stream` is not implemented for `std::io::Bytes<zip::read::ZipFile<'_>>`
4

0 回答 0