refactor(zip): remove redundant password byte conversion

Simplify password handling by removing platform-specific code and utilizing ByteSlice for all conversions.
This commit is contained in:
ttyS3 2024-09-06 15:54:37 +00:00 committed by João Marcos
parent 56f69e19a3
commit 84f1ec4daf
2 changed files with 5 additions and 11 deletions

View File

@ -10,7 +10,6 @@ use std::{
thread, thread,
}; };
use bstr::ByteSlice;
use filetime_creation::{set_file_mtime, FileTime}; use filetime_creation::{set_file_mtime, FileTime};
use fs_err as fs; use fs_err as fs;
use same_file::Handle; use same_file::Handle;
@ -45,7 +44,7 @@ where
for idx in 0..archive.len() { for idx in 0..archive.len() {
let mut file = match password { let mut file = match password {
Some(password) => archive Some(password) => archive
.by_index_decrypt(idx, password.as_bytes())? .by_index_decrypt(idx, password)?
.map_err(|_| zip::result::ZipError::UnsupportedArchive("Password required to decrypt file"))?, .map_err(|_| zip::result::ZipError::UnsupportedArchive("Password required to decrypt file"))?,
None => archive.by_index(idx)?, None => archive.by_index(idx)?,
}; };
@ -127,7 +126,7 @@ where
let file_in_archive = (|| { let file_in_archive = (|| {
let zip_result = match password.clone() { let zip_result = match password.clone() {
Some(password) => archive Some(password) => archive
.by_index_decrypt(idx, password.as_bytes())? .by_index_decrypt(idx, &password)?
.map_err(|_| zip::result::ZipError::UnsupportedArchive("Password required to decrypt file")), .map_err(|_| zip::result::ZipError::UnsupportedArchive("Password required to decrypt file")),
None => archive.by_index(idx), None => archive.by_index(idx),
}; };

View File

@ -7,12 +7,7 @@ mod list;
use std::{ops::ControlFlow, path::PathBuf}; use std::{ops::ControlFlow, path::PathBuf};
use rayon::prelude::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator}; use rayon::prelude::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator};
use utils::colors; use utils::colors;
use bstr::ByteSlice;
// OsStrExt for password as_bytes() conversion
#[cfg(unix)]
use std::os::unix::prelude::OsStrExt;
#[cfg(windows)]
use std::os::windows::prelude::OsStrExt;
use crate::{ use crate::{
check, check,
@ -193,7 +188,7 @@ pub fn run(
output_file_path, output_file_path,
question_policy, question_policy,
args.quiet, args.quiet,
args.password.as_deref().map(|str| str.as_bytes()), args.password.as_deref().map(|str| <[u8] as ByteSlice>::from_os_str(str).expect("convert password to bytes failed")),
) )
}) })
} }
@ -232,7 +227,7 @@ pub fn run(
formats, formats,
list_options, list_options,
question_policy, question_policy,
args.password.as_deref().map(|str|str.as_bytes()), args.password.as_deref().map(|str| <[u8] as ByteSlice>::from_os_str(str).expect("convert password to bytes failed")),
)?; )?;
} }