-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils.py
53 lines (40 loc) · 1.58 KB
/
utils.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import gzip
from pathlib import Path
import requests
import zstandard
from loguru import logger
def download_file(url: str, output_path: Path) -> bool:
try:
output_path.parent.mkdir(parents=True, exist_ok=True)
response = requests.get(url, stream=True)
response.raise_for_status()
# Get the file size from headers (if available)
file_size = int(response.headers.get("content-length", 0))
with output_path.open("wb") as file:
if file_size == 0:
file.write(response.content)
else:
downloaded = 0
from tqdm import tqdm
for chunk in tqdm(
response.iter_content(chunk_size=8192),
total=file_size // 8192,
unit="KB",
desc="Downloading",
):
if chunk:
file.write(chunk)
downloaded += len(chunk)
return True
except requests.exceptions.RequestException:
logger.error("Failed to donwload the file.")
return False
def decompress_zst(input_path: Path, output_path: Path) -> None:
with open(input_path, "rb") as compressed:
dctx = zstandard.ZstdDecompressor()
with open(output_path, "wb") as destination:
dctx.copy_stream(compressed, destination)
def decompress_gz(input_path: Path, output_path: Path) -> None:
with gzip.open(input_path, "rb") as gz_file:
with open(output_path, "wb") as output_file:
output_file.write(gz_file.read())