| 123456789101112131415161718192021222324252627282930313233343536373839 |
- import zipfile, os
- SRC = "target/resource-platform-v2.jar"
- DST = "target/resource-platform-v3.jar"
- NEW_CLASS_SRC = "build_tmp_classes/com/example/resourceplatform/common/PurchaseDuration.class"
- TARGET_ENTRY = "BOOT-INF/classes/com/example/resourceplatform/common/PurchaseDuration.class"
- with open(NEW_CLASS_SRC, "rb") as f:
- new_bytes = f.read()
- with zipfile.ZipFile(SRC, "r") as zin:
- infos = zin.infolist()
- manifest = zin.getinfo("META-INF/MANIFEST.MF")
- with zipfile.ZipFile(DST, "w", compression=zipfile.ZIP_DEFLATED) as zout:
- zout.writestr(manifest, zin.read(manifest.filename))
- for info in infos:
- name = info.filename
- if name == "META-INF/MANIFEST.MF":
- continue
- if name == TARGET_ENTRY:
- zi = zipfile.ZipInfo(name)
- zi.compress_type = info.compress_type
- zi.external_attr = info.external_attr
- zi.date_time = info.date_time
- zout.writestr(zi, new_bytes)
- print("replaced:", name)
- else:
- data = zin.read(info.filename)
- zi = zipfile.ZipInfo(name, date_time=info.date_time)
- zi.compress_type = info.compress_type
- zi.external_attr = info.external_attr
- zout.writestr(zi, data)
- print("wrote", DST, os.path.getsize(DST), "bytes")
- # sanity: manifest first + class present
- with zipfile.ZipFile(DST) as z:
- names = z.namelist()
- print("manifest first?", names[0] == "META-INF/MANIFEST.MF")
- print("class present?", TARGET_ENTRY in names)
|