tools/zipremove.py

43 lines
1.4 KiB
Python
Raw Normal View History

2024-04-06 03:57:52 +01:00
import zipfile
import os
import sys
import tempfile
from pathlib import Path
2024-04-06 04:28:55 +01:00
import shutil
2024-04-06 03:57:52 +01:00
def filter_and_repack_zip(zip_path):
temp_dir = tempfile.TemporaryDirectory()
new_zip_path = os.path.join(temp_dir.name, 'new_archive.zip')
with zipfile.ZipFile(zip_path, 'r') as original_zip:
with zipfile.ZipFile(new_zip_path, 'w') as new_zip:
for file in original_zip.namelist():
2024-04-08 01:29:24 +01:00
if "(USA" in file or "(USA, Europe)" in file or "(Proto" in file or "[BIOS" in file:
2024-04-06 03:57:52 +01:00
new_zip.writestr(file, original_zip.read(file))
2024-04-08 01:27:30 +01:00
else:
print(f"Removing {file} from {zip_path}")
2024-04-06 04:28:55 +01:00
# Remove the original file and move the new file to the original location
os.remove(zip_path)
shutil.move(new_zip_path, zip_path)
2024-04-06 03:57:52 +01:00
def process_directory(directory_path):
for zip_file in Path(directory_path).glob('*.zip'):
print(f"Processing {zip_file}")
filter_and_repack_zip(zip_file)
print("All ZIP files processed.")
2024-04-06 03:57:52 +01:00
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python script.py <path_to_directory_or_zip>")
2024-04-06 03:57:52 +01:00
sys.exit(1)
path = sys.argv[1]
if os.path.isdir(path):
process_directory(path)
elif os.path.isfile(path) and path.endswith('.zip'):
print(f"Processing {path}")
filter_and_repack_zip(path)
else:
print("The path is not a directory or a ZIP file.")