summaryrefslogtreecommitdiff
path: root/mammoth/zips.py
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-09-12 16:31:05 +0100
committerMichael Williamson <mike@zwobble.org>2015-09-12 16:31:05 +0100
commitc0a034ba83fb75031d839e49b699b8093432cfd0 (patch)
treea58b762f70cfbac777108a375ff8d93f9dde0d47 /mammoth/zips.py
parent4c88820f3d4778e75a4be50f7a2d78d03dc86316 (diff)
Support overwriting embedded style map
Diffstat (limited to 'mammoth/zips.py')
-rw-r--r--mammoth/zips.py28
1 files changed, 25 insertions, 3 deletions
diff --git a/mammoth/zips.py b/mammoth/zips.py
index 24c6b1c..432be5d 100644
--- a/mammoth/zips.py
+++ b/mammoth/zips.py
@@ -1,4 +1,6 @@
import contextlib
+import io
+import shutil
from zipfile import ZipFile
@@ -27,8 +29,28 @@ class _Zip(object):
except KeyError:
return False
- def write_str(self, name, value):
- self._zip_file.writestr(name, value)
-
def read_str(self, name):
return self._zip_file.read(name).decode("utf8")
+
+
+def update_zip(fileobj, files):
+ source = ZipFile(fileobj, "r")
+ try:
+ destination_fileobj = io.BytesIO()
+ destination = ZipFile(destination_fileobj, "w")
+ try:
+ names = set(source.namelist()) | set(files.keys())
+ for name in names:
+ if name in files:
+ contents = files[name]
+ else:
+ contents = source.read(name)
+ destination.writestr(name, contents)
+ finally:
+ destination.close()
+ finally:
+ source.close()
+
+ fileobj.seek(0)
+ destination_fileobj.seek(0)
+ shutil.copyfileobj(destination_fileobj, fileobj)