Compression module APIs

Chris Rebert clp2 at rebertia.com
Thu May 6 07:52:30 EDT 2010


On Thu, May 6, 2010 at 4:41 AM, Florian Weimer <fw at deneb.enyo.de> wrote:
> * Chris Rebert:
>> On Thu, May 6, 2010 at 4:09 AM, Florian Weimer <fw at deneb.enyo.de> wrote:
>>> As far as I can see, the compression-related APIs (gzip, zlib, bzip2)
>>> in Python 2.5 have three distinct APIs.  Is there really no unified
>>> interface, or am I missing something?
>>
>> bz2.BZ2File and gzip.GzipFile both offer a file-like interface for
>> reading/writing compressed files in their respective formats.
>> The gzip module is already built on top of the zlib module, so there's
>> no ZlibFile.
>> zlib and bz2 also both offer compatible one-shot compress() and
>> decompress() functions.
>> So, the interfaces are sorta "unified", although it is true they're
>> not grouped into a single generic "compression" module.
>
> Some overlap is there.  But but there does not appear to be a way to
> decompress a gzip stream on the fly (which is surprising, as this is a
> fairly common operation), and there are no counterparts to
> bz2.BZ2{Dec,C}ompressor.

Well, there are zlib equivalents:
http://docs.python.org/library/zlib.html#zlib.decompressobj
http://docs.python.org/library/zlib.html#zlib.compressobj

As for gzip, you might be able to pass a StringIO object as the
`fileobj` argument to zip.GzipFile's constructor; that'd give you
basically the same thing as a Gzip{Dec,C}ompressor. I haven't tried
it, but there's a decent chance it might work.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list