Skip to content

Commit fba7730

Browse files
ctruedenclaude
andcommitted
Detect archive format by magic bytes when extension is missing
The micro.mamba.pm server stopped redirecting to versioned URLs like micromamba-2.5.0-2.tar.bz2 and now serves files directly via GET with a Content-Disposition header. Since the download URL (/latest) has no file extension, the temp file landed with no extension, causing unpack() to raise "Unsupported archive type". Fix by falling back to magic byte detection (GZip: 1F 8B, BZip2: BZh, ZIP: PK) when the extension is absent or unrecognized. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent e6ea0b6 commit fba7730

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

src/appose/util/download.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,30 @@ def unpack(input_file: Path, output_dir: Path) -> None:
161161
un_tar_gz(input_file, output_dir)
162162
elif filename.endswith(".zip"):
163163
un_zip(input_file, output_dir)
164+
else:
165+
_unpack_by_magic_bytes(input_file, output_dir)
166+
167+
168+
def _unpack_by_magic_bytes(input_file: Path, output_dir: Path) -> None:
169+
"""
170+
Detect archive format from magic bytes and unpack accordingly.
171+
172+
Used when the file extension is missing or unrecognized (e.g. when the
173+
server serves the file directly without a redirect to a named URL).
174+
"""
175+
with open(input_file, "rb") as f:
176+
magic = f.read(6)
177+
if len(magic) < 2:
178+
raise OSError(f"File too small to detect archive type: {input_file.name}")
179+
# GZip magic: 1F 8B
180+
if magic[:2] == b"\x1f\x8b":
181+
un_tar_gz(input_file, output_dir)
182+
# BZip2 magic: 42 5A 68 ("BZh")
183+
elif magic[:3] == b"\x42\x5a\x68":
184+
un_tar_bz2(input_file, output_dir)
185+
# ZIP magic: 50 4B 03 04 ("PK\x03\x04")
186+
elif magic[:4] == b"\x50\x4b\x03\x04":
187+
un_zip(input_file, output_dir)
164188
else:
165189
raise ValueError(f"Unsupported archive type for file: {input_file.name}")
166190

0 commit comments

Comments
 (0)