Sunday, July 17, 2011

Keeping the DS_Store file out while zipping an EPUB (and zipping EPUB on PC)

This is long overdue; it has been a very busy summer. I think I tweeted this a while back, but it clearly needs a more permanent home.

My EPUB Straight to the Point book explains how to use Terminal to zip EPUB files in two steps, first by adding the mimetype file with no compression, and second by adding the rest of the files with compression.

Unfortunately, there is an invisible file called DS_Store in every Mac OS folder that also gets added and that later generates validation errors (since it is present, but not declared in the manifest).

To remedy this problem, use the first zip line as usual:

zip -0Xq file.epub mimetype

(that is a number 0, not a letter O, and of course, use whatever filename for the EPUB file that you like)

But, to keep those pesky DS_Store files from being erroneously added to your EPUB file, you should modify the second line so it looks like this:

zip -Xr9Dq file.epub * -x *.DS_Store

which basically says, compress all the rest of the files and folders into the file.epub file, but omit DS_Store files, wherever they may be.

And I can't remember who tweeted my latest favorite shortcut, but it's a good one. In Terminal, use the up arrow to repeat an earlier command line. Then press Return to execute.

Finally, if you're on a PC, you should check out the folder2zip utility created by Gerald Schmidt and his colleagues at the Open University. To use it, type folder2zip C:\path\to\my\epub\folder epub


  1. There's no need to use the terminal on the Mac, just use the zip/unzip AppleScripts that can be downloaded via the link below. They already used this technique.

  2. The only reason is a perhaps perverse need to control every bit of the process. With Terminal, I know exactly what I'm doing and what I'm getting and don't have to rely on anyone else's code. I do so much troubleshooting that I don't want any more variables than absolutely necessary. Once I get a few minutes (ha!) I'd like to test these tools to make sure they're as reliable as you all say. But the few times I have taken the time, I have gotten weird errors and inconsistencies. Maybe it's just me. Thanks for offering the scripts, I look forward to looking at them.

  3. On Windows 7 there is no need for a third party zip utility, but I'm getting the following error with a file zipped using XP:

    ERROR: epub test xp.epub: length of first filename in archive must be 8, but was 22

    I don't have a Vista machine handy to try it out on.

  4. You can combine the two zip lines. The File metadata is to short for compression. We exclude all Files beginning with a dot.

    zip -Xr9D -x.* mimetype *

    We have published more than 1500 epub's in this way.


  5. Christian KirchhoffJuly 18, 2011 at 4:24 AM

    In an EPUB file container, the file mimetype must be the first and it must be uncompressed.

    "length of first filename in archive must be 8, but was 22" might express that the first file found in the container was not "mimetype".

    Does folder2zip respect that rule?

  6. I totally understand about wanting to control things, but using the terminal for something like this is a waste of valuable time. It's the perfect thing to automate with a script. It's still using the command line behind the scenes AND you can just look at the AppleScript code to see exactly what it's doing. So you get the control and speed! If you want to change the commands, just edit the script to do what you want. Then the SCRIPT does the work instead of you. That's what computers are for right? Best wishes all...

  7. Dan,

    Your scripts rock. I owe you several frosty beers.


  8. Here's another set of AppleScripts to pack/unpack ePub files:

  9. Seriously, who is the jerk who decreed that the mimetype file must be uncompressed? It's an extremely short file, and it is to be located in a .zip (compressed) file. I really want to know why this is a requirement.


More of my books