-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_zst.py
37 lines (27 loc) · 917 Bytes
/
extract_zst.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from pathlib import Path
import tempfile
import tarfile
import zstandard
# pip install zstandard
def extract_zst(archive, out_path):
"""extract .zst file
works on Windows, Linux, MacOS, etc.
Parameters
----------
archive: pathlib.Path or str
.zst file to extract
out_path: pathlib.Path or str
directory to extract files and directories to
"""
if zstandard is None:
raise ImportError("pip install zstandard")
archive = Path(archive).expanduser()
out_path = Path(out_path).expanduser().resolve()
# need .resolve() in case intermediate relative dir doesn't exist
dctx = zstandard.ZstdDecompressor()
with tempfile.TemporaryFile(suffix=".tar") as ofh:
with archive.open("rb") as ifh:
dctx.copy_stream(ifh, ofh)
ofh.seek(0)
with tarfile.open(fileobj=ofh) as z:
z.extractall(out_path)