Dezip.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import shutil
  2. from pathlib import Path
  3. from concurrent.futures import ThreadPoolExecutor
  4. import py7zr
  5. Dossier = "/media/remy/T7/IGNDonnee/"
  6. SOURCE = Path(Dossier)
  7. SORTIE = Path(Dossier)
  8. TYPES = ["DST", "MNT", "SRC"]
  9. NB_THREADS = 4
  10. for t in TYPES:
  11. (SORTIE / t).mkdir(exist_ok=True)
  12. (SOURCE / "FAIT").mkdir(exist_ok=True)
  13. temp = Path("_temp_rgealti")
  14. if temp.exists():
  15. shutil.rmtree(temp)
  16. def compresser_fichier(args):
  17. fichier, dossier_sortie = args
  18. archive_sortie = dossier_sortie / f"{fichier.name}.7z"
  19. with py7zr.SevenZipFile(archive_sortie, "w") as z:
  20. z.write(fichier, arcname=fichier.name)
  21. archives = list(
  22. SOURCE.glob("RGEALTI_2-0_1M_ASC_LAMB93-IGN69_D0*.7z")
  23. )
  24. with ThreadPoolExecutor(max_workers=NB_THREADS) as pool:
  25. for archive in archives:
  26. print(archive)
  27. if temp.exists():
  28. shutil.rmtree(temp)
  29. temp.mkdir()
  30. # Extraction archive source
  31. with py7zr.SevenZipFile(archive, "r") as z:
  32. z.extractall(temp)
  33. print("fin Extraction")
  34. taches = []
  35. for t in TYPES:
  36. for dossier in temp.rglob(f"RGEALTI_{t}_1M_*"):
  37. if dossier.is_dir():
  38. for fichier in dossier.iterdir():
  39. if fichier.is_file():
  40. taches.append((fichier, SORTIE / t))
  41. list(pool.map(compresser_fichier, taches))
  42. # Déplacement archive traitée
  43. shutil.move(
  44. str(archive),
  45. str(SOURCE / "FAIT" / archive.name)
  46. )
  47. if temp.exists():
  48. shutil.rmtree(temp)