def write_header(f, fn):
    '''
    100	 name	 name of file
    8	 mode	 file mode
    8	 uid	 owner user ID
    8	 gid	 owner group ID
    12	 size	 length of file in bytes
    12	 mtime	 modify time of file
    8	 chksum	 checksum for header
    1	 link	 indicator for links
    100	 linkname	 name of linked file
    '''
    def rpad(s, size):
        L = len(s)
        return s + (size - L) * '\0'
        
    header  = rpad(fn, 100) 
    header += rpad('%o' % stat(fn).st_mode, 8)
    header += rpad('%o' % stat(fn).st_uid, 8)
    header += rpad('%o' % stat(fn).st_gid, 8)
    size = getsize(fn) if isfile(fn) else 0
    header += rpad('%o' % size, 12)
    header += rpad('%o' % stat(fn).st_mtime, 12)
    header += 8 * '\0' # 8 zeros while the cksum is computed
    if islink(fn): header += 1 * '2'
    elif isfile(fn): header += 1 * '0'
    elif isdir(fn): header += 1 * '5'
    if islink(fn): header += rpad(os.readlink(fn), 100)
    else: header += 100 * '\0'

    # the checksum part is shamelessy stolen from the tarfile module 
    # with little edit
    cksum = 256 + sum(ord(h) for h in header)
    header = rpad(header, 512)
    header = header[:-364] + '%06o\0' % cksum + header[-357:]

    f.write( header )

    100	 name	 name of file
    8	 mode	 file mode
    8	 uid	 owner user ID
    8	 gid	 owner group ID
    12	 size	 length of file in bytes
    12	 mtime	 modify time of file
    8	 chksum	 checksum for header
    1	 link	 indicator for links
    100	 linkname	 name of linked file
    
recipes/Python/577422_Creating_tar_archive_without_using_tarfile/recipe-577422.py