Skip to main content

Bulletproof EPUB zipping in a single pass (or, How to zip an EPUB from the Terminal in OS X sans .DS_Store files)

I wrote a blogpost recently on how to remove .DS_Store files that erroneously find their way into EPUBs. I have since found a proper solution to my woes and have what seems to be a bulletproof way of zipping EPUBs without the need for double-zipping or later removal of .DS_Store files. It places the mimetype where it should be (at the top of the hierarchy) and leaves the .DS_Store files outside the archive. So without any further ado here is the magical line of code to be run in Terminal from within the folder containing your book files that you want to compress into an EPUB:

    zip -Xr9D book.epub mimetype META-INF OEBPS -x *.DS_Store

Note: All the files within the folders will be incorporated. If future EPUB specs change what is incorporated in the top level folder, then additional files and folders will need to be added after OEBPS or in their specific place in the order should they have one (files after the -x are the ones to be excluded). 


Update: Thanks to elmimmo (see comments) who suggests the following code so as not compress the mimetype and create an invalid EPUB file:

    zip -X0 book.epub mimetype; zip -Xur9D book.epub META-INF OEBPS -x *.DS_Store


I'm very gratefully that he shared this code, and if anyone else has any further thoughts or suggestions then please do comment. However, the reason that the code I posted in the first instance works (even though it shouldn't) and passes epubcheck is because it does not compress the mimetype (even though the line of code tells it too), which as far as I can tell is because the file is not large enough for zip to bother compressing. It hence records the mimetype as "stored" not "deflated", which is exactly what we want for an EPUB.


I'll leave the decision up to you as to which version you use, but keep in mind if you use the former and it stops working, you can switch to the second.  

Comments

  1. This applies compression to mimetype, hence producing a non-valid EPUB. You have no choice but to do it in two passes, but you can stack them up in the same line

    zip -X0 book.epub mimetype || zip -Xur9D book.epub META-INF OEBPS -x *.DS_Store

    ReplyDelete

Post a comment