summaryrefslogtreecommitdiff
path: root/mammoth/zips.py
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-09-12 16:01:12 +0100
committerMichael Williamson <mike@zwobble.org>2015-09-12 16:01:12 +0100
commit470ce2b5f07ed0191842db93bd5b653099c00aff (patch)
tree630ff60f2536b53a8f5642752f885dabaf280ee6 /mammoth/zips.py
parent224b5cf9c8b0fda519bcb11d8b8ac45775a3981b (diff)
Extract zips module
Diffstat (limited to 'mammoth/zips.py')
-rw-r--r--mammoth/zips.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/mammoth/zips.py b/mammoth/zips.py
new file mode 100644
index 0000000..c6fe358
--- /dev/null
+++ b/mammoth/zips.py
@@ -0,0 +1,28 @@
+import contextlib
+
+from zipfile import ZipFile
+
+
+def open_zip(fileobj, mode):
+ return _Zip(ZipFile(fileobj, mode))
+
+
+class _Zip(object):
+ def __init__(self, zip_file):
+ self._zip_file = zip_file
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *args):
+ self._zip_file.close()
+
+ def open(self, name):
+ return contextlib.closing(self._zip_file.open(name))
+
+ def exists(self, name):
+ try:
+ self._zip_file.getinfo(name)
+ return True
+ except KeyError:
+ return False