Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save atelierbram/71b3d1093ffe8121be3452deba2425e3 to your computer and use it in GitHub Desktop.
Save atelierbram/71b3d1093ffe8121be3452deba2425e3 to your computer and use it in GitHub Desktop.
Unzipping & Zipping ePub from Command Line

Unzipping & Zipping ePub from Command Line

To unzip the epub, move the ePub to a folder, cd to it then simply:

unzip MyEbook.epub

E-book readers require that the mimetype file is the first one in the zip document. What's more, to be fully compliant, this file should start at a very specific point - a 30-byte offset from the beginning of the zip file (so that the mimetype text itself starts at byte 38). If this sounds intimidating, don't worry. It's actually quite easy to achieve if you're careful.

To zip up an epub:

zip -X MyNewEbook.epub mimetype
zip -rg MyNewEbook.epub META-INF -x \*.DS_Store
zip -rg MyNewEbook.epub OEBPS -x \*.DS_Store

Some explanations necessary here. We start each line with two flags:

  • -r (recursive) This means move down through any directories/folders recursively, ensuring that everything in the folders specified gets included
  • -g (grow file)
@atelierbram
Copy link
Author

Found some even more concise versions from the Ebooks StackExchange: https://ebooks.stackexchange.com/questions/257/how-to-repack-an-epub-file-from-command-line/7278#7278?newreg=ec07b030898d48868a88caec7809c255

Linux: (Uses the name of the current folder as the epub filename) zip -rX "../$(basename "$(realpath .)").epub" mimetype $(ls|xargs echo|sed 's/mimetype//g')

macOS: (you should change the epub name here to whatever it should be called) zip -rX "../myprecious.epub" mimetype $(ls|xargs echo|sed 's/mimetype//g') -x *.DS_Store

I found this version also worked better for epubs I found that don't use the OEBPS folder. I don't know if not having that folder is valid per standards, but I found examples of it being missing in the wild.

comment by @phoenixeliot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment