Skip to content

Instantly share code, notes, and snippets.

@joel-wright
Created April 21, 2017 16:06
Show Gist options
  • Save joel-wright/b8282f689ff1a622c035cd2741e8e265 to your computer and use it in GitHub Desktop.
Save joel-wright/b8282f689ff1a622c035cd2741e8e265 to your computer and use it in GitHub Desktop.
import base64
import json
import tarfile
import time
files = {
'c/o': {'name': 'tox.ini', 'size': 3935},
'c/o2': {'name': 'setup.cfg', 'size': 4396}
}
def tar_padding(sz):
_bs = tarfile.BLOCKSIZE
return '\x00' * (_bs - (sz - 1) % _bs - 1)
def tar_header(v):
info = tarfile.TarInfo(name=v['name'])
info.size = v['size']
info.type = tarfile.REGTYPE
info.mode = 0664
info.mtime = time.time()
return info.tobuf(format=tarfile.GNU_FORMAT)
print json.dumps([{
'path': k,
'preamble': base64.b64encode(tar_header(v)),
'postamble': base64.b64encode(tar_padding(v['size']))
} for k, v in files.items()] + [{'path': 'c/__end_tar'}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment