repack_v5.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import zipfile, os
  2. SRC = "target/resource-platform-v4.jar"
  3. OUT = "target/resource-platform-v5.jar"
  4. CLASSES = "build_tmp_classes"
  5. zin = zipfile.ZipFile(SRC)
  6. names = zin.namelist()
  7. def is_lib(n):
  8. return n.startswith("BOOT-INF/lib/")
  9. new_map = {}
  10. for root, _, files in os.walk(CLASSES):
  11. for f in files:
  12. full = os.path.join(root, f)
  13. arc = "BOOT-INF/classes/" + os.path.relpath(full, CLASSES).replace(os.sep, "/")
  14. new_map[arc] = full
  15. zout = zipfile.ZipFile(OUT, "w", zipfile.ZIP_DEFLATED)
  16. zout.writestr("META-INF/MANIFEST.MF", zin.read("META-INF/MANIFEST.MF"), zipfile.ZIP_STORED)
  17. for n in names:
  18. if n == "META-INF/MANIFEST.MF":
  19. continue
  20. if is_lib(n):
  21. zout.writestr(n, zin.read(n), zipfile.ZIP_STORED)
  22. elif n.startswith("BOOT-INF/classes/") and not n.endswith("/"):
  23. if n in new_map:
  24. with open(new_map[n], "rb") as fh:
  25. zout.writestr(n, fh.read(), zipfile.ZIP_DEFLATED)
  26. else:
  27. zout.writestr(n, zin.read(n), zipfile.ZIP_DEFLATED)
  28. else:
  29. zout.writestr(n, zin.read(n), zipfile.ZIP_DEFLATED)
  30. zout.close()
  31. zv = zipfile.ZipFile(OUT)
  32. print("manifest first?", zv.namelist()[0] == "META-INF/MANIFEST.MF")
  33. print("BusinessException replaced?", "BOOT-INF/classes/com/example/resourceplatform/common/BusinessException.class" in zv.namelist())