- publishing free software manuals
An Introduction to Python
by Guido van Rossum and Fred L. Drake, Jr.
Paperback (6"x9"), 124 pages
ISBN 0954161769
RRP £12.95 ($19.95)

Sales of this book support the Python Software Foundation! Get a printed copy>>>

11.3 Working with Binary Data Record Layouts

The ‘struct’ module provides pack() and unpack() functions for working with variable length binary record formats. The following example shows how to loop through header information in a ZIP file (with pack codes "H" and "L" representing two and four byte unsigned numbers respectively):

    import struct

    data = open('myfile.zip', 'rb').read()
    start = 0
    for i in range(3): # show the first 3 file headers
        start += 14
        fields = struct.unpack('LLLHH', data[start:start+16])
        crc32, comp, uncomp, filenamesize, extra = fields

        start += 16
        filename = data[start:start+filenamesize]
        start += filenamesize
        extra = data[start:start+extra]
        print filename, hex(crc32), comp, uncomp

        start += extra + comp     # skip to the next header
ISBN 0954161769An Introduction to PythonSee the print edition