Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to zip a WordprocessingML folder into readable docx

I have been trying to write a simple Markdown -> docx parser/writer, but am completely stuck with the last part, which should be the easiest: i.e. compressing the folder into a .docx that Word, or any other .docx reader, will recognize.

My parser-writer is irrelevant really: I have this problem if I simply unzip any old Word-produced *.docx and then try to recompress it with the usual compression utilities, giving it the file-ending docx. Is there some mysterious header I should be adding, or do I need a special OPC compression utility, or what?

I don't so much want a tool that will do this, as to figure out what is supposed to be there. It seems to be independent of the WordprocessingML specification.

Needless to say I don't know anything about compression. Everything I can find via Google has to do with fancy utilities you can use in business, but I'm making a little executable that would be GPLd or something, and should work on anything.

like image 622
Michael Avatar asked Oct 03 '09 15:10

Michael


3 Answers

The most common problem around manually zipping together Open XML documents is that it will not work if you zip the directory instead of the contents. In other words, the[content_types].xml file, and the word, docProps, and _rels directories need to reside at the root level of the zip file.

like image 86
Eric White Avatar answered Oct 08 '22 05:10

Eric White


Here are steps to unzip my.docx and re-zip:

% mkdir unzipped
% cd unzipped/
% unzip ../my.docx    
% zip -r ../rezipped.docx *
% open ../rezipped.docx 
like image 23
Sam Barnum Avatar answered Oct 08 '22 04:10

Sam Barnum


The compression algorithm used is "Zip" (Base 64) compression.

7zip seems to offer this, though i have no tested it.

like image 3
Mica Avatar answered Oct 08 '22 05:10

Mica