1

I want to decompress this string "\x1F\x8B\x08\x00\x00\x00\x00\x00\x00\x00UP]k\xC3 \x14\xFD+\xC3\xE7\xCD\xA8\xF9X\xE2\xEBX\xA1\x0CF\x1F\xBA\xEE%\x10\xAC\xB1\xAD\xC4h\x88f%\x8C\xFD\xF7]\x1B\xDA\xAD\xF8\xE29\xE7z\xEE9~#\xE7\x11G\xAF\xBB\x1C=\x22\xDFv_j\x04H+@\xBAW\x1A\xEEe\x91>SF\x18+i\x9Ef\x04\x84\xA1;\x02/]\x8F\xA5Q\xC2\xF6\xC2\x075\xE2\xFE8\x89\xB1m\xA4\x85\x89V\xFB\xC1\x88\x19\xA6\xDE\xB6\x1Fe\xB6y\x08\xCA\x87\xA7N\xCD\x1E\xC4^H\x10\xF6\x19'\x19/\x14g\x92K\xC1[\x06\xCA\xB2\x9Ca\x82K@\x07m\x8F\x17B\x98\xC1\xD7\xC9a2\xA6\xD9W\xA4j\xBAI\x9E\x84\xAB\x93\x7F\x80g\x18N\x9D,\xEB\xEA\x84fyJIAI\xCE'\xAF\xC6:\xB9\x0B\xE0\xF6\xDA\xA8\x95qg0\x8FE\x87Ke\x86iQbZU\x98\x924\xD6\x1C];\xC9\xB0n\xA3Jhd\x8C\x08\xB7\xCF\x1AN\xCE\xAA-|R\x94\xB3\x82\xA6\xE0\x902v\x19\xB4l\xE7!\x9F\xEB\xD5\x1A\x88\xB3>\xE8\xBF\x85\xC1u\xCA\x22n\xA1\x11\xA4\x99wj|\x17\x8B\x0F\x86\xF2\x8D\x8C\xE5\x85\x0Cn\x9Co\xDBt\xEF\xF5\xF2X\x1A\xADlx9\x09k\x95\xB9\x9A\xC8+DtI\xB0\xD116\xFA\xF9\x05\xBAs\xAET\xE0\x01\x00\x00"

this string is compress by gzip encoding, i want decompress this ,so i save this string in the file and read file to decompress that is not work ,otherwise i use the python interpreter mode and paste the string in the command line the code is work

i suppose this is the character-set issue but i can't fix it !so i hope someone give me a solution! my codes is blow ,and the work environment is linux.

  import zlib

  file_obj=open("compress_data.log","r+")

  for gz_data in file_obj.readlines():

       print gz_data

       decompressed_data = zlib.decompress(gz_data, 16+zlib.MAX_WBITS)

       print decompressed_data
liyang
  • 21
  • 1
  • 5

1 Answers1

3

That data is in "gzip" format. Try using Python's built-in gzip library.

If the origin of that data is a disk file, try this:

import gzip

for data in gzip.GzipFile("compress_data.log"):
    print data

If the origin of that data is a Python string, try this:

import gzip
import StringIO

compressed_data = "\x1F\x8B\x08\x00\x00\x00\x00\x00\x00\x00UP]k\xC3 \x14\xFD+\xC3\xE7\xCD\xA8\xF9X\xE2\xEBX\xA1\x0CF\x1F\xBA\xEE%\x10\xAC\xB1\xAD\xC4h\x88f%\x8C\xFD\xF7]\x1B\xDA\xAD\xF8\xE29\xE7z\xEE9~#\xE7\x11G\xAF\xBB\x1C=\x22\xDFv_j\x04H+@\xBAW\x1A\xEEe\x91>SF\x18+i\x9Ef\x04\x84\xA1;\x02/]\x8F\xA5Q\xC2\xF6\xC2\x075\xE2\xFE8\x89\xB1m\xA4\x85\x89V\xFB\xC1\x88\x19\xA6\xDE\xB6\x1Fe\xB6y\x08\xCA\x87\xA7N\xCD\x1E\xC4^H\x10\xF6\x19'\x19/\x14g\x92K\xC1[\x06\xCA\xB2\x9Ca\x82K@\x07m\x8F\x17B\x98\xC1\xD7\xC9a2\xA6\xD9W\xA4j\xBAI\x9E\x84\xAB\x93\x7F\x80g\x18N\x9D,\xEB\xEA\x84fyJIAI\xCE'\xAF\xC6:\xB9\x0B\xE0\xF6\xDA\xA8\x95qg0\x8FE\x87Ke\x86iQbZU\x98\x924\xD6\x1C];\xC9\xB0n\xA3Jhd\x8C\x08\xB7\xCF\x1AN\xCE\xAA-|R\x94\xB3\x82\xA6\xE0\x902v\x19\xB4*l\xE7!*\x9F\xEB\xD5\x1A\x88\xB3>\xE8\xBF\x85\xC1u\xCA\x22n\xA1\x11\xA4\x99wj|\x17\x8B\x0F\x86\xF2\x8D\x8C\xE5\x85\x0Cn\x9Co\xDBt\xEF\xF5\xF2X\x1A\xADlx9\x09k\x95\xB9\x9A\xC8+DtI\xB0\xD116\xFA\xF9\x05\xBAs\xAET\xE0\x01\x00\x00"

compressed_data = StringIO.StringIO(compressed_data)

for data in gzip.GzipFile(fileobj=compressed_data):
    print data

If your data is stored in a disk file as ASCII text with Python escape sequences, then this program might work for you:

from gzip import GzipFile
from StringIO import StringIO

with open("compress_data.log") as file_obj:
    gz_data = file_obj.read().rstrip('\n')
unescaped_data = gz_data.decode('string_escape')
decompressed_data = GzipFile(fileobj=StringIO(unescaped_data)).read()
print decompressed_data

Note: if the above program works for you, your data is in a non-sensical format. This is probably the result of a bug in the program that produced the data.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308