ABAO77's picture
Upload 12 files
0ec5620 verified
import os
import zipfile
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: Path to the zip file
destination_folder: Path to the destination folder
Raises:
FileNotFoundError: If 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}")