Unzip and zip OpenOffice.org ODT files

As you probably know, ODT files of OpenOffice.org (LibreOffice) are in essence just ZIP archives.

It is very easy to unpack them. But it is not so easy to create them. If you simply zip the contents of unpacked ODT and change the extension to .odt, it will not work.

The key here is that the mimetype file shall come into the archive first.

Two simple bash scripts are given here to extract and then pack back the ODT files.


Script odt_extract (provide  .odt file name and desired destination directory as command line arguments):

echo "Extracting ODT file $1 to directory $2 ..."
unzip "$1" -d "$2"   #1>/dev/null


Script odt_compress (provide  source directory and desired .odt file name as command line arguments):

echo "Compressing contents of directory $1 into ODT compatible document $2 ... "
# Move into document source directory
cd "$1"
# Special zipping procedure to create correctly structured ODT file
# Remember that if the provided resulting file path is relative, it shall be so in respect to the current working directory!
# First comes the "mimetype" file ...
zip -0 -X "$2" mimetype   #1>/dev/null
# ... then everything else follows
zip -r "$2" * -x mimetype   #1>/dev/null

One Comment on “Unzip and zip OpenOffice.org ODT files”

  1. Andrzej says:

    On Linux:
    zip –update filename.odt content.xml

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.