(
message: &str,
zip_file_path: &Path,
extract_dir: &Path,
skip: usize,
result_tx: &mpsc::Sender<Message>,
)
| 259 | } |
| 260 | |
| 261 | pub async fn unzip( |
| 262 | message: &str, |
| 263 | zip_file_path: &Path, |
| 264 | extract_dir: &Path, |
| 265 | skip: usize, |
| 266 | result_tx: &mpsc::Sender<Message>, |
| 267 | ) -> Result<()> { |
| 268 | info!("Unzipping {:?} to {:?}", zip_file_path, extract_dir); |
| 269 | let file = File::open(zip_file_path)?; |
| 270 | let mut archive = ZipArchive::new(file)?; |
| 271 | |
| 272 | std::fs::create_dir_all(extract_dir) |
| 273 | .context(format!("Failed to create dir '{:?}'", extract_dir))?; |
| 274 | |
| 275 | let template = crate::t!("progress-files-eta"); |
| 276 | |
| 277 | let mut progress = ProgressInfo { |
| 278 | message: message.to_string(), |
| 279 | template, |
| 280 | total: archive.len() as u32, |
| 281 | current: 0, |
| 282 | ..Default::default() |
| 283 | }; |
| 284 | |
| 285 | result_tx |
| 286 | .send(Message::Progress(progress.clone())) |
| 287 | .await |
| 288 | .ok(); |
| 289 | |
| 290 | for i in 0..archive.len() { |
| 291 | let mut file = archive |
| 292 | .by_index(i) |
| 293 | .context("Failed to get file from archive")?; |
| 294 | let file_name = Path::new(file.name()) |
| 295 | .components() |
| 296 | .skip(skip) |
| 297 | .collect::<PathBuf>(); |
| 298 | let target_path = Path::new(extract_dir).join(file_name); |
| 299 | if target_path == extract_dir { |
| 300 | continue; |
| 301 | } |
| 302 | info!("Extracting {:?}", target_path); |
| 303 | if file.is_dir() { |
| 304 | std::fs::create_dir_all(target_path.clone()) |
| 305 | .context(format!("unzip failed to create dir '{:?}'", target_path))?; |
| 306 | } else { |
| 307 | let mut output_file = File::create(target_path.clone()) |
| 308 | .context(format!("unzip failed to create file '{:?}'", target_path))?; |
| 309 | io::copy(&mut file, &mut output_file).context("unzip failed to copy file")?; |
| 310 | } |
| 311 | |
| 312 | progress.current = (i + 1) as u32; |
| 313 | if progress.current.is_multiple_of(100) { |
| 314 | result_tx |
| 315 | .send(Message::Progress(progress.clone())) |
| 316 | .await |
| 317 | .ok(); |
| 318 | } |
no test coverage detected