in client/securedrop_client/crypto.py [0:0]
def read_gzip_header_filename(filename: str) -> str:
"""
Extract the original filename from the header of a gzipped file.
Adapted from Python's gzip._GzipReader._read_gzip_header.
"""
original_filename = ""
with open(filename, "rb") as f:
gzip_header_identification = f.read(2)
if gzip_header_identification != GZIP_FILE_IDENTIFICATION:
raise OSError(f"Not a gzipped file ({gzip_header_identification!r})")
(gzip_header_compression_method, gzip_header_flags, _) = struct.unpack("<BBIxx", f.read(8))
if gzip_header_compression_method != 8:
raise OSError("Unknown compression method")
if gzip_header_flags & GZIP_FLAG_EXTRA_FIELDS:
(extra_len,) = struct.unpack("<H", f.read(2))
f.read(extra_len)
if gzip_header_flags & GZIP_FLAG_FILENAME:
fb = b""
while True:
s = f.read(1)
if not s or s == b"\000":
break
fb += s
original_filename = str(fb, "utf-8")
return original_filename