Python之gzip模塊的使用


gzip模塊作用:
  為GNU zip文件提供了一個類似的接口,它使用zlib來壓縮和解壓數據。

 1、寫壓縮gzip文件

#!/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))
gzip_write.py

 測試效果

[root@ mnt]# python3 gzip_write.py 
example.text.gz 包含的大小: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壓縮級別的測試以及適當設置值

#!/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()

# 讀取文件內容,並且復制1024份出來
data = open('content.txt', 'r').read() * 1024

# 輸入文件內容,返回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))
gzip_compresslevel.py

運行效果

[root@ mnt]# python3 gzip_compresslevel.py 
Level  Size        Checksum
-----  ----------  ---------------------------------
data   344064      03456cbea4852fa964775f38f03f2f2b
   0   344159        1b9895c8eaa94a0fdc5002d35fd44a93
   1   3333        42f88491459c7a7028da1ffb5f2bdc82
   2   3114        13771e090b90d4692a71079856256191
   3   3114        f822f153e8b6da768f8b84559e75e2ca
   4   1797        41e03538d99b3697db87901537e2577f #4以后最優
   5   1797        6cf8fcb66c90ae9a15e480db852800b4
   6   1797        38064eed4cad2151a6c33e6c6a18c7ec
   7   1797        dab0bd23a4d856da383cda3caea87a82
   8   1797        782dc69ce1d62e4646759790991fb531
   9   1797        6d2d8b1532d1a2e50d7e908750aad446

 3、gzip多行的寫入壓縮

#!/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('The same line\n',10) #重復寫入10次
        )
gzip_writelines.py

運行效果

[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、讀取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())
gzip_read.py

運行效果

[root@ mnt]# python3 gzip_read.py 
test gzip content

5、讀取gzip壓縮文件利用seek定位取值

#!/usr/bin/env python3
# encoding: utf-8

import gzip
import io

with gzip.open('example.text.gz', 'rb') as input_file:
    print('讀取整個壓縮文件的內容')
    all_data = input_file.read()
    print(all_data)
    expected = all_data[5:10]  # 切片取值
    print('切片取值:', expected)

    # 將流文件的指針切至起始點
    input_file.seek(0)

    # 將流文件的指針切至5的下標
    input_file.seek(5)

    new_data = input_file.read(5)
    print('移動指針取值:', new_data)
    print('判斷兩種取值是否一樣:',expected == new_data)
gzip_seek.py

運行效果

[root@ mnt]# python3 gzip_seek.py 
讀取整個壓縮文件的內容
b'test gzip content'
切片取值: b'gzip '
移動指針取值: b'gzip '
判斷兩種取值是否一樣: True

 6、gzip字節流的處理(ByteIO)的示例

#!/usr/bin/env python3
# encoding: utf-8

import gzip
from io import BytesIO
import binascii

# 獲取未壓縮的數據
uncompress_data = b'The same line,over and over.\n' * 10
print('Uncompressed Len:', len(uncompress_data))
print('Uncompress Data:', uncompress_data)

buffer = BytesIO()
with gzip.GzipFile(mode='wb', fileobj=buffer) as f:
    f.write(uncompress_data)

# 獲取壓縮的數據
compress_data = buffer.getvalue()
print('Compressed:', len(compress_data))
print('Compress Data:', binascii.hexlify(compress_data))

# 重新讀取數據
inbuffer = BytesIO(compress_data)
with gzip.GzipFile(mode='rb', fileobj=inbuffer) as f:
    reread_data = f.read(len(uncompress_data))
print('利用未壓縮的長度,獲取壓縮后的數據長度:', len(reread_data))
print(reread_data)
gzip_BytesIO.py
[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'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM