Skip to content

Commit

Permalink
Fix file header detection in open_decompress (#998)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunzheng authored Jan 22, 2025
1 parent cb7b9ec commit e7d007a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
9 changes: 6 additions & 3 deletions dissect/target/helpers/fsutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,12 @@ def open_decompress(
An binary or text IO stream, depending on the mode with which the file was opened.
Example:
bytes_buf = open_decompress(Path("/dir/file.gz")).read()
.. code-block:: python
for line in open_decompress(Path("/dir/file.gz"), "rt"):
print(line)
bytes_buf = open_decompress(Path("/dir/file.gz")).read()
for line in open_decompress(Path("/dir/file.gz"), "rt"):
print(line)
"""
if path and fileobj:
raise ValueError("path and fileobj are mutually exclusive")
Expand All @@ -527,6 +529,7 @@ def open_decompress(
file = path.open("rb")
else:
file = fileobj
file.seek(0)

magic = file.read(5)
file.seek(0)
Expand Down
5 changes: 4 additions & 1 deletion tests/helpers/test_fsutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,11 @@ def test_pure_dissect_path__from_parts_no_fs_exception() -> None:
)
def test_open_decompress(file_name: str, compressor: Callable, content: bytes) -> None:
vfs = VirtualFilesystem()
vfs.map_file_fh(file_name, io.BytesIO(compressor(content)))
fh = io.BytesIO(compressor(content))
vfs.map_file_fh(file_name, fh)
assert fsutil.open_decompress(vfs.path(file_name)).read() == content
fh.seek(2)
assert fsutil.open_decompress(fileobj=fh).read() == content


def test_open_decompress_text_modes() -> None:
Expand Down

0 comments on commit e7d007a

Please sign in to comment.