repack.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import zipfile, os, sys
  2. SRC = "target/resource-platform-new.jar"
  3. DST = "target/resource-platform-v2.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 must be first
  11. manifest = zin.getinfo("META-INF/MANIFEST.MF")
  12. with zipfile.ZipFile(DST, "w", compression=zipfile.ZIP_DEFLATED) as zout:
  13. # 1) manifest first
  14. zout.writestr(manifest, zin.read(manifest.filename))
  15. for info in infos:
  16. name = info.filename
  17. if name == "META-INF/MANIFEST.MF":
  18. continue
  19. if name == TARGET_ENTRY:
  20. zi = zipfile.ZipInfo(name)
  21. zi.compress_type = info.compress_type
  22. zi.external_attr = info.external_attr
  23. zi.date_time = info.date_time
  24. zout.writestr(zi, new_bytes)
  25. print("replaced:", name)
  26. else:
  27. data = zin.read(info.filename)
  28. zi = zipfile.ZipInfo(name, date_time=info.date_time)
  29. zi.compress_type = info.compress_type
  30. zi.external_attr = info.external_attr
  31. zout.writestr(zi, data)
  32. print("wrote", DST, os.path.getsize(DST), "bytes")