#How to unzip

12 messages · Page 1 of 1 (latest)

burnt osprey
#

I was wondering how I could unzip things, do I have to use rust or is there another way? because I am very bad at rust

shell ridge
#

Read the file using readBinaryFile

#

Though for best performance you'll prob want to go with Rust

#

Especially if the zip file isn't tiny

#

Since the readBinaryFile API does force you to read the whole file into memory

unkempt monolith
#

use the zip_extract rust crate

#

i used that for my app.

#
#[tauri::command]
pub async fn unzip_archive(archive_path: String, target_dir: String) -> Result<String, String> {
  // The third parameter allows you to strip away toplevel directories.
  // If `archive` contained a single directory, its contents would be extracted instead.
  let _target_dir = std::path::PathBuf::from(target_dir); // Doesn't need to exist

  let archive = std::fs::read(&archive_path).expect("Failed to read archive");
  zip_extract::extract(std::io::Cursor::new(archive), &_target_dir, true)
    .expect("Failed to extract archive");

// erase the archive
// ! Using std:: is BAD practice, but it is the only way to get this to work for now
  std::fs::remove_file(archive_path).expect("Failed to remove archive");
  Ok(archive_path)
}
#

this is a basic implementation

#

then you can call it from JS using tauri's invoke api.

#

using tauri's fs api for reading the archive and using tauris path api for setting the target directory are much better options and i highly recommend that you do that instead. The above is just a working example i gave to get you up and running,