`sevenz_rust::decompress_file` 失败,显示“找不到文件或目录”。

huangapple go评论62阅读模式
英文:

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(&quot;data/sample.7z&quot;, &quot;data/sample&quot;).expect(&quot;complete&quot;);

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: &amp;str) -&gt; Option&lt;PathBuf&gt; {
    let formatted_filename = format!(&quot;data/{}.7z&quot;, filename);
    let filepath = Path::new(&amp;formatted_filename);
    fs::canonicalize(filepath).ok()
}

fn main() {
    let filename = &quot;enwiki-20230201-pages-meta-history1.xml-p1p857&quot;;

    if let Some(path) = get_full_path(filename) {
        let target = path.with_extension(&quot;&quot;);
        
        println!(&quot;Result Path: {:?}&quot;, target);

        decompress_file(path, target).expect(&quot;complete&quot;);
    } else {
        println!(&quot;Failed to get full path.&quot;);
    }
}

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: &quot;/Users/megan/Documents/files/dev/package-name/data/enwiki-20230201-pages-meta-history1.xml-p1p857&quot;
thread &#39;main&#39; panicked at &#39;complete: Io(Os { code: 2, kind: NotFound, message: &quot;No such file or directory&quot; }, &quot;/Users/megan/Documents/files/dev/package-name/data/enwiki-20230201-pages-meta-history1.xml-p1p857/&quot;)&#39;, 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&lt;T,E&gt;::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.

huangapple
  • 本文由 发表于 2023年5月13日 15:06:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定