Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 79c5ef1

Browse files
committed
Issue #3488: Provide convenient shorthand functions gzip.compress
and `gzip.decompress`. Original patch by Anand B. Pillai.
1 parent 852823d commit 79c5ef1

5 files changed

Lines changed: 58 additions & 1 deletion

File tree

Doc/library/gzip.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ The module defines the following items:
8282
The *filename* argument is required; *mode* defaults to ``'rb'`` and
8383
*compresslevel* defaults to ``9``.
8484

85+
.. function:: compress(data, compresslevel=9)
86+
87+
Compress the *data*, returning a :class:`bytes` object containing
88+
the compressed data. *compresslevel* has the same meaning as in
89+
the :class:`GzipFile` constructor above.
90+
91+
.. function:: decompress(data)
92+
93+
Decompress the *data*, returning a :class:`bytes` object containing the
94+
uncompressed data.
95+
8596

8697
.. _gzip-usage-examples:
8798

@@ -112,6 +123,11 @@ Example of how to GZIP compress an existing file::
112123
f_out.close()
113124
f_in.close()
114125

126+
Example of how to GZIP compress a binary string::
127+
128+
import gzip
129+
s_in = b"Lots of content here"
130+
s_out = gzip.compress(s_in)
115131

116132
.. seealso::
117133

Lib/gzip.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import builtins
1111
import io
1212

13-
__all__ = ["GzipFile","open"]
13+
__all__ = ["GzipFile", "open", "compress", "decompress"]
1414

1515
FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16
1616

@@ -476,6 +476,23 @@ def readline(self, size=-1):
476476
return b''.join(bufs) # Return resulting line
477477

478478

479+
def compress(data, compresslevel=9):
480+
"""Compress data in one shot and return the compressed string.
481+
Optional argument is the compression level, in range of 1-9.
482+
"""
483+
buf = io.BytesIO()
484+
with GzipFile(fileobj=buf, mode='wb', compresslevel=compresslevel) as f:
485+
f.write(data)
486+
return buf.getvalue()
487+
488+
def decompress(data):
489+
"""Decompress a gzip compressed string in one shot.
490+
Return the decompressed string.
491+
"""
492+
with GzipFile(fileobj=io.BytesIO(data)) as f:
493+
return f.read()
494+
495+
479496
def _test():
480497
# Act like gzip; with -d, act like gunzip.
481498
# The input file is not deleted, however, nor are any other gzip

Lib/test/test_gzip.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,26 @@ def test_zero_padded_file(self):
265265
d = f.read()
266266
self.assertEqual(d, data1 * 50, "Incorrect data in file")
267267

268+
# Testing compress/decompress shortcut functions
269+
270+
def test_compress(self):
271+
for data in [data1, data2]:
272+
for args in [(), (1,), (6,), (9,)]:
273+
datac = gzip.compress(data, *args)
274+
self.assertEqual(type(datac), bytes)
275+
with gzip.GzipFile(fileobj=io.BytesIO(datac), mode="rb") as f:
276+
self.assertEqual(f.read(), data)
277+
278+
def test_decompress(self):
279+
for data in (data1, data2):
280+
buf = io.BytesIO()
281+
with gzip.GzipFile(fileobj=buf, mode="wb") as f:
282+
f.write(data)
283+
self.assertEqual(gzip.decompress(buf.getvalue()), data)
284+
# Roundtrip with compress
285+
datac = gzip.compress(data)
286+
self.assertEqual(gzip.decompress(datac), data)
287+
268288
def test_main(verbose=None):
269289
support.run_unittest(TestGzip)
270290

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ Neale Pickett
634634
Jim St. Pierre
635635
Dan Pierson
636636
Martijn Pieters
637+
Anand B. Pillai
637638
François Pinard
638639
Zach Pincus
639640
Michael Piotrowski

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ Extensions
9595
Library
9696
-------
9797

98+
- Issue #3488: Provide convenient shorthand functions ``gzip.compress``
99+
and ``gzip.decompress``. Original patch by Anand B. Pillai.
100+
98101
- Issue #8807: poplib.POP3_SSL class now accepts a context parameter, which is a
99102
ssl.SSLContext object allowing bundling SSL configuration options,
100103
certificates and private keys into a single (potentially long-lived)

0 commit comments

Comments
 (0)