It's time to stop unzipping your EPUBs and start using zip properly on macOS


I'm no zip guru and trust me, if I can use a GUI I will but I've come to realise that I'm wasting a lot of time zipping and unzipping, and while BBEdit does a good job of allowing us to browse EPUBs and edit EPUBs, it has its limitations.

Zipping an EPUB

There are a variety of ways you might arrive at an EPUB: for example, outputting from an app like InDesign, or Pages, etc., or creating using the save option in epubcheck's expanded validation process (thanks to bowerbird for drawing my attention to this; see also Jorge's comment on this post), or manually zipping.

My way

I usually output from InDesign and then unzip to make fixes and changes. Currently when I need to re-zip, this is the code I use:
zip -Xr9D ../book.epub mimetype META-INF OEBPS -x *.DS_Store
Note: it is important that prior to running this code you change the current directory you are in, so that you are inside the main folder of the EPUB you wish to zip. The code will then create the EPUB just outside the folder, i.e. in the folder (or directory) that is the parent of the unzipped EPUB folder.

By way of explanation, the following options are selected in the creation of the EPUB (see zip manual for more details):
-X: Do not save extra file attributes (Extended Attributes on OS/2, uid/gid and file times on Unix).
-r: Travel the directory structure recursively.
-9: "The maximum compression option -9 also attempts compression on all files regardless of extension."
-D: Do not create entries in the zip archive for directories.
An order for the contents to be arranged in is then provided and zip is told to exclude any pesky .DS_Store files.

Once is enough

With the initial zipping out of the way, the point of this post is to state that if you output the EPUB from an app and need to edit it, then you should only ever need to do the unzipping once, and should never need to do a full re-zipping unless you make some mistake using the Terminal.

Simply keep the EPUB and the expanded version side by side and as outlined below you'll be able to keep them manually synchronized. Likewise if you've constructed the EPUB without the assistance of an EPUB creation app, you should only ever need to zip into an EPUB package once.

Adding a file to a zip archive

Rather than unzipping every time you want to make edits and rezipping simply add to, or update files within, the EPUB package. This is simple to do because the normal behaviour of a zip command is to add. But of course within an EPUB we keep files in subfolders and you will want to add, for example, a file to the OEBPS and perhaps a subfolder of it. No problem:
zip -Xr9D ../book.epub . -i OEBPS/chapter.xhtml
Or if we want to update an existing file we add -u for update:
zip -uXr9D ../book.epub . -i OEBPS/chapter.xhtml
And for a folder/directory:
zip -uXr9D ../book.epub . -i OEBPS/* -x *.DS_Store
Remember we are always running this code from within the top-level folder of the EPUB, the one that contains the mimetype, the OEBPS and the META-INF folders.

Make it easy on yourself: One line of code to update everything

Commonly, while we can specify updates to specific files or folders, it's much less trouble to run an update across the entire EPUB:
zip -uXr9D ../book.epub -x *.DS_Store
This command will very quickly check the differences between the uncompressed folders and our zipped EPUB and make changes and additions automatically, excluding of course those pesky DS_Store files. It is much quicker than overwriting the whole EPUB and if there are other files that you wish to exclude, place those after the -x too for exclusion.

Deleting a file

Deleting a file manually:
zip -d ../book.epub OEBPS/chapter.xhtml

Examining the archive contents

To check that all these commands are doing what we want them to do we can examine the archive using the unzip program. Don't worry, we don't actually need to unzip to see the contents, simply run:
unzip -l ../book.epub

But why?

For someone unused to using the macOS Terminal, this all seems like a pain to be doing, so why engage with the command line? Well, you've probably been engaging reluctantly anyway with zip and unzip. But doing it this way means that you can have an uncompressed version of your EPUB that you work on, which makes everything easier: adding files, working on them, and previewing them  in your favourite text editors, browsers, etc.

It also changes your mindset that you are working on the compressed EPUB, so instead of having a growing list of unzipped versions of your EPUB that you lose track of, or keep dragging to the trash, you have one uncompressed EPUB, and one zipped EPUB, and you work from there. This also saves time, because compressing and decompressing does take a few moments. And if something goes wrong, if you make a mistake adding, updating or deleting from the EPUB, you can simply delete it and start again from scratch. So there's no need to worry when experimenting with zip commands and getting used to the syntax.

Comments

  1. You're not wrong on the versioning / zipping front, and this is a nifty trick, but seems more trouble than it's worth if one can just break into the code via Sigil or the like.

    ReplyDelete
    Replies
    1. Thank you for your comment, I've used Sigil in the past and used to wholly rely on it. This post is really a stripping back to the absolute basics and amending EPUBs in the purest possible way. Of use to some I hope.

      Delete

Post a Comment