Use
calibre-debug --expode-book file.epub
which will unpack file.epub into a temp dir and then wait for you to type y after making your changes and then recreate the epub from the temp dir.
You can automate it by using something like expect to pass in the y automatically after running the replace tool.
|