问题描述
我试图在我的文件上写很多 zlib 块,有没有办法从我的文件中获取所有未压缩的内容?从下面的示例代码中,我只能获得我的第一个数据.提前感谢您的任何意见!
Python 3.6.8 |Anaconda, Inc.|(默认,2018 年 12 月 29 日,19:04:46)[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] 在达尔文输入帮助"、版权"、信用"或许可"以获取更多信息.>>>导入 zlib>>>str1 = b'我的第一个字符串'>>>str2 = b'我的第二个字符串'>>>z1 = zlib.compress(str1)>>>z2 = zlib.compress(str2)>>>使用 open('test.z', 'wb') 作为 fh:... fh.write(z1)... fh.write(z2)...2324>>>p = open('test.z','rb').read()>>>磷b'x\x9c\xcb\xadTH\xcb,*.Q(.)\xca\xccK\x07\x00.6\x05\xe6x\x9c\xcb\xadT(NM\xce\xcfKQ(.)\xca\xccK\x07\x003\xfb\x06:'>>>zlib.decompress(p)b'我的第一个字符串'>>>zlib.decompress(p)b'我的第一个字符串'
没有办法在单个函数或方法调用中解压缩串联的压缩流.但是,使用 zlib 包中的工具可以获得等效的功能.
>>>导入 zlib>>>b1 = b'这是一些数据'>>>b2 = b'这里还有一些数据'>>>流 = zlib.compress(b1) + zlib.compress(b2)>>>而流:... dco = zlib.decompressobj()... dec = dco.decompress(stream)... 打印(十进制)...流= dco.unused_data...b'这是一些数据'b'这里还有一些数据'zlib.decompressobj() 创建一个对象解压缩单个压缩流 - 如 zlib.decompress
- 并在其 unused_data 属性.所以我们可以循环解压,直到整个串联流都被解压.
至少,我找不到
I tried to write many zlib block on my file, is there a way I can get all uncompressed content from my file? from the example code below I can only get my first data. Thank you in advance for any input!
Python 3.6.8 |Anaconda, Inc.| (default, Dec 29 2018, 19:04:46)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import zlib
>>> str1 = b'my first string'
>>> str2 = b'my second string'
>>> z1 = zlib.compress(str1)
>>> z2 = zlib.compress(str2)
>>> with open('test.z', 'wb') as fh:
... fh.write(z1)
... fh.write(z2)
...
23
24
>>> p = open('test.z','rb').read()
>>> p
b'x\x9c\xcb\xadTH\xcb,*.Q(.)\xca\xccK\x07\x00.6\x05\xe6x\x9c\xcb\xadT(NM\xce\xcfKQ(.)\xca\xccK\x07\x003\xfb\x06:'
>>> zlib.decompress(p)
b'my first string'
>>> zlib.decompress(p)
b'my first string'
There isn't a way to decompress concatenated compressed streams in a single function or method call. However, it's possible to get equivalent functionality using the tools in the zlib package.
>>> import zlib
>>> b1 = b'Here is some data'
>>> b2 = b'Here is some more data'
>>> stream = zlib.compress(b1) + zlib.compress(b2)
>>> while stream:
... dco = zlib.decompressobj()
... dec = dco.decompress(stream)
... print(dec)
... stream = dco.unused_data
...
b'Here is some data'
b'Here is some more data'
zlib.decompressobj() creates an object which decompresses a single compressed stream - like zlib.decompress
- and exposes the remaining bytes in the stream in its unused_data attribute. So we can decompress in a loop until the entire concatenated stream has been decompressed.
At least, not that I could find
这篇关于python zlib 如何解压多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!