Python IOError:不是Gzip文件(Gzip和Blowfish Encrypt/Compress)

2024-04-29 10:09:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python的内置库gzip有一些问题。几乎查遍了所有其他关于它的问题,但似乎都不起作用。

我的问题是,当我尝试减压时,我会得到IOError

我得到:

Traceback (most recent call last):
File "mymodule.py", line 61, in
  return gz.read()
File "/usr/lib/python2.7/gzip.py", line 245,
  readself._read(readsize)
File "/usr/lib/python2.7/gzip.py", line 287, in
  _readself._read_gzip_header()
File "/usr/lib/python2.7/gzip.py", line 181, in
  _read_gzip_header
raise IOError, 'Not a gzipped file'IOError: Not a gzipped file

这是我通过网络发送它的代码,它可能不理解我为什么要做这些事情,但它通常是在一段时间的循环和内存效率,我只是简化了它。

buffer = cStringIO.StringIO(output) #output is from a subprocess call
small_buffer = cStringIO.StringIO()
small_string = buffer.read() #need a string to write to buffer 
gzip_obj = gzip.GzipFile(fileobj=small_buffer,compresslevel=6, mode='wb')
gzip_obj.write(small_string)
compressed_str = small_buffer.getvalue()

blowfish = Blowfish.new('abcd', Blowfish.MODE_ECB)
remainder = '|'*(8 - (len(compressed_str) % 8))
compressed_str += remainder
encrypted = blowfish.encrypt(compressed_str)
#i send it over smb, then retrieve it later

然后这是检索它的代码:

#buffer is a cStringIO object filled with data from  retrieval
decrypter = Blowfish.new('abcd', Blowfish.MODE_ECB)
value = buffer.getvalue()
decrypted = decrypter.decrypt(value)
buff = cStringIO.StringIO(decrypted)
buff.seek(0)
gz = gzip.GzipFile(fileobj=buff)
return gz.read()

这就是问题所在

return gz.read()


Tags: inpyreadreturnbufferlinecompressedfile