diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-09-12 16:01:12 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-09-12 16:01:12 +0100 |
| commit | 470ce2b5f07ed0191842db93bd5b653099c00aff (patch) | |
| tree | 630ff60f2536b53a8f5642752f885dabaf280ee6 /mammoth/zips.py | |
| parent | 224b5cf9c8b0fda519bcb11d8b8ac45775a3981b (diff) | |
Extract zips module
Diffstat (limited to 'mammoth/zips.py')
| -rw-r--r-- | mammoth/zips.py | 28 |
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 |
