-
-
Save nmoutschen/c9b977854b51ab594cc2cf8425318a6b to your computer and use it in GitHub Desktop.
Download and decode a gzipped file on the fly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use tokio::io::AsyncReadExt; | |
use tokio_stream::StreamExt; | |
use tokio_util::io::StreamReader; | |
use hyper::Client; | |
use hyper_tls::HttpsConnector; | |
const LINK: &str = "https://datasets.imdbws.com/title.basics.tsv.gz"; | |
#[tokio::main] | |
async fn main() -> Result<(), Box<dyn std::error::Error>> { | |
let https = HttpsConnector::new(); | |
let client = Client::builder().build::<_, hyper::Body>(https); | |
let res = client.get(LINK.parse()?).await?; | |
let body = res | |
.into_body() | |
.map(|result| result.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))); | |
let body = StreamReader::new(body); | |
let mut decoder = async_compression::tokio::bufread::GzipDecoder::new(body); | |
let mut buffer = [0; 1024]; | |
loop { | |
let len = decoder.read(&mut buffer).await?; | |
if len == 0 { | |
break; | |
} | |
println!("{}", String::from_utf8_lossy(&buffer[..len])); | |
} | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment