Last active
March 8, 2020 20:37
-
-
Save narenaryan/2bdb58244c66f1610feb0e4252c53a56 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from zipfile import ZipFile, ZipInfo | |
| from io import BytesIO | |
| def update_or_insert(path, data): | |
| """ | |
| Param: path -> file in archive | |
| Param: data -> data to be updated | |
| Returns a new zip file with the updated content | |
| for the given path | |
| """ | |
| new_zip = BytesIO() | |
| with ZipFile('config.zip', 'r') as old_archive: | |
| with ZipFile(new_zip, 'w') as new_archive: | |
| for item in old_archive.filelist: | |
| # If you spot an existing file, create a new object | |
| if item.filename == path: | |
| zip_inf = ZipInfo(path) | |
| new_archive.writestr(zip_inf, data) | |
| else: | |
| # Copy other contents as it is | |
| new_archive.writestr(item, old_archive.read(item.filename)) | |
| return new_zip | |
| new_zip = update_or_insert( | |
| 'docker/docker-compose.yaml', | |
| b'docker-compose-file-content-new' | |
| ) | |
| # Flush new zip to disk | |
| with open('config.zip', 'wb') as f: | |
| f.write(new_zip.getbuffer()) | |
| new_zip.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment