repack_v3.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import zipfile, os
  2. SRC = "target/resource-platform-v2.jar"
  3. DST = "target/resource-platform-v3.jar"
  4. NEW_CLASS_SRC = "build_tmp_classes/com/example/resourceplatform/common/PurchaseDuration.class"
  5. TARGET_ENTRY = "BOOT-INF/classes/com/example/resourceplatform/common/PurchaseDuration.class"
  6. with open(NEW_CLASS_SRC, "rb") as f:
  7. new_bytes = f.read()
  8. with zipfile.ZipFile(SRC, "r") as zin:
  9. infos = zin.infolist()
  10. manifest = zin.getinfo("META-INF/MANIFEST.MF")
  11. with zipfile.ZipFile(DST, "w", compression=zipfile.ZIP_DEFLATED) as zout:
  12. zout.writestr(manifest, zin.read(manifest.filename))
  13. for info in infos:
  14. name = info.filename
  15. if name == "META-INF/MANIFEST.MF":
  16. continue
  17. if name == TARGET_ENTRY:
  18. zi = zipfile.ZipInfo(name)
  19. zi.compress_type = info.compress_type
  20. zi.external_attr = info.external_attr
  21. zi.date_time = info.date_time
  22. zout.writestr(zi, new_bytes)
  23. print("replaced:", name)
  24. else:
  25. data = zin.read(info.filename)
  26. zi = zipfile.ZipInfo(name, date_time=info.date_time)
  27. zi.compress_type = info.compress_type
  28. zi.external_attr = info.external_attr
  29. zout.writestr(zi, data)
  30. print("wrote", DST, os.path.getsize(DST), "bytes")
  31. # sanity: manifest first + class present
  32. with zipfile.ZipFile(DST) as z:
  33. names = z.namelist()
  34. print("manifest first?", names[0] == "META-INF/MANIFEST.MF")
  35. print("class present?", TARGET_ENTRY in names)