Opened 12 years ago

Last modified 8 years ago

#84 assigned new-feature

cromp -- pythonic cromp

Reported by: Fred T. Hamster Owned by: bugdock
Priority: minor Milestone:
Component: feistymeow-octopus Version:
Keywords: cromp Cc:

Description

http://docs.python.org/py3k/tutorial/stdlib2.html

the byte packing stuff in the struct module looks perfect.

python is good with binary records?

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 without using the zipfile module. Pack codes "H" and "I" represent two and four byte unsigned numbers respectively. The "<" indicates that they are standard size and in little-endian byte order:

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('<IIIHH', data[start:start+16])
crc32, comp_size, uncomp_size, filenamesize, extra_size = fields

start += 16
filename = data[start:start+filenamesize]
start += filenamesize
extra = data[start:start+extra_size]
print(filename, hex(crc32), comp_size, uncomp_size)

start += extra_size + comp_size # skip to the next header

Change History (1)

comment:1 by Fred T. Hamster, 8 years ago

Owner: changed from Fred T. Hamster to bugdock
Status: newassigned
Note: See TracTickets for help on using tickets.