#! /usr/bin/env python3

#
# bindatrec.py
# Working with binary data record layouts.
#
# Author: Billy Wilson Arante
# Created: 03/29/2016 PHT
# Citation: https://docs.python.org/3.4/tutorial/stdlib2.html
#

def struct_unpack():
    """The struct module, pack() and unpack() functions."""

    import struct

    with open('text.zip', 'rb') as f:
        data = f.read()
        
        print(data) # Display data's content
        print()

    start = 0

    for i in range(3):
        start = start + 14
        fields = struct.unpack('<IIIHH', data[start:start+16])
        
        print(fields) # Display field's content

        crc32, comp_size, uncomp_size, fnsize, extra_size = fields

        start = start + 16
        fname = data[start:start+fnsize]
        
        start = start + fnsize
        extra = data[start:start+extra_size]

        print(fname, hex(crc32), comp_size, uncomp_size)

        start = start + extra_size + comp_size

        print()


def main():
    """The program itself."""

    struct_unpack()


if __name__ == '__main__':
    main()
