File size: 1,649 Bytes
5ce8318
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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())
            #Remove zip file
    print(f"Successfully extracted {zip_file_path} to {destination_folder}")