forked from bat67/The-Python-Standard-Library-by-Example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgzip_BytesIO.py
More file actions
31 lines (25 loc) · 725 Bytes
/
Copy pathgzip_BytesIO.py
File metadata and controls
31 lines (25 loc) · 725 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python3
# encoding: utf-8
#
# Copyright (c) 2008 Doug Hellmann All rights reserved.
#
"""
"""
#end_pymotw_header
import gzip
from io import BytesIO
import binascii
uncompressed_data = b'The same line, over and over.\n' * 10
print('UNCOMPRESSED:', len(uncompressed_data))
print(uncompressed_data)
buf = BytesIO()
with gzip.GzipFile(mode='wb', fileobj=buf) as f:
f.write(uncompressed_data)
compressed_data = buf.getvalue()
print('COMPRESSED:', len(compressed_data))
print(binascii.hexlify(compressed_data))
inbuffer = BytesIO(compressed_data)
with gzip.GzipFile(mode='rb', fileobj=inbuffer) as f:
reread_data = f.read(len(uncompressed_data))
print('\nREREAD:', len(reread_data))
print(reread_data)