@@ -317,9 +317,6 @@ def _get_file_length_from_http_response(response: requests.Response) -> Optional
317317
318318 This function extracts the file size from the HTTP response headers, either from the
319319 `Content-Range` or `Content-Length` header, if available (in that order).
320- The HTTP response object containing the headers.
321- `int` or `None`: The length of the file in bytes if the information is available,
322- otherwise `None`.
323320
324321 Args:
325322 response (`requests.Response`):
@@ -329,6 +326,15 @@ def _get_file_length_from_http_response(response: requests.Response) -> Optional
329326 `int` or `None`: The length of the file in bytes, or None if not available.
330327 """
331328
329+ # If HTTP response contains compressed body (e.g. gzip), the `Content-Length` header will
330+ # contain the length of the compressed body, not the uncompressed file size.
331+ # And at the start of transmission there's no way to know the uncompressed file size for gzip,
332+ # thus we return None in that case.
333+ content_encoding = response .headers .get ("Content-Encoding" , "identity" ).lower ()
334+ if content_encoding != "identity" :
335+ # gzip/br/deflate/zstd etc
336+ return None
337+
332338 content_range = response .headers .get ("Content-Range" )
333339 if content_range is not None :
334340 return int (content_range .rsplit ("/" )[- 1 ])
@@ -422,11 +428,7 @@ def http_get(
422428 )
423429
424430 hf_raise_for_status (r )
425- content_length = _get_file_length_from_http_response (r )
426-
427- # NOTE: 'total' is the total number of bytes to download, not the number of bytes in the file.
428- # If the file is compressed, the number of bytes in the saved file will be higher than 'total'.
429- total = resume_size + int (content_length ) if content_length is not None else None
431+ total : Optional [int ] = _get_file_length_from_http_response (r )
430432
431433 if displayed_filename is None :
432434 displayed_filename = url
0 commit comments