|
| 1 | +import zipfile |
| 2 | +import os |
| 3 | + |
| 4 | +def unzip_all(zip_folder, extract_to): |
| 5 | + # Ensure the extract_to folder exists |
| 6 | + os.makedirs(extract_to, exist_ok=True) |
| 7 | + |
| 8 | + # Check if the zip_folder exists and contains files |
| 9 | + if not os.path.isdir(zip_folder): |
| 10 | + print(f"Error: The specified folder '{zip_folder}' does not exist.") |
| 11 | + return |
| 12 | + |
| 13 | + # Get a list of all .zip files in the directory |
| 14 | + zip_files = [f for f in os.listdir(zip_folder) if f.endswith(".zip")] |
| 15 | + if not zip_files: |
| 16 | + print("No .zip files found in the specified folder.") |
| 17 | + return |
| 18 | + |
| 19 | + # Iterate through each zip file and extract it |
| 20 | + for filename in zip_files: |
| 21 | + zip_path = os.path.join(zip_folder, filename) |
| 22 | + |
| 23 | + # Ensure it's a valid zip file |
| 24 | + if zipfile.is_zipfile(zip_path): |
| 25 | + try: |
| 26 | + with zipfile.ZipFile(zip_path, 'r') as zip_ref: |
| 27 | + print(f"Extracting '{filename}' to '{extract_to}'...") |
| 28 | + zip_ref.extractall(extract_to) |
| 29 | + print(f"'{filename}' extracted successfully.") |
| 30 | + except Exception as e: |
| 31 | + print(f"Failed to extract '{filename}': {e}") |
| 32 | + else: |
| 33 | + print(f"'{filename}' is not a valid zip file.") |
| 34 | + |
| 35 | +# Example usage: |
| 36 | +zip_folder = "/path/to/your/zip/folder" |
| 37 | +extract_to = "/path/to/extract/destination" |
| 38 | + |
| 39 | +unzip_all(zip_folder, extract_to) |
0 commit comments