gzip file problem



  • I've got a small gzip compressed file called Lcomp.py
    The file is valid & I can print it in both it's compressed & uncompressed formats

    with open('Lcomp.py', 'r') as f: m=f.read(); print(); print('compressed L:'); s=repr(m); print(s); print(len(s), 'bytes')
    import uzlib; from uio import StringIO as s; u=uzlib.DecompIO(s(m),31); m=u.read(); print(); print('decomp: '); print(m); print(len(m), 'bytes')
    if '\x1f\x08' in m:  import uzlib; from uio import StringIO as s; u=uzlib.DecompIO(s(m),31); m=u.read(); print(); print('if test decomp: '); print(m); print(len(m), 'bytes')
    

    gives

    compressed L:
    '\x1f\x08\x08v<Ia\x04\x00L.py\x00]\r\n0\x10\uf17e\x03\x14\x0b*\x04?b\t\x1bx\x0c-\x05 4\U0001102e\x12S\x12\x07o"\x0b\x0b\x03\x001\x0cHy9\x08dg\x011+\x05d\x04\x18Vq\\$\x1b)\x07\x01]\x150(~\u64bc\u0379FkF7!kH,lrU\x0ee\x01a#2X\x03.\x0e\x06W\x10+\x0fmZ\x13\u02b1\x0e\x12\x1b\u05e0\x0f\x0132\x11!<%F\x08\x1c:\x05\x1685\x01K\x04zsU\x04\x03\x08~v\x13\x1b\x06\U0002f6ff\x1cn>\\I\x07\x00\x00\x00'
    386 bytes
    
    decomp: 
    b'[[72, 86, 148], ["  if len(L)!=5: TxL.append([\'wrong list length\', len(L)); return\\r", "  print(); print(\'new file:\'); print(nf); with open(\'newfile\', \'w\') as f: f.write(nf)\\r", "            if \'[\' in m: TxL.append(m)\\r"], [72, 86, 148], [], []]\r\n'
    247 bytes
    

    What I don't understand is why it fails to print a second time after I test it for the compression header '\x1f\x08' ? Any body see what I'm doing wrong?



  • @kjm said in gzip file problem:

    if '\x1f\x08'

    what if you write

    if b'\x1f\x08' in
    


  • OK, I redid

    def _decomp(c):
      import uzlib
      from uio import StringIO as s
      u=uzlib.DecompIO(s(c),31)
      n=u.read()
      print(); print('decomp: ')
      print(n)
      print(len(n), 'bytes')
    
    
    with open('Lcomp.py', 'r') as f: m=f.read()
    print(); print('compressed L:')
    s=repr(m); print(s)
    print(len(s), 'bytes')
    
    _decomp(m)
    if '\x1f\x08' in m:  _decomp(m)
    

    but my problem persists

    compressed L:
    '\x1f\x08\x08v<Ia\x04\x00L.py\x00]\r\n0\x10\uf17e\x03\x14\x0b*\x04?b\t\x1bx\x0c-\x05 4\U0001102e\x12S\x12\x07o"\x0b\x0b\x03\x001\x0cHy9\x08dg\x011+\x05d\x04\x18Vq\\$\x1b)\x07\x01]\x150(~\u64bc\u0379FkF7!kH,lrU\x0ee\x01a#2X\x03.\x0e\x06W\x10+\x0fmZ\x13\u02b1\x0e\x12\x1b\u05e0\x0f\x0132\x11!<%F\x08\x1c:\x05\x1685\x01K\x04zsU\x04\x03\x08~v\x13\x1b\x06\U0002f6ff\x1cn>\\I\x07\x00\x00\x00'
    386 bytes
    
    decomp: 
    b'[[72, 86, 148], ["  if len(L)!=5: TxL.append([\'wrong list length\', len(L)); return\\r", "  print(); print(\'new file:\'); print(nf); with open(\'newfile\', \'w\') as f: f.write(nf)\\r", "            if \'[\' in m: TxL.append(m)\\r"], [72, 86, 148], [], []]\r\n'
    247 bytes
    

    only 1 print



  • @kjm
    you have overridden value of m in second line
    so in third line it is not the same operation as you decompress already decompressed value.

    And really better if you follow guaidance of writing python code, as you write all in one line.
    Python designed indents to have more understandable code at first look.


Log in to reply
 

Pycom on Twitter