How to Compress a file to zip format with Python

0
file compression with python

The Python zipfile module is the easiest way to convert any type of file into a zip compressed format. zipfile module is a built-in module to the python programming language and you need not install it.

Change any file to zip format with Python

from zipfile import ZipFile

with ZipFile('test.zip', 'w') as zipObj:
   # Add  files to the zip
   zipObj.write('Roughwork\pretify.html')

The above code will convert the 'pretify.html' file into a compressed zip file 'test.zip' with the help of zipfile module.

the mode here specifies that we currently do not have any zipfile with the name 'test.zip' and the program will create it by itself.

We have opened the file with the contest manager so we do not need to close the zip file otherwise we will have to close the zip file by calling the zipfile.close() method.

from zipfile import ZipFile

zipObj = ZipFile('test.zip', 'w')
zipObj.write('Roughwork\pretify.html')
zipObj.close()

Convert multiple files into zip format with Python

In the above example, we have added a single file into the zip compressed format. once we create the zipfile object we can add as many files as we want. In the below example we have added 5 different files into the zip file format with python.

from zipfile import ZipFile

with ZipFile('test.zip', 'w') as zipObj:
   # Add  more than one files to the zip
   zipObj.write('Roughwork\pretify.html')
   zipObj.write('Roughwork\pretify1.html')
   zipObj.write('Roughwork\pretify2.html')
   zipObj.write('Roughwork\pretify3.html')
   zipObj.write('Roughwork\pretify4.html')

The zipfile file in the above case will be created with 5 files in it. The mode of the zip file that we opened as 'w' means that it is in the writable mode and it will create the new zip file if it is not already there.

Open the Zipfile in the append mode

If you do not want to overwrite the current zip file with the new one then you should open the zip file with the mode set to 'a which means that the file that ZipFile class opens will not overwrite the current file that has the same name and path. This technique is helpful if you do not want to overwrite the file mistakenly.

from zipfile import ZipFile

zipObj = ZipFile('test.zip', 'a')
zipObj.write('Roughwork\pretify.html')
zipObj.close()

in the same way, we can also open the file with the read-only mode and in that case, we should set the mode to 'r' which means that the file will be open in the read-only mode and there should be no operation performed, that are related to the modification of the file.

Controlling the level of Compression of the zip file with Python

the constructor of the ZipFile class also takes another parameter that can control the level of compression. There is 4 different level of compression and we need to pass any of these 4 constant values that can help us control the level of compression of the zip file.

In our cases, we have to pass the compression value set to ZIP_STORED, ZIP_DEFLATED, ZIP_BZIP2 or ZIP_LZMA. Other than these values will cause an error.

from zipfile import ZipFile
import zipfile

zipObj = ZipFile('test.zip', 'w',compression=zipfile.ZIP_DEFLATED)
zipObj.write('Roughwork\pretify.html')
zipObj.close()

Read the Complete Guide of working with Zip files using Python Programming.

Post a Comment

0Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !