Repository: ouch-org/ouch (Rust)

Issue:
7z BadSignature error when compressing and then listing
### Version

0.6.1

### Description

This gives an error:

```sh
ouch c README.md a.7z.gz
ouch l a.7z.gz
```

### Current Behavior

```
[ERROR] 7z error
 - BadSignature([31, 139, 8, 0, 0, 0])
```

### Expected Behavior

It should be able to list it.

### Additional Information

Decompressing the same archive works as expected.


Discussion:
This is interesting.
I tried the list command failed on this line of code

https://github.com/hasenbanck/sevenz-rust2/blob/main/src/de_funcs.rs#L96

But the decompress command won't.

My error message is the same as you are, hmmmm.

Interesting! ahah
ok, I found the root cause. I'll open a pr to fix this.



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 232173a14..46d30df34 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -30,6 +30,9 @@ Categories Used:
 - Give better error messages when archive extensions are invalid [\#817](https://github.com/ouch-org/ouch/pull/817) ([marcospb19](https://github.com/marcospb19))
 
 ### Bug Fixes
+
+- Fix 7z BadSignature error when compressing and then listing [\#819](https://github.com/ouch-org/ouch/pull/819) ([tommady](https://github.com/tommady))
+
 ### Tweaks
 
 - Make `.bz3` opt-out [\#814](https://github.com/ouch-org/ouch/pull/814) ([amyspark](https://github.com/amyspark))
diff --git a/src/archive/sevenz.rs b/src/archive/sevenz.rs
index 2618209a5..f741b78a4 100644
--- a/src/archive/sevenz.rs
+++ b/src/archive/sevenz.rs
@@ -171,12 +171,10 @@ where
 }
 
 /// List contents of `archive_path`, returning a vector of archive entries
-pub fn list_archive(
-    archive_path: &Path,
-    password: Option<&[u8]>,
-) -> Result<impl Iterator<Item = crate::Result<FileInArchive>>> {
-    let reader = fs::File::open(archive_path)?;
-
+pub fn list_archive<R>(reader: R, password: Option<&[u8]>) -> Result<impl Iterator<Item = crate::Result<FileInArchive>>>
+where
+    R: Read + Seek,
+{
     let mut files = Vec::new();
 
     let entry_extract_fn = |entry: &SevenZArchiveEntry, _: &mut dyn Read, _: &PathBuf| {
diff --git a/src/commands/list.rs b/src/commands/list.rs
index ef7a5f509..4a344a7a9 100644
--- a/src/commands/list.rs
+++ b/src/commands/list.rs
@@ -122,7 +122,10 @@ pub fn list_archive_contents(
                 }
             }
 
-            Box::new(archive::sevenz::list_archive(archive_path, password)?)
+            let mut vec = vec![];
+            io::copy(&mut reader, &mut vec)?;
+
+            Box::new(archive::sevenz::list_archive(io::Cursor::new(vec), password)?)
         }
         Gzip | Bzip | Bzip3 | Lz4 | Lzma | Snappy | Zstd | Brotli => {
             unreachable!("Not an archive, should be validated before calling this function.");


Test patch:
diff --git a/tests/integration.rs b/tests/integration.rs
index b43bb0881..9f870db2b 100644
--- a/tests/integration.rs
+++ b/tests/integration.rs
@@ -1065,3 +1065,27 @@ fn fail_when_compressing_archive_as_the_second_extension() {
         .is_some());
     }
 }
+
+#[test]
+fn sevenz_list_should_not_failed() {
+    let temp_dir = tempdir().unwrap();
+    let root_path = temp_dir.path();
+    let src_files_path = root_path.join("src_files");
+    fs::create_dir_all(&src_files_path).unwrap();
+
+    let archive = root_path.join("archive.7z.gz");
+    crate::utils::cargo_bin()
+        .arg("compress")
+        .arg("--yes")
+        .arg(fs::File::create(src_files_path.join("README.md")).unwrap().path())
+        .arg(&archive)
+        .assert()
+        .success();
+
+    crate::utils::cargo_bin()
+        .arg("list")
+        .arg("--yes")
+        .arg(&archive)
+        .assert()
+        .success();
+}