OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to decomress a long byte stream using python?

  • Thread starter Thread starter zmz
  • Start date Start date
Z

zmz

Guest
I tried to decompress a long byte stream using python's zlib, but it always stopped at 65536.

My initial code was:

Code:
data = zlib.decompress(base64.b64decode(rawdata)[14:])

The return data is correct, but trucated to at most 65536 bytes.

I then tried to search on Stackoverflow, and found several possible solutions, but none worked.

Solution 1, use zlib.decompressobj

Code:
data = zlib.decompressobj().decompress(base64.b64decode(rawdata)[14:])

It's truncated at 65536 too.

Solution 2, use Subprocess to open gunzip process to decompress

Code:
def gunzip(string):
    proc = subprocess.Popen('gunzip',stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=False)
    proc.stdin.write(string)
    proc.stdin.close()
    body=b''
    while True:
        output = proc.stdout.read()
        if len(output) > 0:
            body += output
        else:
            error_msg = proc.poll()
            if error_msg is not None:
                break
    return body

It doesn't work, the output is always empty (so is the body) and error_msg returns 1.

Can anyone help?

Thanks.
<p>I tried to decompress a long byte stream using python's zlib, but it always stopped at 65536.</p>
<p>My initial code was:</p>
<pre><code>data = zlib.decompress(base64.b64decode(rawdata)[14:])
</code></pre>
<p>The return data is correct, but trucated to at most 65536 bytes.</p>
<p>I then tried to search on Stackoverflow, and found several possible solutions, but none worked.</p>
<p>Solution 1, use zlib.decompressobj</p>
<pre><code>data = zlib.decompressobj().decompress(base64.b64decode(rawdata)[14:])
</code></pre>
<p>It's truncated at 65536 too.</p>
<p>Solution 2, use Subprocess to open gunzip process to decompress</p>
<pre><code>def gunzip(string):
proc = subprocess.Popen('gunzip',stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=False)
proc.stdin.write(string)
proc.stdin.close()
body=b''
while True:
output = proc.stdout.read()
if len(output) > 0:
body += output
else:
error_msg = proc.poll()
if error_msg is not None:
break
return body
</code></pre>
<p>It doesn't work, the output is always empty (so is the body) and error_msg returns 1.</p>
<p>Can anyone help?</p>
<p>Thanks.</p>
 

Latest posts

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top