util.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. use flowy_test::Cleaner;
  2. use nanoid::nanoid;
  3. use std::fs::{create_dir_all, File};
  4. use std::io::copy;
  5. use std::path::{Path, PathBuf};
  6. use zip::ZipArchive;
  7. pub fn unzip_history_user_db(folder_name: &str) -> std::io::Result<(Cleaner, PathBuf)> {
  8. // Open the zip file
  9. let zip_file_path = format!(
  10. "./tests/user/migration_test/history_user_db/{}.zip",
  11. folder_name
  12. );
  13. let reader = File::open(zip_file_path)?;
  14. let output_folder_path = format!(
  15. "./tests/user/migration_test/history_user_db/unit_test_{}",
  16. nanoid!(6)
  17. );
  18. // Create a ZipArchive from the file
  19. let mut archive = ZipArchive::new(reader)?;
  20. // Iterate through each file in the zip
  21. for i in 0..archive.len() {
  22. let mut file = archive.by_index(i)?;
  23. let output_path = Path::new(&output_folder_path).join(file.mangled_name());
  24. if file.name().ends_with('/') {
  25. // Create directory
  26. create_dir_all(&output_path)?;
  27. } else {
  28. // Write file
  29. if let Some(p) = output_path.parent() {
  30. if !p.exists() {
  31. create_dir_all(p)?;
  32. }
  33. }
  34. let mut outfile = File::create(&output_path)?;
  35. copy(&mut file, &mut outfile)?;
  36. }
  37. }
  38. let path = format!("{}/{}", output_folder_path, folder_name);
  39. Ok((
  40. Cleaner::new(PathBuf::from(output_folder_path)),
  41. PathBuf::from(path),
  42. ))
  43. }