Python Gzip 模块

2024年8月29日 | 13分钟阅读

该模块提供了一个简单的接口,用于压缩和解压缩文件,类似于 GNU 的 gzip 和 gunzip 工具。GzipFile 类以及 open()、compress() 和 decompress() 便利函数都由 gzip 模块提供。GzipFile 类读取和写入 gzip 格式的文件,自动压缩或解压缩内容,使其看起来像一个普通的文件对象。

gzip 模块的必要性

关于 gzip 模块有什么必要性的问题的最主要答案是数据压缩。将数据编码、重新排列或以其他方式更改以最小化其大小的过程称为数据压缩。它基本上涉及以比原始表示更少的位数来重新编码数据。压缩由使用函数或算法寻找最小化数据大小的最有效方法的程序执行。例如,算法可以通过使用参考字典在位字符串之间进行转换,从而用较小的位字符串表示一个位字符串。另一个例子是插入一个已由程序看到的字符串的引用或指针的公式。在图像压缩方面,这是一个绝佳的例子。当图像中出现一系列颜色,例如“蓝、红、红、蓝”时,该公式可以在保留底层信息的同时,将此数据字符串转换为单个位。文本压缩通常通过删除所有多余字符、用更小的位字符串替换更常见的位字符串,以及插入单个字符作为重复字符字符串的引用来完成。通过正确的方法,数据压缩可以将文本文件的大小减小多达 50%,大大减小其总大小。压缩可以应用于内容或整个传输以进行数据传输。较大的文件,单独或与其他文件组合,或作为存档文件的一部分,在通过互联网发送或接收时,可以采用多种压缩格式之一进行传输,例如 ZIP、RAR、7z 或 MP3。

压缩有几个优点,包括减少存储硬件、数据传输时间和通信带宽。这有潜力节省大量资金。压缩文件比未压缩文件需要更少的存储空间,从而在存储成本上节省大量费用。压缩文件在网络上传输也需要更少的时间,并且使用更少的带宽。这可以节省成本,同时提高生产力。数据压缩的主要缺点是它需要更多的处理资源来压缩必要的数据。因此,压缩供应商优先考虑优化速度和资源效率,以减少密集压缩作业的影响。

压缩有几个优点,包括减少存储硬件、数据传输时间和通信带宽。这有潜力节省大量资金。压缩文件比未压缩文件需要更少的存储空间,从而在存储成本上节省大量费用。压缩文件在网络上传输也需要更少的时间,并且使用更少的带宽。这可以节省成本,同时提高生产力。

数据压缩的主要缺点是它需要更多的处理资源来压缩必要的数据。因此,压缩供应商优先考虑优化速度和资源效率,以减少密集压缩作业的影响。

由于未压缩的文本或多媒体(语音、图像或视频)数据需要大量的位数来表示,因此需要大量的带宽,通过使用良好的压缩编码策略,可以减少这种存储空间和带宽需求。压缩程度、引入的失真量以及压缩和解压缩数据所需的计算资源都是设计数据压缩方案时要考虑的因素。

在互联网时代,数据传输对时间非常敏感。考虑一个音频文件,它不过是在设定的时间段内的声音强度变化。声音文件用于通过网络传输此音频。如果声音文件的大小过大,传输文件所需的时间就会增加。压缩可以减少传输文件所需的时间。

在计算机术语中,压缩是指降低数据物理大小的过程,以便它占用更少的存储空间和内存。因此,压缩文件更容易传输,因为数据大小已大大减小。

代码

输出

