Skip to content

Instantly share code, notes, and snippets.

@manuelep
Last active December 26, 2015 15:49
Show Gist options
  • Save manuelep/7175473 to your computer and use it in GitHub Desktop.
Save manuelep/7175473 to your computer and use it in GitHub Desktop.
Python script with the aim of extracting and reconstructing OpenDocuments. Maybe it can be used for zip archive but it's not my focus now.
#!/usr/bin/python
# -*- coding: utf-8 -*-
# usage: $ odt.py [action] [filename.odf]
import zipfile, sys, os
action = sys.argv[1]
archname = sys.argv[2]
src = './%s.src' % '.'.join(archname.split('.')[:-1])
def extract():
if not os.path.exists(src):
os.makedirs(src)
with zipfile.ZipFile(archname, "r") as odt:
odt.extractall(src)
def compress():
with zipfile.ZipFile(archname, "w") as odt:
for dirname, subdirs, files in os.walk(src):
odt.write(dirname)
for filename in files:
filepath = os.path.join(dirname, filename)
odt.write(filepath, os.path.relpath(filepath, src))
if __name__ == "__main__":
if action == 'extract':
print 'Extracting %s to %s...' % (archname, src)
extract()
else:
print 'Creating %s file from content in %s...' % (archname, src)
compress()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment