Extract zlib compressed data from binary file in python

后端 未结 2 1537
失恋的感觉
失恋的感觉 2020-12-17 02:12

My company uses a legacy file format for Electromiography data, which is no longer in production. However, there is some interest in maintaining retro-compatibility, so I am

2条回答
  •  独厮守ぢ
    2020-12-17 03:13

    To start, why not scan the files for all valid zip streams (it's good enough for small files and to figure out the format):

    import zlib
    from glob import glob
    
    def zipstreams(filename):
        """Return all zip streams and their positions in file."""
        with open(filename, 'rb') as fh:
            data = fh.read()
        i = 0
        while i < len(data):
            try:
                zo = zlib.decompressobj()
                yield i, zo.decompress(data[i:])
                i += len(data[i:]) - len(zo.unused_data)
            except zlib.error:
                i += 1
    
    for filename in glob('*.mio'):
        print(filename)
        for i, data in zipstreams(filename):
            print (i, len(data))
    

    Looks like the data streams contain little-endian double precision floating point data:

    import numpy
    from matplotlib import pyplot
    
    for filename in glob('*.mio'):
        for i, data in zipstreams(filename):
            if data:
                a = numpy.fromstring(data, '

提交回复
热议问题