英文:
sevenz_rust::decompress_file fails with "No such file or directory"
问题
I'm encountering an error and wanted to make sure I'm understanding how the sevenz_rust::decompress_file
function should be used. This is my first rust program, so there may be some anti-patterns or obvious mistakes in this code.
Here is how the package documentation describes how decompress_file
should be used:
Decompress source file "data/sample.7z" to dest path "data/sample"
sevenz_rust::decompress_file("data/sample.7z", "data/sample").expect("complete");
Right now I'm trying to decompress a file from a folder data
that is adjacent to my src
folder:
use std::fs;
use std::path::{Path, PathBuf};
use sevenz_rust::decompress_file;
fn get_full_path(filename: &str) -> Option<PathBuf> {
let formatted_filename = format!("data/{}.7z", filename);
let filepath = Path::new(&formatted_filename);
fs::canonicalize(filepath).ok()
}
fn main() {
let filename = "enwiki-20230201-pages-meta-history1.xml-p1p857";
if let Some(path) = get_full_path(filename) {
let target = path.with_extension("");
println!("Result Path: {:?}", target);
decompress_file(path, target).expect("complete");
} else {
println!("Failed to get full path.");
}
}
And my folder structure is as follows:
package-name/
data/
enwiki-20230201-pages-meta-history1.xml-p1p857.7z
src/
main.rs
Cargo.lock
Cargo.toml
This program fails with an error saying that there is No such file or directory
(when referring to the "dest/target" path) but my understanding is that the target can (should?) be created at runtime. Is there an issue with how the absolute path is being used?
Result Path: "/Users/megan/Documents/files/dev/package-name/data/enwiki-20230201-pages-meta-history1.xml-p1p857"
thread 'main' panicked at 'complete: Io(Os { code: 2, kind: NotFound, message: "No such file or directory" }, "/Users/megan/Documents/files/dev/package-name/data/enwiki-20230201-pages-meta-history1.xml-p1p857/")', src/main.rs:28:39
stack backtrace:
0: rust_begin_unwind
at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:579:5
1: core::panicking::panic_fmt
at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/panicking.rs:64:14
2: core::result::unwrap_failed
at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/result.rs:1750:5
3: core::result::Result<T,E>::expect
at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/result.rs:1047:23
4: wikiscan::main
at ./src/main.rs:28:9
5: core::ops::function::FnOnce::call_once
at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
英文:
I'm encountering an error and wanted to make sure I'm understanding how the sevenz_rust::decompress_file
function should be used. This is my first rust program, so there may be some anti-patterns or obvious mistakes in this code.
Here is how the package documentation describes how decompress_file
should be used:
> Decompress source file "data/sample.7z" to dest path "data/sample"
sevenz_rust::decompress_file("data/sample.7z", "data/sample").expect("complete");
Right now I'm trying to decompress a file from a folder data
that is adjacent to my src
folder:
use std::fs;
use std::path::{Path, PathBuf};
use sevenz_rust::decompress_file;
fn get_full_path(filename: &str) -> Option<PathBuf> {
let formatted_filename = format!("data/{}.7z", filename);
let filepath = Path::new(&formatted_filename);
fs::canonicalize(filepath).ok()
}
fn main() {
let filename = "enwiki-20230201-pages-meta-history1.xml-p1p857";
if let Some(path) = get_full_path(filename) {
let target = path.with_extension("");
println!("Result Path: {:?}", target);
decompress_file(path, target).expect("complete");
} else {
println!("Failed to get full path.");
}
}
And my folder structure is as follows:
package-name/
data/
enwiki-20230201-pages-meta-history1.xml-p1p857.7z
src/
main.rs
Cargo.lock
Cargo.toml
This program fails with an error saying that there is No such file or directory
(when referring to the "dest/target" path) but my understanding is that the target can (should?) be created at runtime. Is there an issue with how the absolute path is being used?
Result Path: "/Users/megan/Documents/files/dev/package-name/data/enwiki-20230201-pages-meta-history1.xml-p1p857"
thread 'main' panicked at 'complete: Io(Os { code: 2, kind: NotFound, message: "No such file or directory" }, "/Users/megan/Documents/files/dev/package-name/data/enwiki-20230201-pages-meta-history1.xml-p1p857/")', src/main.rs:28:39
stack backtrace:
0: rust_begin_unwind
at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:579:5
1: core::panicking::panic_fmt
at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/panicking.rs:64:14
2: core::result::unwrap_failed
at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/result.rs:1750:5
3: core::result::Result<T,E>::expect
at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/result.rs:1047:23
4: wikiscan::main
at ./src/main.rs:28:9
5: core::ops::function::FnOnce::call_once
at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
答案1
得分: 0
问题出在文件本身:压缩文件是空的。用一个包含内容的新的“.7z”文件替换后,代码运行正常。
英文:
The problem ended up being with the file itself: the compressed file was empty. After replacing the file with a new ".7z" file that contained content the code worked.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论