Advertisement
gruntfutuk

dezipwav

Jun 26th, 2025
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.27 KB | None | 0 0
  1. import zipfile
  2. from pathlib import Path
  3. from typing import Iterator # Still need to import Iterator, but not List or Union
  4.  
  5. # Define type alias for clarity
  6. type FilePath = str | Path # Using 'type' keyword for type aliases (PEP 695 in Python 3.12+)
  7.  
  8. def extract_wav_files_from_zip(
  9.     zip_path: FilePath,
  10.     extract_to_dir: FilePath
  11. ) -> list[Path]: # Using built-in list generic
  12.     """
  13.    Extracts all .wav files from a specified ZIP archive to a target directory.
  14.  
  15.    Args:
  16.        zip_path: The path to the ZIP archive (str or Path object).
  17.        extract_to_dir: The directory where the .wav files should be extracted (str or Path object).
  18.  
  19.    Returns:
  20.        A list of Path objects for the extracted .wav files.
  21.  
  22.    Raises:
  23.        FileNotFoundError: If the specified zip_path does not exist.
  24.        zipfile.BadZipFile: If the file is not a valid ZIP archive.
  25.        PermissionError: If there are issues writing to the extract_to_dir.
  26.    """
  27.     zip_archive_path = Path(zip_path)
  28.     target_directory = Path(extract_to_dir)
  29.  
  30.     # Ensure the ZIP file exists
  31.     if not zip_archive_path.is_file():
  32.         raise FileNotFoundError(f"ZIP archive not found at: {zip_archive_path}")
  33.  
  34.     # Ensure the target directory exists, create if not
  35.     target_directory.mkdir(parents=True, exist_ok=True)
  36.  
  37.     extracted_files: list[Path] = [] # Using built-in list generic
  38.  
  39.     try:
  40.         with zipfile.ZipFile(zip_archive_path, 'r') as zf:
  41.             for member in zf.namelist():
  42.                 member_path = Path(member)
  43.                 if member_path.suffix.lower() == '.wav':
  44.                     extracted_file_full_path = target_directory.joinpath(member_path.name).resolve()
  45.  
  46.                     if not str(extracted_file_full_path).startswith(str(target_directory.resolve())):
  47.                         print(f"Skipping potentially malicious path outside target directory: {member}")
  48.                         continue
  49.  
  50.                     zf.extract(member, target_directory)
  51.                     extracted_files.append(extracted_file_full_path)
  52.                     print(f"Extracted: {member} to {extracted_file_full_path}")
  53.     except zipfile.BadZipFile as e:
  54.         raise zipfile.BadZipFile(f"Error: Not a valid ZIP file. {e}")
  55.     except Exception as e:
  56.         raise Exception(f"An error occurred during extraction: {e}")
  57.  
  58.     return extracted_files
  59.  
  60. def create_dummy_zip_with_wav(
  61.     zip_name: FilePath = "test_audio_archive.zip",
  62.     num_wav_files: int = 2,
  63.     num_other_files: int = 1
  64. ) -> Path:
  65.     """
  66.    Creates a dummy ZIP archive containing .wav files and other file types for testing.
  67.    """
  68.     zip_path = Path(zip_name)
  69.     temp_dir = Path("temp_dummy_files")
  70.     temp_dir.mkdir(exist_ok=True)
  71.  
  72.     with zipfile.ZipFile(zip_path, 'w') as zf:
  73.         for i in range(num_wav_files):
  74.             wav_file = temp_dir / f"audio_clip_{i + 1}.wav"
  75.             wav_file.write_bytes(b'\x00\x00\x00\x00')
  76.             zf.write(wav_file, wav_file.name)
  77.             wav_file.unlink()
  78.  
  79.         for i in range(num_other_files):
  80.             txt_file = temp_dir / f"document_{i + 1}.txt"
  81.             txt_file.write_text(f"This is a dummy text file {i + 1}.")
  82.             zf.write(txt_file, txt_file.name)
  83.             txt_file.unlink()
  84.  
  85.         sub_dir = temp_dir / "subdir"
  86.         sub_dir.mkdir(exist_ok=True)
  87.         sub_wav_file = sub_dir / "nested_audio.wav"
  88.         sub_wav_file.write_bytes(b'\x01\x01\x01\x01')
  89.         zf.write(sub_wav_file, str(sub_wav_file.relative_to(temp_dir)))
  90.         sub_wav_file.unlink()
  91.         sub_dir.rmdir()
  92.  
  93.     temp_dir.rmdir()
  94.     print(f"Created dummy ZIP: {zip_path}")
  95.     return zip_path
  96.  
  97. # --- Main execution example (remains the same) ---
  98. if __name__ == "__main__":
  99.     dummy_zip = create_dummy_zip_with_wav(num_wav_files=3, num_other_files=2)
  100.     extract_dir = Path("extracted_wav_files")
  101.  
  102.     try:
  103.         extracted_wavs = extract_wav_files_from_zip(dummy_zip, extract_dir)
  104.         print(f"\nSuccessfully extracted {len(extracted_wavs)} .wav files to: {extract_dir}")
  105.         for wav_file in extracted_wavs:
  106.             print(f"- {wav_file}")
  107.  
  108.         for f in extracted_wavs:
  109.             f.unlink(missing_ok=True)
  110.         if extract_dir.exists():
  111.             extract_dir.rmdir()
  112.             print(f"Cleaned up extraction directory: {extract_dir}")
  113.  
  114.     except (FileNotFoundError, zipfile.BadZipFile, PermissionError, Exception) as e:
  115.         print(f"Operation failed: {e}")
  116.     finally:
  117.         if dummy_zip.exists():
  118.             dummy_zip.unlink()
  119.             print(f"Cleaned up dummy ZIP: {dummy_zip}")
  120.  
  121.     print("\n--- Testing with a non-existent ZIP ---")
  122.     try:
  123.         extract_wav_files_from_zip("non_existent.zip", "temp_output")
  124.     except FileNotFoundError as e:
  125.         print(f"Caught expected error: {e}")
  126.  
  127.     print("\n--- Testing with a bad ZIP file (e.g., a text file renamed to .zip) ---")
  128.     bad_zip_path = Path("bad_archive.zip")
  129.     bad_zip_path.write_text("This is not a zip file.")
  130.     try:
  131.         extract_wav_files_from_zip(bad_zip_path, "temp_output")
  132.     except zipfile.BadZipFile as e:
  133.         print(f"Caught expected error: {e}")
  134.     finally:
  135.         if bad_zip_path.exists():
  136.             bad_zip_path.unlink()
  137.             print(f"Cleaned up bad ZIP file: {bad_zip_path}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement