Created
January 6, 2020 23:14
-
-
Save matutter/d23532877ae9afebbce027c7de2b53af 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 tarfile import TarFile, TarInfo, open as OpenTarfile | |
| from tempfile import NamedTemporaryFile, TemporaryDirectory | |
| import shutil | |
| file_data = [ | |
| (b'1239EAD09123FCC', "/data/local/tmp/me"), | |
| (b'aaaaaaaaaaaaaaa', "/data/local/tmp/a"), | |
| (b'bbbbbbbbbbbbbbb', "/data/local/tmp/b") | |
| ] | |
| tar_name = None | |
| with TemporaryDirectory(prefix="xxx") as tmp_dir: | |
| with NamedTemporaryFile(delete=False, dir=tmp_dir) as fd: | |
| tar_name = fd.name | |
| print(f'Created {fd.name}') | |
| with OpenTarfile(fileobj=fd, mode='w:gz') as tar: | |
| for data, filename in file_data: | |
| with NamedTemporaryFile(delete=False, dir=tmp_dir) as in_fd: | |
| in_fd.write(data) | |
| in_fd.close() | |
| tar.add(in_fd.name, arcname=filename) | |
| shutil.copy(tar_name, "test.tar.gz") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment