Advertisement
Eonirr

Untitled

Mar 9th, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. use std::error::Error;
  2. use std::fs::File;
  3. use std::{fs};
  4. use reqwest;
  5. use reqwest::Client;
  6. use futures_util::StreamExt;
  7. use zip;
  8. use std::env;
  9. use std::process::{Command, Stdio};
  10. use indicatif::{ProgressBar, ProgressStyle};
  11. use std::cmp::min;
  12. use std::io::Write;
  13. use std::path::{Path};
  14.  
  15. #[tokio::main]
  16. async fn main() -> Result<(), Box<dyn Error>>
  17. {
  18. let client = reqwest::ClientBuilder::new().build()?;
  19. let uri = "https://play.valkream.com/Launcher.zip";
  20. let path = "Launcher.zip";
  21.  
  22. println!("\n\tDownloading Launcher...\n");
  23. download_file(&client, uri, path).await?;
  24.  
  25. println!("\n\tExtracting Launcher...\n");
  26. match extract()
  27. {
  28. Ok(_) => {}
  29. Err(_) => {println!("Extraction error")}
  30. }
  31. Ok(())
  32. }
  33.  
  34. pub async fn download_file<T: reqwest::IntoUrl, U: AsRef<Path>>(client: &Client, url: T, path: U) -> Result<(), Box<dyn Error>>
  35. {
  36. let response = client.get(url).send().await?;
  37.  
  38. if let Some(total_size) = response.content_length()
  39. {
  40. let progress_bar = ProgressBar::new(total_size);
  41.  
  42. progress_bar.set_style(ProgressStyle::default_bar()
  43. .template("[{elapsed_precise}] [{bar:100.cyan/blue}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})")?
  44. .progress_chars("#>-"));
  45.  
  46. let mut file = File::create(path)?;
  47. let mut downloaded: u64 = 0;
  48. let mut result = response.bytes_stream();
  49. while let Some(item) = result.next().await
  50. {
  51. let i = &item?;
  52. file.write_all(i)?;
  53. let new = min(downloaded + (i.len() as u64), total_size);
  54. downloaded = new;
  55. progress_bar.set_position(new);
  56. }
  57. }
  58. Ok(())
  59. }
  60.  
  61. fn extract()-> Result<(), Box<dyn Error>>
  62. {
  63. let read_zip = File::open("Launcher.zip")?;
  64. let mut new_zip = zip::ZipArchive::new(read_zip);
  65. let path = env::current_dir()?;
  66.  
  67. if let Ok(mut e) = new_zip{
  68. zip::ZipArchive::extract(&mut e, path).expect("");
  69. };
  70.  
  71. fs::remove_file("Launcher.zip")?;
  72.  
  73. let _ = Command::new("./Valkream Launcher.exe").stdout(Stdio::piped()).spawn()?;
  74.  
  75. Ok(())
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement