<s> from __future__ import ( <EOL> absolute_import , <EOL> print_function , <EOL> unicode_literals , <EOL> ) <EOL> import zipfile <EOL> from contextlib import contextmanager <EOL> from io import BytesIO <EOL> from pydocx . exceptions import MalformedDocxException <EOL> @ contextmanager <EOL> def ZipFile ( path , mode = '<STR_LIT:r>' ) : <EOL> try : <EOL> f = zipfile . ZipFile ( path , mode ) <EOL> except zipfile . BadZipfile : <EOL> raise MalformedDocxException ( '<STR_LIT>' ) <EOL> yield f <EOL> f . close ( ) <EOL> def create_zip_archive ( paths_to_data ) : <EOL> '''<STR_LIT>''' <EOL> archive = BytesIO ( ) <EOL> with ZipFile ( archive , '<STR_LIT:w>' ) as zf : <EOL> for arcname , data in paths_to_data . items ( ) : <EOL> if data is None : <EOL> continue <EOL> zf . writestr ( arcname , data . encode ( '<STR_LIT:utf-8>' ) ) <EOL> return archive </s>
