Skip to content

Instantly share code, notes, and snippets.

@nmoutschen
Created February 2, 2022 13:56
Show Gist options
  • Save nmoutschen/c9b977854b51ab594cc2cf8425318a6b to your computer and use it in GitHub Desktop.
Save nmoutschen/c9b977854b51ab594cc2cf8425318a6b to your computer and use it in GitHub Desktop.
Download and decode a gzipped file on the fly
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