compression - recompress useing python and zlib -


i trying decompress file make changes , compress again. decompressing seems work fine. need method compress compressed same way before.

here code:

import zlib  path = './input' pathout = './output'  def getint(intarray):     summe = 0     in range(len(list(intarray))):         summe += intarray[i]*256**i         return(summe)   print(path) inputfile = open(path, 'rb') header = {}  header.update({"intro":inputfile.read(28)}) print("intro",(header["intro"]))  key in ["header_size", "c_size", "header_v", "u_size", "blocks"]:     header.update({key:inputfile.read(4)})     print(key,getint(header[key]))   inputfile.seek(getint(header['header_size']))  blocks_count = getint(header['blocks']) data = [] in range(blocks_count):     block_header = {}     block_header.update({"c_size":inputfile.read(2)})     print("c_size",getint(block_header["c_size"]))     block_header.update({"u_size":inputfile.read(2)})     print("u_size",getint(block_header["u_size"]))     block_header.update({"checksum":inputfile.read(4)})     print("checksum",getint(block_header["checksum"]))     temp = inputfile.read(getint(block_header['c_size']))[2:-4]     data.append(zlib.decompressobj().decompress(b'x\x9c' + temp)) output = b'' inputfile.seek(0) output = inputfile.read(getint(header["header_size"])) inputfile.close()  compressor = zlib.compressobj(1)  block in data:     compressor.compress(block)     output += compressor.flush(zlib.z_sync_flush)  print("output length",len(output)) print("c_size",getint(header["c_size"])) outputfile = open(pathout, 'wb') outputfile.write(output) outputfile.close() 

when try decompress output says:

traceback (most recent call last):     file "c:\users\lsdesktop\desktop\bla - copy.py", line 45, in <module>         data.append(zlib.decompressobj().decompress(b'x\x9c' + temp)) zlib.error: error -3 while decompressing data: invalid stored block lengths 

here first 50 bytes including header of first block. ones how should (how before compressed):

b'b\x0c\x00 rwfyx\x01\xacy\tx\x14g\x16\xae\x9e)p\x00\r\x18\x15\xb9lk\xc7\x03\xaf\x88\x1a\x8c\x0c\xa8\x084(\x82\x17\x87\x17\x88\xa3\x8c\x1c;r\x0b' 

and first 50 bytes including header of first block after compressed it:

b'\xacy\tx\x14g\x16\xae\x9e)p\x00\r\x18\x15\xb9lk\xc7\x03\xaf\x88\x1a\x8c\x0c\xa8\x084(\x82\x17\x87\x17\x88\xa3\x8c\x1c;r\x0b\x82&\nk4\x1c\x89fq\x89' 

try z_finish instead of z_sync_flush. decompressing separate zlib streams, try make new single zlib stream never terminated. make separate zlib streams, need use z_finish each time. must use z_finish @ least once @ end, or never generate proper zlib stream.

as wrappers around that, if attempting write out same format read in, you're not doing that. need recreate header information, i.e. lengths , checksums. format documented? did copy code read input somewhere else?

this not problem, should not stripping zlib header , trailer [2:-4], , adding header b'x\x9c' +. in order decompress. decompress zlib stream directly.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -