import zipfile import os import glob def recursive_unzip_random(): layer = 0 while True: # 1. Find the current zip file in the directory # We look for any file ending in .zip zip_files = glob.glob("*.zip") if not zip_files: print("No more zip files found. Extraction complete.") break # Take the first zip found (assuming only one path exists) current_zip = zip_files[0] print(f"Layer {layer}: Processing '{current_zip}'...") try: with zipfile.ZipFile(current_zip, 'r') as z: # Get the list of files inside *before* extracting file_list = z.namelist() z.extractall() # Remove the zip we just finished processing to keep the folder clean os.remove(current_zip) # 2. Process the extracted files next_zip_found = False for filename in file_list: # If it's a directory, ignore it if os.path.isdir(filename): continue # Check extensions to decide what to do if filename.endswith(".zip"): # This is the next layer; leave it alone for the next loop iteration next_zip_found = True else: # This is content (text, image, etc.) # We rename it with a number prefix to preserve the order (000_filename.txt) # This is crucial if the filenames themselves form a sentence. if "flag" not in filename: os.remove(filename) if not next_zip_found: print("End of chain reached (no nested zip found).") break layer += 1 except zipfile.BadZipFile: print(f"Error: '{current_zip}' is corrupted.") break if __name__ == "__main__": recursive_unzip_random()