Spaces:
Runtime error
Runtime error
Create test.py
Browse files
test.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, subprocess, sys, zipfile
|
| 2 |
+
|
| 3 |
+
# === 1.B Extraer el dataset
|
| 4 |
+
def extract_zip(zip_file_path, destination_path):
|
| 5 |
+
"""
|
| 6 |
+
Extracts the contents of a ZIP file to a specified directory.
|
| 7 |
+
|
| 8 |
+
Args:
|
| 9 |
+
zip_file_path (str): The full path to the ZIP file.
|
| 10 |
+
destination_path (str): The directory where the contents will be extracted.
|
| 11 |
+
"""
|
| 12 |
+
# Create the destination directory if it doesn't exist
|
| 13 |
+
os.makedirs(destination_path, exist_ok=True)
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
# Open the ZIP file in read mode
|
| 17 |
+
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
|
| 18 |
+
# Extract all the contents to the specified directory
|
| 19 |
+
zip_ref.extractall(destination_path)
|
| 20 |
+
print(f"✅ Extracted '{zip_file_path}' to '{destination_path}' successfully.")
|
| 21 |
+
except zipfile.BadZipFile:
|
| 22 |
+
print(f"❌ Error: The file '{zip_file_path}' is not a valid ZIP file.")
|
| 23 |
+
except FileNotFoundError:
|
| 24 |
+
print(f"❌ Error: The file '{zip_file_path}' was not found.")
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"❌ An unexpected error occurred: {e}")
|
| 27 |
+
|
| 28 |
+
# Example usage:
|
| 29 |
+
zip_file = "/home/user/app/voxpopuli_es_500.zip"
|
| 30 |
+
DATASET_PATH = "/tmp/dataset/voxpopuli_es_500"
|
| 31 |
+
|
| 32 |
+
# To protect against security vulnerabilities, it is important to sanitize the destination path.
|
| 33 |
+
# This prevents an attacker from using a malicious ZIP file to write outside the destination folder.
|
| 34 |
+
safe_destination = os.path.abspath(DATASET_PATH)
|
| 35 |
+
|
| 36 |
+
# Call the function
|
| 37 |
+
extract_zip(zip_file, safe_destination)
|