Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::error::Error;
- use std::fs::File;
- use std::{fs};
- use reqwest;
- use reqwest::Client;
- use futures_util::StreamExt;
- use zip;
- use std::env;
- use std::process::{Command, Stdio};
- use indicatif::{ProgressBar, ProgressStyle};
- use std::cmp::min;
- use std::io::Write;
- use std::path::{Path};
- #[tokio::main]
- async fn main() -> Result<(), Box<dyn Error>>
- {
- let client = reqwest::ClientBuilder::new().build()?;
- let uri = "https://play.valkream.com/Launcher.zip";
- let path = "Launcher.zip";
- println!("\n\tDownloading Launcher...\n");
- download_file(&client, uri, path).await?;
- println!("\n\tExtracting Launcher...\n");
- match extract()
- {
- Ok(_) => {}
- Err(_) => {println!("Extraction error")}
- }
- Ok(())
- }
- pub async fn download_file<T: reqwest::IntoUrl, U: AsRef<Path>>(client: &Client, url: T, path: U) -> Result<(), Box<dyn Error>>
- {
- let response = client.get(url).send().await?;
- if let Some(total_size) = response.content_length()
- {
- let progress_bar = ProgressBar::new(total_size);
- progress_bar.set_style(ProgressStyle::default_bar()
- .template("[{elapsed_precise}] [{bar:100.cyan/blue}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})")?
- .progress_chars("#>-"));
- let mut file = File::create(path)?;
- let mut downloaded: u64 = 0;
- let mut result = response.bytes_stream();
- while let Some(item) = result.next().await
- {
- let i = &item?;
- file.write_all(i)?;
- let new = min(downloaded + (i.len() as u64), total_size);
- downloaded = new;
- progress_bar.set_position(new);
- }
- }
- Ok(())
- }
- fn extract()-> Result<(), Box<dyn Error>>
- {
- let read_zip = File::open("Launcher.zip")?;
- let mut new_zip = zip::ZipArchive::new(read_zip);
- let path = env::current_dir()?;
- if let Ok(mut e) = new_zip{
- zip::ZipArchive::extract(&mut e, path).expect("");
- };
- fs::remove_file("Launcher.zip")?;
- let _ = Command::new("./Valkream Launcher.exe").stdout(Stdio::piped()).spawn()?;
- Ok(())
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement