19

I unzipped an epub file to make some minor modifications. How can I repack it properly to be a valid epub file from command line on Linux?

20
zip -rX ../my.epub mimetype META-INF/ OEBPS/

Without -X you could get the following when validating it with EpubCheck:

ERROR: my.epub: Mimetype entry must not have an extra field in its ZIP header

If mimetype is not the first in the epub file EpubCheck prints the following:

ERROR: my.epub: Mimetype entry missing or not the first in archive
4
zip -X0 "my.epub" "mimetype"
zip -Xr "my.epub" "META-INF/" "OEBPS/"

As palacsint already pointed out, without the -X flag, one would run into the epubcheck error PKG_005

"The mimetype file has an extra field of length %1$s. The use of the extra field feature of the ZIP format is not permitted for the mimetype file."

because the mimetype file is being used as magical number within the compressed zip file, and the zip extra field feature (where zip programs and operating systems can place non-standardized data) would destroy the magical number mechanism (see https://github.com/IDPF/epubcheck/pull/497). The -0 flag makes sure that the mimetype file doesn't get compressed, which is also necessary for the mimetype file to work as a magical number, otherwise the data at the position where the mimetype file is expected could be compressed, scrambled gibberish.

2

A one-liner based on the previous answers, which will take the current dirname as the epub name, and the contents of the current dir as the epub: zip -rX "../$(basename "$(realpath .)").epub" mimetype $(ls|xargs echo|sed 's/mimetype//g')

  • On a Mac, realpath is not available by default and you'll want to exclude the invisible .DS_Store files… Making the command: zip -rX "../myprecious.epub" mimetype $(ls|xargs echo|sed 's/mimetype//g') -x *.DS_Store. – Fabien Snauwaert Feb 21 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.