Skip to content

Instantly share code, notes, and snippets.

@tipabu
Created April 21, 2017 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tipabu/5c96175d26fac1ec1771b5d01c6573a7 to your computer and use it in GitHub Desktop.
Save tipabu/5c96175d26fac1ec1771b5d01c6573a7 to your computer and use it in GitHub Desktop.
import base64
import json
files = {'c/o': {'name': 'tox.ini', 'size': 3935},
'c/o2': {'name': 'setup.cfg', 'size': 4396}}
def tar_padding(sz):
return '\x00' * (512 - (sz - 1) % 512 - 1)
def tar_header(v):
buf = []
buf.append(v['name'][:100])
buf.append('\x00' * (100 - len(v['name'][:100])))
buf.append('0000640\x00') # mode
buf.append(2 * '0000000\x00') # owner/group
buf.append('%011o\x00' % v['size'])
buf.append('%011o\x00' % 0) # last-modified
buf.append(' ' * 8) # checksum placeholder
buf.append('0')
buf = ''.join(buf)
chksum = sum(ord(c) for c in buf)
buf = buf[:148] + ('%06o\x00 ' % chksum) + buf[156:]
buf += tar_padding(len(buf))
assert len(buf) == 512, len(buf)
return buf
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