3am is when the world is most interesting.
Random header image... Refresh for more!

Zipping up OOXML files

For future records, when creating OOXML files from scratch, make sure to use the ‘-D’ option to not store directory entries in the ZIP archive. This makes the Office apps choke (at least on OS X).

Here’s an example of an Excel created XLSX file:
Monstar:n adam$ unzip -l finances
Archive: finances
Length Date Time Name
-------- ---- ---- ----
1432 01-01-80 00:00 [Content_Types].xml
588 01-01-80 00:00 _rels/.rels
972 01-01-80 00:00 xl/_rels/workbook.xml.rels
828 01-01-80 00:00 xl/workbook.xml
5547 01-01-80 00:00 xl/styles.xml
6646 01-01-80 00:00 xl/worksheets/sheet2.xml
7562 01-01-80 00:00 xl/theme/theme1.xml
22807 01-01-80 00:00 xl/worksheets/sheet1.xml
5595 01-01-80 00:00 xl/sharedStrings.xml
2375 01-01-80 00:00 xl/calcChain.xml
615 01-01-80 00:00 docProps/core.xml
829 01-01-80 00:00 docProps/app.xml
-------- -------
55796 12 files


To recreate that after editting, use the following command:
Monstar:n adam$ zip -rD finances.xlsx *
adding: [Content_Types].xml (deflated 75%)
adding: _rels/.rels (deflated 60%)
adding: docProps/app.xml (deflated 52%)
adding: docProps/core.xml (deflated 49%)
adding: xl/_rels/workbook.xml.rels (deflated 73%)
adding: xl/calcChain.xml (deflated 77%)
adding: xl/sharedStrings.xml (deflated 86%)
adding: xl/styles.xml (deflated 85%)
adding: xl/theme/theme1.xml (deflated 79%)
adding: xl/workbook.xml (deflated 46%)
adding: xl/worksheets/sheet1.xml (deflated 82%)
adding: xl/worksheets/sheet2.xml (deflated 76%)

This will yield a file like:

Monstar:n adam$ unzip -l finances.xlsx
Archive: finances.xlsx
Length Date Time Name
-------- ---- ---- ----
1432 01-01-80 00:00 [Content_Types].xml
588 01-01-80 00:00 _rels/.rels
829 01-01-80 00:00 docProps/app.xml
615 01-01-80 00:00 docProps/core.xml
972 01-01-80 00:00 xl/_rels/workbook.xml.rels
2375 01-01-80 00:00 xl/calcChain.xml
5595 01-01-80 00:00 xl/sharedStrings.xml
5547 01-01-80 00:00 xl/styles.xml
7562 01-01-80 00:00 xl/theme/theme1.xml
828 01-01-80 00:00 xl/workbook.xml
22807 01-01-80 00:00 xl/worksheets/sheet1.xml
6646 01-01-80 00:00 xl/worksheets/sheet2.xml
-------- -------
55796 12 files


If you skip the ‘-D’ option, you end up with a zip file that looks like this:

Monstar:n adam$ unzip -l finances2.xlsx
Archive: finances2.xlsx
Length Date Time Name
-------- ---- ---- ----
1432 01-01-80 00:00 [Content_Types].xml
0 06-19-10 04:05 _rels/
588 01-01-80 00:00 _rels/.rels
0 06-19-10 04:05 docProps/
829 01-01-80 00:00 docProps/app.xml
615 01-01-80 00:00 docProps/core.xml
16222 06-19-10 04:13 finances
13477 06-19-10 04:08 finances.xlsx
0 06-19-10 04:05 xl/
0 06-19-10 04:05 xl/_rels/
972 01-01-80 00:00 xl/_rels/workbook.xml.rels
2375 01-01-80 00:00 xl/calcChain.xml
5595 01-01-80 00:00 xl/sharedStrings.xml
5547 01-01-80 00:00 xl/styles.xml
0 06-19-10 04:05 xl/theme/
7562 01-01-80 00:00 xl/theme/theme1.xml
828 01-01-80 00:00 xl/workbook.xml
0 06-19-10 04:05 xl/worksheets/
22807 01-01-80 00:00 xl/worksheets/sheet1.xml
6646 01-01-80 00:00 xl/worksheets/sheet2.xml
-------- -------
85495 20 files

All of those ‘0′ length directory entries make Excel puke.

Additionally, note that you have to add the file extension ‘.xlsx’ (or appropriate for your OOXML file type) in order for Office to open the file after you’ve created it (even though the Office created files don’t have extensions by default). I think there is a way to add metadata that determines the MIME type, and would solve this problem, but that may be a OS X Office problem only.

0 comments

There are no comments yet...

Kick things off by filling out the form below.

Leave a Comment