How to create a zip file using Python?


ZIP is an archive file format used to for lossless data compression. One or more directories or files are used to create a ZIP file. ZIP supports multiple compression algorithms, DEFLATE being the most common. ZIP files have .zip as extension. In this article we are going to discuss how to create a Zip file using Python.

Creating uncompressed ZIP file in Python

Uncompressed ZIP files do not reduce the size of the original directory. As no compression is sharing uncompressed ZIP files over a network has no advantage as compared to sharing the original file.

Using shutil.make_archive to create Zip file

Python has a standard library shutil which can be used to create uncompressed ZIP files. This method of creating ZIP file should be used only to organize multiple files in a single file.

Syntax

Following is the syntax of shutil.make_archive()

shutil.make_archive('output file name', 'zip', 'directory name')

Example

Following is an example to create ZIP file using shutil.make_archive()

import shutil import os.path # Creating the ZIP file archived = shutil.make_archive('E:/Zipped file', 'zip', 'E:/Folder to be zipped') if os.path.exists('E:/Zipped file.zip'): print(archived) else: print("ZIP file not created")

Output

Following is an output of the above code −

E:\Zipped file.zip

Creating compressed ZIP file in Python

Compressed ZIP files reduce the size of the original directory by applying compression algorithm. Compressed ZIP files result in faster file sharing over a network as the size of the ZIP file is significantly smaller than original file.

The zipfile library in python allows for creation of compressed ZIP files using different methods.

Creating ZIP file from multiple files

In this method, ZipFile() creates a ZIP file in which the files which are to be compressed are added. This is achieved by creating object of ZipFile using 'with' keyword and then writing the files using .write() method.

Example

Following is an example to create ZIP file using multiple files −

import os from zipfile import ZipFile # Create a ZipFile Object with ZipFile('E:/Zipped file.zip', 'w') as zip_object: # Adding files that need to be zipped zip_object.write('E:/Folder to be zipped/Greetings.txt') zip_object.write('E:/Folder to be zipped/Introduction.txt') # Check to see if the zip file is created if os.path.exists('E:/Zipped file.zip'): print("ZIP file created") else: print("ZIP file not created")

Output

Following is an output of the above code −

ZIP file created

Creating ZIP file from entire directory

In this method, a for loop is used to traverse the entire directory and then add all the files present in the directory to a ZIP file which is created using ZipFile.

Example

Following is an example to create ZIP file from entire directory −

import os from zipfile import ZipFile # Create object of ZipFile with ZipFile('E:/Zipped file.zip', 'w') as zip_object: # Traverse all files in directory for folder_name, sub_folders, file_names in os.walk('E:/Folder to be zipped'): for filename in file_names: # Create filepath of files in directory file_path = os.path.join(folder_name, filename) # Add files to zip file zip_object.write(file_path, os.path.basename(file_path)) if os.path.exists('E:/Zipped file.zip'): print("ZIP file created") else: print("ZIP file not created")

Output

Following is an output of the above code −

ZIP file created

Creating ZIP file from specific files in a directory

In this method, lambda function is used to filter files with specific extensions to be added in the ZIP file. The lambda function is passed as parameter to a function in which the files are filtered based on the extension.

Example

Following is an example to create ZIP file using specific files in a directory −

import os from zipfile import ZipFile def zip_csv(directory_name, zip_file_name, filter): # Create object of ZipFile with ZipFile(zip_file_name, 'w') as zip_object: # Traverse all files in directory for folder_name, sub_folders, file_names in os.walk(directory_name): for filename in file_names: # Filter for csv files if filter(filename): # Create filepath of files in directory file_path = os.path.join(folder_name, filename) # Add files to zip file zip_object.write(file_path, os.path.basename(file_path)) if __name__ == '__main__': zip_csv('E:/Folder to be zipped', 'E:/Zipped file.zip', lambda name: 'csv' in name) if os.path.exists('E:/Zipped file.zip'): print("ZIP file created with only CSV files") else: print("ZIP file not created")

Output

Following is an output of the above code −

ZIP file created with only CSV files

Updated on: 25-Aug-2023

44K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements