-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.py
More file actions
55 lines (41 loc) · 1.65 KB
/
file.py
File metadata and controls
55 lines (41 loc) · 1.65 KB
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
54
55
import zipfile
import time
import json
import os
from progressBar import progress_bar_unzipping
def unZIP(zip_file_path, folder_name):
# Creating folder if it does not exist
if not os.path.exists(folder_name):
os.makedirs(folder_name)
print(f"Folder created: {folder_name}")
# Extracting .zip file
try:
extract_to = os.path.join(os.getcwd(), folder_name)
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
file_list = zip_ref.namelist()
total_files = len(file_list)
start_time = time.time()
for i, file in enumerate(file_list, 1):
zip_ref.extract(file, extract_to)
elapsed_time = time.time() - start_time
speed = i / elapsed_time if elapsed_time > 0 else 0
progress_bar_unzipping(i, total_files, speed, elapsed_time, prefix='Extracting')
print(f"- Successfully extracted")
except FileNotFoundError:
print(f"Error: File '{zip_file_path}' not found.")
except zipfile.BadZipFile:
print(f"Error: The file '{zip_file_path}' is either corrupted or not a valid ZIP file.")
except Exception as e:
print(f"Unexpected error: {e}")
def loadJsonFile(json_file_path):
try:
with open(json_file_path, "r", encoding="utf-8") as file:
data = json.load(file)
return data
except FileNotFoundError:
print(f"Error: File '{json_file_path}' not found.")
except json.JSONDecodeError:
print("Error: The JSON file is improperly formatted.")
except Exception as e:
print(f"Unexpected error: {e}")
return None