From 470ce2b5f07ed0191842db93bd5b653099c00aff Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 12 Sep 2015 16:01:12 +0100 Subject: Extract zips module --- mammoth/zips.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 mammoth/zips.py (limited to 'mammoth/zips.py') 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 -- cgit v1.2.3