mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-06 11:35:45 +00:00
zip: ensure usage of relational paths during compression
This commit is contained in:
parent
3687e7255d
commit
8e37078043
@ -6,6 +6,7 @@ use walkdir::WalkDir;
|
||||
|
||||
use super::compressor::Entry;
|
||||
use crate::{compressors::Compressor, file::File};
|
||||
use crate::utils;
|
||||
|
||||
pub struct TarCompressor {}
|
||||
|
||||
@ -20,19 +21,12 @@ impl TarCompressor {
|
||||
}
|
||||
|
||||
fn make_archive_from_files(input_filenames: Vec<PathBuf>) -> crate::Result<Vec<u8>> {
|
||||
let change_dir_and_return_parent = |filename: &PathBuf| -> crate::Result<PathBuf> {
|
||||
let previous_location = env::current_dir()?;
|
||||
let parent = filename.parent().unwrap();
|
||||
env::set_current_dir(parent)?;
|
||||
Ok(previous_location)
|
||||
};
|
||||
|
||||
|
||||
let buf = Vec::new();
|
||||
let mut b = Builder::new(buf);
|
||||
|
||||
for filename in input_filenames {
|
||||
let previous_location = change_dir_and_return_parent(&filename)?;
|
||||
// Safe unwrap since this filename came from `fs::canonicalize`.
|
||||
let previous_location = utils::change_dir_and_return_parent(&filename)?;
|
||||
let filename = filename.file_name().unwrap();
|
||||
for entry in WalkDir::new(&filename) {
|
||||
let entry = entry?;
|
||||
|
@ -6,7 +6,7 @@ use std::{
|
||||
use walkdir::WalkDir;
|
||||
|
||||
use super::compressor::Entry;
|
||||
use crate::{compressors::Compressor, file::File};
|
||||
use crate::{compressors::Compressor, file::File, utils};
|
||||
|
||||
pub struct ZipCompressor {}
|
||||
|
||||
@ -55,6 +55,10 @@ impl ZipCompressor {
|
||||
zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Deflated);
|
||||
|
||||
for filename in input_filenames {
|
||||
|
||||
let previous_location = utils::change_dir_and_return_parent(&filename)?;
|
||||
let filename = filename.file_name().unwrap();
|
||||
|
||||
for entry in WalkDir::new(filename) {
|
||||
let entry = entry?;
|
||||
let entry_path = &entry.path();
|
||||
@ -65,6 +69,8 @@ impl ZipCompressor {
|
||||
let file_bytes = std::fs::read(entry.path())?;
|
||||
writer.write_all(&*file_bytes)?;
|
||||
}
|
||||
|
||||
std::env::set_current_dir(previous_location)?;
|
||||
}
|
||||
|
||||
let bytes = writer.finish().unwrap();
|
||||
|
12
src/utils.rs
12
src/utils.rs
@ -1,7 +1,4 @@
|
||||
use std::{
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use std::{env, fs, path::{Path, PathBuf}};
|
||||
|
||||
use colored::Colorize;
|
||||
|
||||
@ -63,3 +60,10 @@ pub(crate) fn get_destination_path(dest: &Option<File>) -> &Path {
|
||||
None => Path::new("."),
|
||||
}
|
||||
}
|
||||
|
||||
pub (crate) fn change_dir_and_return_parent(filename: &PathBuf) -> crate::Result<PathBuf> {
|
||||
let previous_location = env::current_dir()?;
|
||||
let parent = filename.parent().unwrap();
|
||||
env::set_current_dir(parent)?;
|
||||
Ok(previous_location)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user