Enter your choice according to the below-listed options::
1. To write data to a gz compressed file.
2. To read data from a gz compressed file.
3. To get the size of the compressed gz file.
4. To compress the input string with the gzip library.
5. To decompress the input string with the gzip library.
6. To exit from the code execution.
1
enter the name of the gz file to which you want to write::
my_gz_file_1.txt.gz
enter the string you want to the write file::
This is a sample data going to be stored in a compressed gz file.
String 'This is a sample data going to be stored in a compressed gz file.' written successfully to the my_gz_file_1.txt.gz file.
To move ahead with code execution enter [y] else [n]
y
Enter your choice according to the below-listed options::
1. To write data to a gz compressed file.
2. To read data from a gz compressed file.
3. To get the size of the compressed gz file.
4. To compress the input string with the gzip library.
5. To decompress the input string with the gzip library.
6. To exit from the code execution.
2
enter the name of the gz file from which you want to read::
my_gz_file_1.txt.gz
The data inside the my_gz_file_1.txt.gz file is::
This is a sample data going to be stored in a compressed gz file.
To move ahead with code execution enter [y] else [n]
y
Enter your choice according to the below-listed options::
1. To write data to a gz compressed file.
2. To read data from a gz compressed file.
3. To get the size of the compressed gz file.
4. To compress the input string with the gzip library.
5. To decompress the input string with the gzip library.
6. To exit from the code execution.
3
enter the name of the gz whose size you want to check::
my_gz_file_1.txt.gz
The file my_gz_file_1.txt.gz has 104 bytes
To move ahead with code execution enter [y] else [n]
y
Enter your choice according to the below-listed options::
1. To write data to a gz compressed file.
2. To read data from a gz compressed file.
3. To get the size of the compressed gz file.
4. To compress the input string with the gzip library.
5. To decompress the input string with the gzip library.
6. To exit from the code execution.
4
enter the data that you want to compress
sample string to compress
The data after compression b'\x1f\x8b\x08\x00K\xd0Qb\x02\xff+N\xcc-\xc8IU(.)\xca\xccKW(\xc9WH\xce\xcf-(J-.\x06\x00i\xb7qc\x19\x00\x00\x00'
To move ahead with code execution enter [y] else [n]
y
Enter your choice according to the below-listed options::
1. To write data to a gz compressed file.
2. To read data from a gz compressed file.
3. To get the size of the compressed gz file.
4. To compress the input string with the gzip library.
5. To decompress the input string with the gzip library.
6. To exit from the code execution.
5
enter the data to be decompress
\x1f\x8b\x08\x00K\xd0Qb\x02\xff+N\xcc-\xc8IU(.)\xca\xccKW(\xc9WH\xce\xcf-(J-.\x06\x00i\xb7qc\x19\x00\x00\x00
The data after compression sample string to compress
To move ahead with code execution enter [y] else [n]
Y
Enter your choice according to the below-listed options::
1. To write data to a gz compressed file.
2. To read data from a gz compressed file.
3. To get the size of the compressed gz file.
4. To compress the input string with the gzip library.
5. To decompress the input string with the gzip library.
6. To exit from the code execution.
1
enter the name of the gz file to which you want to write::
gz_file_new.gz
enter the string you want to the write file::
gzip module use cases are explained with the help of this python code
String 'gzip module use cases are explained with the help of this python code ' written successfully to the gz_file_new.gz file.
To move ahead with code execution enter [y] else [n]
y
Enter your choice according to the below-listed options::
1. To write data to a gz compressed file.
2. To read data from a gz compressed file.
3. To get the size of the compressed gz file.
4. To compress the input string with the gzip library.
5. To decompress the input string with the gzip library.
6. To exit from the code execution.
2
enter the name of the gz file from which you want to read::
gz_file_new.gz
The data inside the gz_file_new.gz file is::
gzip module use cases are explained with the help of this python code
To move ahead with code execution enter [y] else [n]
y
Enter your choice according to the below-listed options::
1. To write data to a gz compressed file.
2. To read data from a gz compressed file.
3. To get the size of the compressed gz file.
4. To compress the input string with the gzip library.
5. To decompress the input string with the gzip library.
6. To exit from the code execution.
3
enter the name of the gz whose size you want to check::
gz_file_new.gz
The file gz_file_new.gz has 97 bytes

Enter your choice according to the below-listed options::
1. To write data to a gz compressed file.
2. To read data from a gz compressed file.
3. To get the size of the compressed gz file.
4. To compress the input string with the gzip library.
5. To decompress the input string with the gzip library.
6. To exit from the code execution.
6

在上面编写的代码中,我们编写了一个类,其中每个函数代表了 Python 的 gzip 模块提供的不同功能的不同用例场景。Python 的 gzip 模块提供的函数的功能包括:将数据压缩并写入文件、从 gz 文件读取数据、获取 gz 文件的大小、压缩数据输入流以及解压缩用户提供的压缩数据输入流。

编写了一个函数,用于将加密或压缩的内容写入 gz 文件。在此函数中,用户会收到一条消息,提示输入要存储数据的 gz 文件名。添加文件名后,会询问用户要存储在指定文件中的数据。一旦用户添加了要压缩的数据,将执行两个操作:首先,输入数据将使用 gzip 库进行压缩,并在成功完成输入字符串或数据后,将压缩后的数据流写入用户指定的名称的文件,并提示用户一条成功消息。另一个函数与上面编写的函数完全不同。在此函数中,数据将从特定的 gz 文件读取,然后解压缩并打印给用户。在此函数中,用户会收到一条消息,提示输入要从中读取压缩数据的 gz 文件名。添加文件名后,将执行两个操作:第一个操作是从指定的 gz 文件读取数据,在从指定的 gz 文件读取数据后,该数据将使用库进行解压缩,并在数据成功解压缩后,将解压缩后的数据打印给用户。

因此,在本文中,我们探讨了 Python 中 gzip 模块的用法,以及如何在不同的用例场景中使用 gzip 模块提供的各种功能。我们还看到一个程序,它实际上调用了编写的不同函数,其中每个函数代表一个单独的用例或功能。