Skript zur Reduktion der Dateigrösse durch Entfernung der enthaltenden .stl Dateien.
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Filtert ein ZIP-Archiv und entfernt alle .stl Dateien.
|
||||
Ausgabe: originalname_heute.zip (oder ersetzt Original mit --replace)
|
||||
"""
|
||||
|
||||
import zipfile
|
||||
import sys
|
||||
import argparse
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def filter_zip_no_stl(source_zip_path: str, replace: bool = False) -> str:
|
||||
"""
|
||||
Öffnet ein ZIP und erstellt ein neues ohne .stl Dateien.
|
||||
|
||||
Args:
|
||||
source_zip_path: Pfad zur Quell-ZIP-Datei
|
||||
replace: Wenn True, wird das Original ersetzt
|
||||
|
||||
Returns:
|
||||
Pfad zur erstellten/ersetzten ZIP-Datei
|
||||
"""
|
||||
source_path = Path(source_zip_path)
|
||||
|
||||
if not source_path.exists():
|
||||
raise FileNotFoundError(f"Datei nicht gefunden: {source_zip_path}")
|
||||
|
||||
if not source_path.suffix.lower() == '.zip':
|
||||
raise ValueError(f"Keine ZIP-Datei: {source_zip_path}")
|
||||
|
||||
# Temporäres Ziel-ZIP
|
||||
temp_path = source_path.with_name(f"{source_path.stem}_heute.zip")
|
||||
|
||||
copied_count = 0
|
||||
skipped_count = 0
|
||||
|
||||
with zipfile.ZipFile(source_path, 'r') as src_zip:
|
||||
with zipfile.ZipFile(temp_path, 'w', zipfile.ZIP_DEFLATED) as dst_zip:
|
||||
for info in src_zip.infolist():
|
||||
# Verzeichnisse überspringen
|
||||
if info.is_dir():
|
||||
continue
|
||||
|
||||
# .stl Dateien ausschließen (case-insensitive)
|
||||
if Path(info.filename).suffix.lower() == '.stl':
|
||||
skipped_count += 1
|
||||
print(f" ✗ {info.filename}")
|
||||
else:
|
||||
# Datei mit Original-Metadaten kopieren
|
||||
data = src_zip.read(info.filename)
|
||||
dst_zip.writestr(info, data)
|
||||
copied_count += 1
|
||||
|
||||
print(f"\nErgebnis: {copied_count} Datei(en) kopiert, {skipped_count} STL-Datei(en) entfernt")
|
||||
|
||||
if replace:
|
||||
shutil.move(str(temp_path), str(source_path))
|
||||
print(f"Original ersetzt: {source_path}")
|
||||
return str(source_path)
|
||||
else:
|
||||
print(f"Ziel-ZIP: {temp_path}")
|
||||
return str(temp_path)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Filtert ZIP-Archive und entfernt alle .stl Dateien."
|
||||
)
|
||||
parser.add_argument(
|
||||
"zipfile",
|
||||
help="Pfad zur Quell-ZIP-Datei"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--replace",
|
||||
action="store_true",
|
||||
help="Original-ZIP durch gefilterte Version ersetzen"
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
filter_zip_no_stl(args.zipfile, args.replace)
|
||||
except (FileNotFoundError, ValueError, zipfile.BadZipFile) as e:
|
||||
print(f"Fehler: {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user