Zip `directory` recursively and return the archive base64-encoded. Entry names are relative to `root`: passing the directory itself puts its contents at the archive root, passing the parent keeps the directory as the archive's single top-level entry.
(directory: str, root: str)
| 109 | |
| 110 | |
| 111 | def _zip_directory(directory: str, root: str) -> str: |
| 112 | """Zip `directory` recursively and return the archive base64-encoded. |
| 113 | |
| 114 | Entry names are relative to `root`: passing the directory itself puts its |
| 115 | contents at the archive root, passing the parent keeps the directory as the |
| 116 | archive's single top-level entry. |
| 117 | """ |
| 118 | directory = os.path.normpath(directory) |
| 119 | buffer = BytesIO() |
| 120 | with zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED, strict_timestamps=False) as archive: |
| 121 | for base, _, files in os.walk(directory): |
| 122 | for name in files: |
| 123 | filename = os.path.join(base, name) |
| 124 | archive.write(filename, os.path.relpath(filename, root)) |
| 125 | return base64.b64encode(buffer.getvalue()).decode("utf-8") |
| 126 | |
| 127 | |
| 128 | def _encode_extension(path: str) -> str: |
no test coverage detected