我正在尝试使用 python bz2compressor 类来压缩数据流。
文档说 bz2compressor.compress()
应该“尽可能”返回压缩数据 block ,但我什么也没得到
当我 flush()
时,我得到了所有压缩数据(我已经尝试过使用 2GB+ 的文件)仍然什么也没有。
有没有办法可以将任何内部缓冲区限制设置为何时返回数据。
谢谢!
最佳答案
I get ALL my compressed data when I flush() (I have tried with files 2GB+) still nothing.
使用压缩器有一个技巧。
我敢打赌,你的 2GB+ 文件不是很随机。随机数据压缩得不好。有序数据被压缩到非常小的尺寸。
例如
>>> import bz2
>>> c=bz2.BZ2Compressor()
>>> import string
>>> data = string.printable*1024
>>> len(data)
102400
>>> c.compress(data)
''
>>> result= c.flush()
>>> len(result)
361
提供的数据有一个模式,这使得它可以很好地压缩。
您需要随机数据。
>>> import random
>>> c=bz2.BZ2Compressor()
>>> size= 0
>>> result= ''
>>> while result == '':
... data = ''.join( random.choice(string.printable) for i in xrange(1024*8) )
... size += len(data)
... result= c.compress(data)...
>>> len(result)
754809
>>> size
901120
当我使用真正随机的数据时,我会得到 block 。
关于Python BZ2 压缩机 - compress() 不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8246769/