Spaces:
Running
Running
admin
commited on
Commit
·
45a7258
1
Parent(s):
ca4fff8
sync ms
Browse files
utils.py
CHANGED
|
@@ -1,7 +1,10 @@
|
|
| 1 |
import os
|
| 2 |
import shutil
|
|
|
|
| 3 |
import zipfile
|
| 4 |
|
|
|
|
|
|
|
| 5 |
EN_US = os.getenv("LANG") != "zh_CN.UTF-8"
|
| 6 |
TMP_DIR = "./__pycache__"
|
| 7 |
|
|
@@ -18,12 +21,22 @@ def clean_dir(dir_path: str):
|
|
| 18 |
os.makedirs(dir_path)
|
| 19 |
|
| 20 |
|
| 21 |
-
def unzip(
|
| 22 |
mk_dir(extract_to)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
|
| 29 |
def compress(folder_path: str, zip_file: str):
|
|
@@ -40,6 +53,5 @@ def compress(folder_path: str, zip_file: str):
|
|
| 40 |
relative_path = os.path.relpath(file_path, folder_path)
|
| 41 |
zipf.write(
|
| 42 |
file_path,
|
| 43 |
-
arcname=os.path.join(os.path.basename(
|
| 44 |
-
folder_path), relative_path),
|
| 45 |
)
|
|
|
|
| 1 |
import os
|
| 2 |
import shutil
|
| 3 |
+
import rarfile
|
| 4 |
import zipfile
|
| 5 |
|
| 6 |
+
from py7zr import SevenZipFile
|
| 7 |
+
|
| 8 |
EN_US = os.getenv("LANG") != "zh_CN.UTF-8"
|
| 9 |
TMP_DIR = "./__pycache__"
|
| 10 |
|
|
|
|
| 21 |
os.makedirs(dir_path)
|
| 22 |
|
| 23 |
|
| 24 |
+
def unzip(archive: str, extract_to: str):
|
| 25 |
mk_dir(extract_to)
|
| 26 |
+
if archive.endswith(".zip"):
|
| 27 |
+
with zipfile.ZipFile(archive, "r") as f:
|
| 28 |
+
f.extractall(extract_to)
|
| 29 |
+
|
| 30 |
+
elif archive.endswith(".7z"):
|
| 31 |
+
with SevenZipFile(archive, "r") as f:
|
| 32 |
+
f.extractall(extract_to)
|
| 33 |
+
|
| 34 |
+
elif archive.endswith(".rar"):
|
| 35 |
+
with rarfile.RarFile(archive, "r") as f:
|
| 36 |
+
f.extractall(extract_to)
|
| 37 |
+
|
| 38 |
+
else:
|
| 39 |
+
raise ValueError("Unsupported file type!")
|
| 40 |
|
| 41 |
|
| 42 |
def compress(folder_path: str, zip_file: str):
|
|
|
|
| 53 |
relative_path = os.path.relpath(file_path, folder_path)
|
| 54 |
zipf.write(
|
| 55 |
file_path,
|
| 56 |
+
arcname=os.path.join(os.path.basename(folder_path), relative_path),
|
|
|
|
| 57 |
)
|