Python

Python Gzip Decompress Function

Python Gzip Decompress Function

You may have heard the terms encode and decode. These terms are used to compress/decompress or convert the input to some other not understandable format. These techniques are usually used for security. Through using the gzip.decompress(s) function, we may decode the encoded bytes of a text into its input string in Python. Within this article today, we will discuss the use of the decompressing function of the gzip library in Python.

Example 01:

Let’s take a look at our first example to make use of the decompress function of Python’s “gzip” library. So, you have to open the Spyder3 code area from your system. For using the “decompress” function, you need to import the “gzip” package or library of python in your code using the keyword “import” as shown in the image below. After this, we have declared and initiated a new variable named “comp” with some string type value. Its value has been converted into bytes using the character “b” before the string as we know that the “compress” and “decompress” functions of gzip take byte-like data to process.

Firstly, we have applied the compress function of variable “comp” using the library “gzip” to compress its value to some characters unable to be understood by a normal human being. After this, the encoded string will be again saved to the “comp” variable using the overriding method. The newly encoded or compressed variable “comp” will be displayed on the output screen using the print function on it. Now, we are using the decompress function of the gzip library to decompress or decode the compressed variable “comp” to the original one. The “comp” variable is used as an argument for the decompress function here. The decompressed value is then stored to the new variable “dec” to avoid any inconvenience. Lastly, we have printed the decoded or decompressed variable “dec” within the console via the print function.

import gzip

comp = b'Hy! My name is John. I am a Police Officer'

comp = gzip.compress(comp)

print(comp)

dec = gzip.decompress(comp) # using gzip.decompress(s)

print(dec)

Let’s run the newly made python code via the debugging button. After running it, we have got the compressed string first and then got the original string again after the decompression.

Example 02:

Let’s take a look at another simple and easy example to see how the decompress function works on a string or variable having mixed characters. So, we have updated the above code very little. We have used the same gzip file via the “import” keyword within the first line of the code area. Variable names for compressed and decompressed values have remained the same i.e., comp, and dec. We have only changed the value used for compression. As you can see, we have not used the character “b” to convert the string to byte and it will cause an error when it will be compressed. The variable “comp” having string value is converted to an encoded string via the “compress” function and then decompressed to an original string using the decompress function here. The decompression value is now stored in the “Dec” variable and will be displayed on the output area of Spyder3 using the print function.

import gzip

comp = '[email protected]'

comp = gzip.compress(comp)

print(comp)

dec = gzip.decompress(comp) # using gzip.decompress(s)

print(dec)

Upon the interpretation and execution of this new code, we got the error as shown below. The error is indicating that it’s a TypeError exception raised due to not converting a string to byte-like data to compress it via the gzip package of python. So, we need to fix it.

Therefore, we have changed the code from the 2nd line. We have added character “b” at the start of variable comp’s value so that it can be taken as byte-like character data.

import gzip

comp = b'[email protected]'

comp = gzip.compress(comp)

print(comp)

dec = gzip.decompress(comp) # using gzip.decompress(s)

print(dec)

After saving and running this updated script, we have got the compressed and decompressed value of a string mentioned in the code.

Example 03:

Let’s have a look at our last but not least and a quite similar example of gzip library. We have started this example with the import of the gzip library in the starting lines. A variable “v1” has been initialized with a string value containing all the special characters in it. The keyword “b” is used before its value to convert it to the byte-like format while compression. In the next consecutive line, we have applied the compress function of the v1 variable via the gzip library to compress it in an encoded format. The compressed value is saved again to variable v1 and displayed on the output via the print clause.

After this, the decompress function is applied to the compressed variable v1 to convert it to the original format again. The decompressed value would be saved in the new variable v1 and displayed on the output using the print statement on it.

Import gzip

v1 = b’@#$%^&*()_+-=”:}{[].,/?~`}’

v1 = gzip.compress(v1)

print(v1)

v2 = gzip.decompress(v1)

print(v2)

The output for this code is as expected i.e., compressed and decompressed value.

Conclusion:

Finally, we have covered the use of decompressing function in Python. We have discussed three different and easy examples for this purpose and avoided complex examples for better understandability of our users. We are hoping eagerly that you will find it quite awesome.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content