Missmatch between windows-cmd and std::process:Command

Hi all,

I am trying to unpack some files using the windows "tar" command.

When I execute the following command in a command-prompt, it works exactly as I'd expect:

tar -xzvf "I:/Downloads/test_dir/mlops.zip" --strip-components=1 -C "I:/Downloads/test_dir/mlops/"

However, when I run this through a "command" in Rust, it throws an error, complaining about the target directory not being accessible tar: could not chdir to ' I:/Downloads/test_dir/mlops/'.

This is the code I'm using to replicate the command:

let src_file = "I:/Downloads/test_dir/mlops.zip"';
let tgt_dir = "I:/Downloads/test_dir/mlops/";

let mut command = std::process::Command::new("tar");
command.args(
    vec![
        "-xzvf",
        &src_file,
        "--strip-components=1",
        &format!("-C {tgt_dir}/")
    ]
);

let result = command.output()?;

if !result.status.success() {
    println!("unpack failed:\n{:?}", std::str::from_utf8(&result.stderr)?);
}

the source-file and target-dir are actually converted from pathbufs, but I printed out the paths in the application and pasted them here instead for brevity.

Any ideas as to why process::Command might be executing things differently resulting in the error?

        &format!("-C {tgt_dir}/")`
                    ^

You're passing that as a single argument and it's parsing it like -CI:/Downloads/... but with a space before the I:.

tar: could not chdir to ' I:/Downloads/test_dir/mlops/'.
                         ^

Try passing it as two args:

        "-xzvf",
        src_file,
        "--strip-components=1",
        "-C",
        tgt_dir,

(Or removing the space: &format!("-C{tgt_dir}/"), but I don't see a reason to do that.)

2 Likes

It looks like that did the trick!
Thank you for the fast reply!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.