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,
};
use bstr::ByteSlice;
use filetime_creation::{set_file_mtime, FileTime};
use fs_err as fs;
use same_file::Handle;
@ -45,7 +44,7 @@ where
for idx in 0..archive.len() {
let mut file = match password {
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"))?,
None => archive.by_index(idx)?,
};
@ -127,7 +126,7 @@ where
let file_in_archive = (|| {
let zip_result = match password.clone() {
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")),
None => archive.by_index(idx),
};

View File

@ -7,12 +7,7 @@ mod list;
use std::{ops::ControlFlow, path::PathBuf};
use rayon::prelude::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator};
use utils::colors;
// OsStrExt for password as_bytes() conversion
#[cfg(unix)]
use std::os::unix::prelude::OsStrExt;
#[cfg(windows)]
use std::os::windows::prelude::OsStrExt;
use bstr::ByteSlice;
use crate::{
check,
@ -193,7 +188,7 @@ pub fn run(
output_file_path,
question_policy,
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,
list_options,
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")),
)?;
}