Python module for Gzip compression
This module provides a straightforward interface for compressing and decompressing files, very much like the gzip and gunzip programs that are included with the GNU operating system. The gzip module is responsible for providing the GzipFile class in addition to the convenience functions open(), compress(), and decompress() respectively. The GzipFile class can read and write files in the gzip format. It can also compress or decompress the contents of the file automatically, giving the appearance of being a standard file.
object.
Need for gzip module:
The primary reason for using gzip is to compress data, which is also the primary response to the question of why gzip is needed. Data compression is the process of reducing the size of data by encoding it, rearranging it in a different sequence, or by modifying it in some other way. It boils down to re-encoding data using a smaller number of bits in comparison to the data’s initial form. A program that use functions or algorithms to determine the most effective method for reducing the amount of data stored in memory is responsible for doing compression. For instance, an algorithm may represent a string of bits with a smaller string of bits by converting between them with the use of a reference dictionary. This may be done in order to save space.’ Another illustration of this would be a formula that inserts a reference or pointer to a string of data that the program has already examined. This is an excellent example of picture compression, which is why you should do it. The method is able to transform a string of data into a single bit while still maintaining the integrity of the underlying information whenever there is a pattern of colors that appears across the image, such as “blue, red, red, blue.” Text compression is often performed by removing any and all unnecessary characters, replacing a shorter bit string with a bit string that is more commonly used, and adding a single character to a string of characters that are repeated multiple times as a reference for the string. A text file’s size can be reduced by as much as fifty percent by the use of data compression, which results in a significant reduction of the file’s overall size. When it comes to the transmission of data, compression can be applied to either the content itself or the whole process. When larger files are sent or received over the internet, they can be transported using one of the several compressed formats, such as ZIP, RAR, 7z, or MP3, which are available. These formats can be used alone, or in conjunction with other files, or as part of an archive file.
Compression offers a number of benefits, including a reduction in the necessary storage hardware, the amount of time spent transferring data, and the amount of bandwidth used for communication. This has the potential to result in significant cost reductions. When compared to uncompressed files, compressed ones take up a significantly smaller amount of space in storage, which results in significant cost savings for the storage service. Transferring a compressed file also takes less time and utilizes less network bandwidth than an uncompressed file does. This has the potential to cut expenditures while while boosting overall productivity. The most significant drawback associated with data compression is the requirement of more processing resources in order to compress the required data. As a consequence of this, compression providers place a high priority on improving both the speed at which they work and the efficiency with which they use their resources in order to lessen the impact that extensive compression activities have.
Compression offers a number of benefits, including a reduction in the necessary storage hardware, the amount of time spent transferring data, and the amount of bandwidth used for communication. This has the potential to result in significant cost reductions. When compared to uncompressed files, compressed ones take up a significantly smaller amount of space in storage, which results in significant cost savings for the storage service. Transferring a compressed file also takes less time and utilizes less network bandwidth than an uncompressed file does. This has the potential to cut expenditures while while boosting overall productivity.
The most significant drawback associated with data compression is the requirement of more processing resources in order to compress the required data. As a consequence of this, compression providers place a high priority on improving both the speed at which they work and the efficiency with which they use their resources in order to lessen the impact that extensive compression activities have.
Because uncompressed text or multimedia (speech, image, or video) data requires a large number of bits to represent it and, as a result, a large amount of bandwidth, the amount of storage space required and the amount of bandwidth required can be lowered by utilizing an effective compression encoding approach. When creating data compression algorithms, some elements that should be taken into consideration include the degree of compression, the amount of distortion that is created, and the amount of computational resources that are necessary to compress and decompress the data.
In this day and age of the Internet, the transport of data is particularly time-sensitive. Take for example an audio file, which is comprised of nothing more than shifting levels of sound intensity over the course of a predetermined amount of time. When moving this audio via networks, sound files are what are employed to do the transporting. If the file sizes of the sound files are excessively high, the amount of time required to transfer the files will also rise. The amount of time necessary to transfer a file can be cut down by the use of compression.
In the world of computing, the phrase “compression” refers to the process of reducing the actual size of data files in order to make better use of available memory and storage space. Since the data size of compressed files is greatly reduced, it is significantly easier to transport these files. Code:
#!/bin/python3
import gzip
import io
import os
import sys
#A simple Python class that will be used to write the different functions that will depict the different use case scenarios of the above-mentioned compression library in Python
class gzipClass:
# Sample constructor is written that can be used to initialize the class variables of this class
def __init__(self):
pass
# a function is written which will write the encrypted or compressed contents to the gz file in this function the user is prompted with the message to enter the name of the gz file in which the user wants to store the data after adding the name of the file the user is asked for the data that he wants to store in the just specified file once the user adds the data he wants to compress there are two operations which are performed first of all the input data is compressed with the help of gzip library and on the successful completion of the input string or data that compressed data stream is written to the file with the name specified by the user and a successful message is prompted to the user
def write_to_gz_file(self):
print("enter the name of the gz file to which you want to write::")
gz_filename = input()
print("enter the string you want to the write file::")
gz_file_content = input()
with gzip.open(gz_filename, 'wb') as gz_filehandle:
# We cannot directly write Python objects like strings!
# We must first convert them into a bytes format using io.BytesIO() and then write it
with io.TextIOWrapper(gz_filehandle, encoding='utf-8') as gz_encoder_obj:
gz_encoder_obj.write(gz_file_content)
print("String '{data}' written successfully to the {fname} file.".format(
data=gz_file_content, fname=gz_filename))
#This function is totally different from the function that we have written above in this function the data is read from the particular gz file and then decompressed and is printed to the user in this function the user is prompted with the message to enter the name of the gz file from which the user wants to read the compressed data on adding the name of the file there are two operations which are performed the first operation which is performed is reading the data from the specified gz file and after reading the data from the specified gz file that data is decompressed with the help of the library and after the successful decompression of the data, that decompressed data is printed to the user
def read_from_gz_file(self):
print("enter the name of the gz file from which you want to read::")
gz_filename = input()
# We cannot directly write Python objects like strings!
# We must first convert them into a bytes format using io.BytesIO() and then write it
with gzip.open(gz_filename, 'rb') as gz_reader_filehandle:
with io.TextIOWrapper(gz_reader_filehandle, encoding='utf-8') as gz_decoder_obj:
gz_file_contents = gz_decoder_obj.read()
print("The data inside the {fname} file is::\n{data}".format(
data=gz_file_contents, fname=gz_filename))
# this function is used to get the size of the gz file in which we have stored the compressed data, this function asks the user for the name of the gz file whose size the user wants to check after adding the name of the file, particularly the gz file, the number of bytes that are present in that particular gz files are calculated and printed, point to be noted in this case is that these bytes of data represent the compressed data that is stored in the gz file
def get_gz_file_size(self):
print("enter the name of the gz whose size you want to check::")
gz_filename = input()
no_of_bytes = os.stat(gz_filename).st_size
print("The file {fname} has {bytes} number of bytes".format(
fname=gz_filename, bytes=no_of_bytes))
# this function is used to compress the input string with the help of the library in this function the user is asked to enter the data that the user wants to compress after adding the data or the string that input string is first converted to the Byte data with the help of encoding function of the string on the successful conversion the byte data is compressed with the help of the Python compression library and in the end that compressed data is printed to the use
def compress_data(self):
print("enter the data that you want to compress")
data_to_compress = input()
byte_data = str.encode(data_to_compress)
compressed_data = gzip.compress(byte_data)
print("The data after compression {} ".format(compressed_data))
# this function performs the exact opposite of the Above Return function. this function converts the compressed data to the decompressed data, this function Asks the user for the compressed bytes of the data, and on adding the input this function uses the same encoding function that was used in the previous function to encode it and then decompress the input stream of compressed data with the help of the decompress function of the gzip module of python.
def decompress_data(self):
print("enter the data to be decompress")
data_to_decompress = input()
byte_data = str.encode(data_to_decompress)
decompressed_data = gzip.decompress(byte_data)
print("The data after decompresion {} ".format(decompressed_data))
#In the end the main function is written in which we have created the object of the above-written class and that object can be used to call all the different functions that we have written inside that class where each function is representing a different use case scenario of the different functions that are offered by the gzip module of python the different functionalities of functions that are offered by the gzip module of the python are like compressing the data and writing to a file, reading data from a gz file, getting the size of a gz file, compressing the input stream of data and decompressing the input stream of compressed data that is provided as an input by the user all these functions are called with the help of the object of the above-written class.
w
Output:
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
In the code that has been shown above, we have written a class in which each function represents a different use case scenario of the various functions that are offered by the gzip module of python. These functions include compressing data and writing it to a file, reading data from a gz file, determining the size of a gz file, compressing the input stream of data and decompressing the input stream of compressed data, and reading the size of a gz file.
After adding the name of the file, the user is prompted with the message to enter the name of the gz file in which the user wants to store the data. After adding the name of the file, the user is asked for the data that he wants to store in the just specified file. Once the user adds the data he wants to compress, there are two operations that are performed. a function is written that will write the encrypted or compressed contents to the gz file. Initially, the input data is compressed with the assistance of the gzip library. Subsequently, after the successful completion of the input string or data, the compressed data stream is written to the file with the name chosen by the user, and the user is prompted with a message indicating that the process was successful. The other function is completely different from the function that we have written above. In this function, the data is read from a specific gz file, then decompressed, and is printed to the user. In this function, the user is prompted with the message to enter the name of the gz file from which the user wants to read the compressed data. On adding the name of the file, there are two operations that are performed: reading the data and decompressing it. when reading the data from the provided gz file, the data is then decompressed with the assistance of the library. Finally, when the data has been successfully decompressed, the decompressed data is printed to the user. Reading the data from the specified gz file is the first operation that is carried out.
This article explains how to use the gzip module in Python, as well as how to make use of the numerous functions supplied by the gzip module in a variety of distinct use case scenarios. Additionally, we have seen a program that actually calls the various functions that have been developed, with each function representing a separate use case or functionality.