ABAO77's picture
Upload 37 files
5ce8318 verified
import os
import zipfile
from typing import Optional
def extract_zip_file(zip_file_path: str, destination_folder: str) -> None:
"""
Extract a zip file to a destination folder.
If destination folder already exists, extraction is skipped.
Args:
zip_file_path (str): Path to the zip file
destination_folder (str): Path to the destination folder
Raises:
FileNotFoundError: If the zip file doesn't exist
"""
# Check if destination folder already exists
if os.path.exists(destination_folder):
print(f"Destination folder {destination_folder} already exists. Skipping extraction.")
return
# Check if zip file exists
if not os.path.exists(zip_file_path):
raise FileNotFoundError(f"Zip file not found: {zip_file_path}")
# Create destination folder
os.makedirs(destination_folder, exist_ok=True)
# Extract the zip file
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
for member in zip_ref.infolist():
# Handle non-ASCII filenames
filename = member.filename.encode('cp437').decode('utf-8')
extracted_path = os.path.join(destination_folder, filename)
# Create directories if needed
os.makedirs(os.path.dirname(extracted_path), exist_ok=True)
# Extract file
if not filename.endswith('/'): # Skip directories
with zip_ref.open(member) as source, open(extracted_path, 'wb') as target:
target.write(source.read())
print(f"Successfully extracted {zip_file_path} to {destination_folder}")
def ensure_directory_exists(directory_path: str) -> None:
"""
Ensure that a directory exists, create it if it doesn't.
Args:
directory_path (str): Path to the directory
"""
os.makedirs(directory_path, exist_ok=True)
def get_file_extension(filename: str) -> Optional[str]:
"""
Get the extension of a file.
Args:
filename (str): Name of the file
Returns:
Optional[str]: The file extension without the dot, or None if no extension
"""
split = os.path.splitext(filename)
if len(split) > 1:
return split[1][1:] # Remove the dot
return None