python标准库之数据压缩

python标准库提供了对通用压缩标准的支持。

一、zlib压缩标准

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
32
33
34
35
36
37

import zlib

def zlib_demo1():
'''
对类字节对象进行压缩和解压
'''
data = b'test'
# data = bytes('test', encoding = "utf8")
# data = str.encode('test')

com_bytes = zlib.compress(data)
print("压缩后的数据: ", com_bytes)

decom_bytes = zlib.decompress(com_bytes)
print("解压后的数据: ", decom_bytes)

def zlib_demo2():
'''
对数据流进行压缩和解压
'''
# 数据流压缩
with open("org_file.txt", "rb") as read, open("com_file.txt", "wb") as write:
com = zlib.compressobj(level=9, memLevel=9)
for data in read:
write.write(com.compress(data))
write.write(com.flush())

# 对数据流进行解压
with open("com_file.txt", "rb") as read, open("decom_file.txt", "wb") as write:
decom = zlib.decompressobj()
for data in read:
write.write(decom.decompress(data))
write.write(decom.flush())

zlib_demo1()
zlib_demo2()

二、gzip压缩标准

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45

import gzip

def gzip_demo1():
'''
对类字节对象进行压缩和解压
'''
data = b'test'
# data = bytes('test', encoding = "utf8")
# data = str.encode('test')

com_bytes = gzip.compress(data)
print("压缩后的数据: ", com_bytes)

decom_bytes = gzip.decompress(com_bytes)
print("解压后的数据: ", decom_bytes)

def gzip_demo2():
'''
对gzip文件的读写操作
'''
data = b'test'
# 写入
with gzip.open('1.gz', 'wb') as write:
write.write(data)
# 读取
with gzip.open('1.gz', 'rb') as read:
data2 = read.read()
print(data2)

def gzip_demo3():
'''
对文件对象的压缩与解压
'''
import shutil
# 压缩
with open('org_file.txt', 'rb') as read, gzip.open('org_file.txt.gz', 'wb') as write:
shutil.copyfileobj(read, write)
# 解压
with gzip.open('org_file.txt.gz', 'rb') as read, open('decom_file.txt', 'wb') as write:
shutil.copyfileobj(read, write)

gzip_demo1()
gzip_demo2()
gzip_demo3()

三、bz2压缩标准

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import bz2

def bz2_demo1():
'''
对类字节对象进行一次性的压缩和解压
'''
data = b'test'
# data = bytes('test', encoding = "utf8")
# data = str.encode('test')

com_bytes = bz2.compress(data)
print("压缩后的数据: ", com_bytes)

decom_bytes = bz2.decompress(com_bytes)
print("解压后的数据: ", decom_bytes)

def bz2_demo2():
'''
对类字节对象进行增量的压缩和解压
'''
data = [b'test', b'apple']
# 增量压缩
com_bz2 = bz2.BZ2Compressor()
com_bytes = com_bz2.compress(b'')
for d in data:
com_bytes += com_bz2.compress(d)
com_bytes += com_bz2.flush() # flush()结束压缩进程
print("增量压缩: ", com_bytes)

# 增量解压缩
decom_bz2 = bz2.BZ2Decompressor()
decom_bytes = decom_bz2.decompress(com_bytes)
print("增量解压: ", decom_bytes)


def bz2_demo3():
'''
对文件对象的压缩与解压
'''
import shutil
# 压缩
with open('org_file.txt', 'rb') as read, bz2.open('org_file.txt.gz', 'wb') as write:
shutil.copyfileobj(read, write)
# 解压
with bz2.open('org_file.txt.gz', 'rb') as read, open('decom_file.txt', 'wb') as write:
shutil.copyfileobj(read, write)

bz2_demo1()
bz2_demo2()
bz2_demo3()

四、lzma压缩标准

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import lzma

def lzma_demo1():
'''
对类字节对象进行一次性的压缩和解压
'''
data = b'test'
# data = bytes('test', encoding = "utf8")
# data = str.encode('test')

com_bytes = lzma.compress(data)
print("压缩后的数据: ", com_bytes)

decom_bytes = lzma.decompress(com_bytes)
print("解压后的数据: ", decom_bytes)

def lzma_demo2():
'''
对类字节对象进行增量的压缩和解压
'''
data = [b'test', b'apple']
# 增量压缩
com_lzma = lzma.LZMACompressor()
com_bytes = com_lzma.compress(b'')
for d in data:
com_bytes += com_lzma.compress(d)
com_bytes += com_lzma.flush()
print("增量压缩: ", com_bytes)

# 增量解压缩
decom_lzma = lzma.LZMADecompressor()
decom_bytes = decom_lzma.decompress(com_bytes)
print("增量解压: ", decom_bytes)


def lzma_demo3():
'''
对文件对象的压缩与解压
'''
import shutil
# 压缩
with open('org_file.txt', 'rb') as read, lzma.open('org_file.txt.xz', 'wb') as write:
shutil.copyfileobj(read, write)
# 解压
with lzma.open('org_file.txt.xz', 'rb') as read, open('decom_file.txt', 'wb') as write:
shutil.copyfileobj(read, write)

lzma_demo1()
lzma_demo2()
lzma_demo3()

五、zip压缩标准

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import zipfile

def zip_demo():
# 创建压缩包
with zipfile.ZipFile(r"file.zip", "a") as write:
# 往压缩包里添加文件
write.write("org_file.txt")

# 测试压缩包
error = write.testzip()
if error:
print("压缩文件错误: ", error)
return

# 打印压缩包信息
write.printdir()
write.close()

# 解压压缩包
with zipfile.ZipFile(r"file.zip", "r") as read:
read.extractall("file") # 解压缩目录
read.close()

zip_demo()

六、tar压缩标准

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import tarfile

def tar_demo():
# 创建压缩包
with tarfile.open(r"file.tar", "w:gz") as write:
# 往压缩包里添加文件
write.add("org_file.txt")
# 打印压缩包信息
write.list()

# 解压压缩包
with tarfile.open(r"file.tar", "r:gz") as read:
read.extractall("file") # 解压缩目录

tar_demo()
0%