import zipfile, os, sys SRC = "target/resource-platform-new.jar" DST = "target/resource-platform-v2.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 must be first manifest = zin.getinfo("META-INF/MANIFEST.MF") with zipfile.ZipFile(DST, "w", compression=zipfile.ZIP_DEFLATED) as zout: # 1) manifest first 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")