gzip action module:
provides a similar interface for GNU zip file, which uses zlib to compress and decompress data.
1, write gzip compressed file
#!/usr/bin/env python3 # encoding: utf-8 import gzip import io import os out_file_name = "example.text.gz" with gzip.open(out_file_name, 'wb') as output: with io.TextIOWrapper(output, encoding='utf-8') as enc: enc.write('test gzip content') print(out_file_name, '包含的大小:{}bytes'.format(os.stat(out_file_name).st_size)) os.system('file -b --mime {}'.format(out_file_name))
Test results
[root@ mnt]# python3 gzip_write.py example.text.gz included size: 56bytes application/x-gzip; charset=binary [root@ mnt]# ll total 12 -rw-r--r-- 1 root root 56 Jan 1 09:16 example.text.gz -rw-r--r-- 1 root root 464 Jan 1 09:13 gzip_write.py
2, gzip compression level testing and appropriate settings
#!/usr/bin/env python3 # encoding: utf-8 import gzip import io import os import hashlib def get_hash(data): """返回md5值""" return hashlib.md5(data).hexdigest() # Reads the contents of the file, and copy out parts 1024 Data = Open ( ' content.txt ' , ' R & lt ' ) .read () * 1024 # Input file contents, return value md5 check_sum = get_hash (data.encode ( ' UTF-. 8 ' )) print('Level Size Checksum') print('----- ---------- ---------------------------------') print('data {:>5} {}'.format(len(data), check_sum)) for i in range(0, 10): file_name = 'compress-level-{}.gz'.format(i) with gzip.open(file_name, 'wb', compresslevel=i) as output: with io.TextIOWrapper(output, encoding='utf-8') as enc: enc.write(data) size = os.stat(file_name).st_size check_sum = get_hash(open(file_name, 'rb').read()) print(' {} {:>4} {}'.format(i, size, check_sum))
running result
[root@ mnt]# python3 gzip_compresslevel.py Level Size Checksum ----- ---------- --------------------------------- data 344064 03456cbea4852fa964775f38f03f2f2b 0 344 159 1b9895c8eaa94a0fdc5002d35fd44a93 . 1 3333 42f88491459c7a7028da1ffb5f2bdc82 2 3114 13771e090b90d4692a71079856256191 . 3 3114 f822f153e8b6da768f8b84559e75e2ca 4 1797 41e03538d99b3697db87901537e2577f # after most 4 . 5 1797 6cf8fcb66c90ae9a15e480db852800b4 . 6 1797 38064eed4cad2151a6c33e6c6a18c7ec . 7 1797 dab0bd23a4d856da383cda3caea87a82 . 8 1797 782dc69ce1d62e4646759790991fb531 . 9 1797 6d2d8b1532d1a2e50d7e908750aad446
3, multi-line write gzip compression
#!/usr/bin/env python3 # encoding: utf-8 import gzip import io import itertools with gzip.open('example_line.txt.gz','wb') as output: with io.TextIOWrapper(output,encoding='utf-8') as enc: enc.writelines( itertools.repeat ( ' of The Same Line \ n- ' , 10) # rewritable 10 )
running result
[root@ mnt]# python3 gzip_writelines.py [root@ mnt]# ll total 12 -rw-r--r-- 1 root root 60 Jan 1 09:41 example_line.txt.gz -rw-r--r-- 1 root root 369 Jan 1 09:40 gzip_writelines.py [root@ mnt]# gzip -d example_line.txt.gz [root@ mnt]# ll total 12 -rw-r--r-- 1 root root 140 Jan 1 09:41 example_line.txt -rw-r--r-- 1 root root 369 Jan 1 09:40 gzip_writelines.py [root@ mnt]# cat example_line.txt The same line The same line The same line The same line The same line The same line The same line The same line The same line The same line
4, read the contents of a file compressed with gzip
#!/usr/bin/env python3 # encoding: utf-8 import gzip import io with gzip.open('example.text.gz', 'rb') as input_file: with io.TextIOWrapper(input_file, encoding='utf-8') as dec: print(dec.read())
running result
[root@ mnt]# python3 gzip_read.py test gzip content
5, read the compressed file using gzip seek positioning value
#!/usr/bin/env python3 # encoding: utf-8 import gzip import io gzip.open with ( ' example.text.gz ' , ' RB ' ) AS input_file: Print ( ' read the entire contents of the compressed file ' ) all_data = input_file.read() print(all_data) expected = all_data [5:10] # slice values Print ( ' slice values: ' , expected) # The stream file pointer to the starting point of the cut input_file.seek (0) # The file pointer to the tangential-flow index of 5 input_file.seek (5 ) new_data = input_file.read (. 5 ) Print ( ' Move the pointer values: ' , new_data) Print ( ' determines whether or not two values, like: ' , expected == new_data)
running result
[root@ mnt]# python3 gzip_seek.py Read the entire contents of compressed files B ' Test the gzip Content ' slice values: B ' the gzip ' to move the pointer values: B ' the gzip ' determines whether the same two values: True
6, an example of processing (ByteIO) of the byte stream gzip
#!/usr/bin/env python3 # encoding: utf-8 import gzip from io import BytesIO import binascii # Obtain uncompressed data uncompress_data = B ' of The Same Line, over and over \ n-. ' * 10 Print ( ' of Uncompressed Len: ' , len (uncompress_data)) Print ( ' Uncompress the Data: ' , uncompress_data) buffer = BytesIO() with gzip.GzipFile(mode='wb', fileobj=buffer) as f: f.write(uncompress_data) # Acquired compressed data compress_data will = buffer.getvalue () Print ( ' Compressed: ' , len (compress_data will)) Print ( ' Compress the Data: ' , binascii.hexlify (compress_data will)) # Reread data InBuffer = BytesIO (compress_data will) with gzip.GzipFile(mode='rb', fileobj=inbuffer) as f: reread_data = reached, f.read (len (uncompress_data)) Print ( ' use uncompressed length, the length of the compressed data acquisition: ' , len (reread_data)) Print (reread_data)
[root@ mnt]# python3 gzip_BytesIO.py Uncompressed Len: 290 Uncompress Data: b'The same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\n' Compressed: 51 Compress Data: b'1f8b0800264c0c5e02ff0bc94855284ecc4d55c8c9cc4bd5c92f4b2d5248cc4b510031f4b8424625f5b8008147920222010000' 利用未压缩的长度,获取压缩后的数据长度: 290 b'The same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\n'