
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create a Zip File Using Python
ZIP is a widely used file format that compresses one or more files into a single archive file. This file format helps to reduce storage space and simplifies sharing or save backup of large data files. ZIP archives usually have a .zip file extension and are commonly used across different platforms.
Compressed ZIP files reduce the size of the original directory by applying a compression algorithm. These result in faster file sharing over a network as the size of the ZIP file is significantly smaller than the original file.
Creating ZIP files in Python
In Python, we have the two standard modules for creating ZIP files as mentioned below -
- zipfile: The Python ZIP module provides various methods to compress and decompress files. It supports multiple compression algorithms, DEFLATE being the most common. ZIP files have .zip. Using this module we can create, read, write, append, and list a ZIP file.
- shutil: The Python Shutil module provides methods to perform various high-level operations on files and collections of files.
We can create a zip file in Python -
- Using the shutil.make_archive() Method
- Using the zipfile.ZipFile() Method
Using shutil.make_archive() to Zip a Folder
The shutil.make_archive() module provides a quick and easy way to archive a complete folder. The make_archive() function handles both archiving and compression in a single step.
Example
Following is an example that compresses a folder named my_folder into a ZIP file named Archive.zip by using the shutil.make_archive() module -
import shutil # Compress the entire folder into a ZIP file shutil.make_archive("Archive", "zip", r"D:\Tutorialspoint\sample")
The above program creates a file with the name Archive.zip which contains all the contents from sample folder.
Using zipfile to Create ZIP Archives
The zipfile.ZipFile() method is useful when we want to add specific files or customize the ZIP file that is created. We can also apply compression for a smaller output size with this method.
Example
Following is an example that shows how to include multiple individual files into a ZIP file using the zipfile.ZipFile() method ?
from zipfile import ZipFile # List of files to include in the archive file_list = ["sample.txt", "sample.png"] # Create ZIP file and write files into it with ZipFile("output.zip", "w") as zipf: for file in file_list: zipf.write(file)
When we execute the above program, a file named output.zip containing sample.txt and sample.png.
Creating a Compressed ZIP File
To compress files within the ZIP then we have to use the ZIP_DEFLATED mode in the ZipFile() method, which reduces the archive size.
Example
Following is an example which creates a compressed ZIP file ?
from zipfile import ZipFile, ZIP_DEFLATED # Create a compressed ZIP file with ZipFile("compressed.zip", "w", compression=ZIP_DEFLATED) as zipf: zipf.write("sample.txt") zipf.write("sample.png")
The above program will generate compressed.zip file with compressed contents of the files added.
Creating ZIP File from an Entire Directory
If we want to zip all files in a folder, which includes all sub-folders. We need to use the os.walk() method to traverse the directory and add each file to the archive manually using the ZipFile() method.
Example
Here's how to compress an entire directory named project_data into a ZIP file named backup.zip ?
import os from zipfile import ZipFile, ZIP_DEFLATED def create_zip_from_folder(folder_path, zip_name): with ZipFile(zip_name, "w", ZIP_DEFLATED) as zipf: for root, dirs, files in os.walk(folder_path): for file in files: file_path = os.path.join(root, file) zipf.write(file_path, os.path.relpath(file_path, start=folder_path)) # Compress the folder into a ZIP file create_zip_from_folder("project_data", "backup.zip")
The above program compresses all files and sub-folders under project_data into backup.zip folder.
Creating uncompressed ZIP file in Python
Uncompressed ZIP files do not reduce the size of the original directory. As no compression is shared, uncompressed ZIP files over a network have 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 a ZIP file should be used only to organize multiple files into a single file.
Following is the syntax of shutil.make_archive() function -
shutil.make_archive('output file name', 'zip', 'directory name')
Example
Following is an example to create a ZIP file using shutil.make_archive() function -
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")
Following is an output of the above code -
E:\Zipped file.zip