@@ -241,7 +241,7 @@ def _get_filepath_or_buffer(
241
241
----------
242
242
filepath_or_buffer : a url, filepath (str, py.path.local or pathlib.Path),
243
243
or buffer
244
- compression : {{'gzip', 'bz2', 'zip', 'xz', None}}, optional
244
+ compression : {{'gzip', 'bz2', 'zip', 'xz', 'zstd', None}}, optional
245
245
encoding : the encoding to use to decode bytes, default is 'utf-8'
246
246
mode : str, optional
247
247
@@ -420,7 +420,7 @@ def file_path_to_url(path: str) -> str:
420
420
return urljoin ("file:" , pathname2url (path ))
421
421
422
422
423
- _compression_to_extension = {"gzip" : ".gz" , "bz2" : ".bz2" , "zip" : ".zip" , "xz" : ".xz" }
423
+ _compression_to_extension = {"gzip" : ".gz" , "bz2" : ".bz2" , "zip" : ".zip" , "xz" : ".xz" , "zstd" : ".zst" }
424
424
425
425
426
426
def get_compression_method (
@@ -471,10 +471,10 @@ def infer_compression(
471
471
----------
472
472
filepath_or_buffer : str or file handle
473
473
File path or object.
474
- compression : {'infer', 'gzip', 'bz2', 'zip', 'xz', None}
474
+ compression : {'infer', 'gzip', 'bz2', 'zip', 'xz', 'zstd', None}
475
475
If 'infer' and `filepath_or_buffer` is path-like, then detect
476
476
compression from the following extensions: '.gz', '.bz2', '.zip',
477
- or '.xz ' (otherwise no compression).
477
+ '.xz', or '.zst ' (otherwise no compression).
478
478
479
479
Returns
480
480
-------
@@ -556,11 +556,11 @@ def get_handle(
556
556
compression : str or dict, default None
557
557
If string, specifies compression mode. If dict, value at key 'method'
558
558
specifies compression mode. Compression mode must be one of {'infer',
559
- 'gzip', 'bz2', 'zip', 'xz', None}. If compression mode is 'infer'
560
- and `filepath_or_buffer` is path-like, then detect compression from
561
- the following extensions: '.gz', '.bz2', '.zip', or '.xz' (otherwise
562
- no compression). If dict and compression mode is one of
563
- {'zip', 'gzip', 'bz2'}, or inferred as one of the above,
559
+ 'gzip', 'bz2', 'zip', 'xz', 'zstd', None}. If compression mode is
560
+ 'infer' and `filepath_or_buffer` is path-like, then detect compression
561
+ from the following extensions: '.gz', '.bz2', '.zip', '.xz', or '.zst'
562
+ (otherwise no compression). If dict and compression mode is one of
563
+ {'zip', 'gzip', 'bz2', 'zstd' }, or inferred as one of the above,
564
564
other entries passed as additional compression options.
565
565
566
566
.. versionchanged:: 1.0.0
@@ -572,7 +572,7 @@ def get_handle(
572
572
.. versionchanged:: 1.1.0
573
573
574
574
Passing compression options as keys in dict is now
575
- supported for compression modes 'gzip' and 'bz2' as well as 'zip'.
575
+ supported for compression modes 'gzip', 'bz2', 'zstd' and 'zip'.
576
576
577
577
memory_map : bool, default False
578
578
See parsers._parser_params for more information.
@@ -689,6 +689,23 @@ def get_handle(
689
689
elif compression == "xz" :
690
690
handle = get_lzma_file (lzma )(handle , ioargs .mode )
691
691
692
+ # Zstd Compression
693
+ elif compression == "zstd" :
694
+ zstd = import_optional_dependency ("zstandard" )
695
+ open_args = {
696
+ arg : compression_args .pop (arg , None )
697
+ for arg in ["encoding" , "errors" , "newline" ]
698
+ }
699
+ if "r" in ioargs .mode :
700
+ open_args ["dctx" ] = zstd .ZstdDecompressor (** compression_args )
701
+ else :
702
+ open_args ["cctx" ] = zstd .ZstdCompressor (** compression_args )
703
+ handle = zstd .open (
704
+ handle ,
705
+ mode = ioargs .mode ,
706
+ ** open_args ,
707
+ )
708
+
692
709
# Unrecognized Compression
693
710
else :
694
711
msg = f"Unrecognized compression type: { compression } "
0 commit comments