Python compress folder to zip

Compressing Folders to ZIP in Python

In daily development, we often need to compress folders for easier transmission or storage. In Python, we can use the zipfile module to compress folders to ZIP format.

How to Implement It

  1. Importing the zipfile Module

    The zipfile module is a built-in Python module for compressing and decompressing ZIP files. We need to import it to use its functionality.

    import zipfile
    
  2. Creating a ZIP File

    Before compressing a folder, we need to create a ZIP file. You can create a zip file object using the ZipFile class in the zipfile module, as follows:

    with zipfile.ZipFile('new_archive.zip', 'w') as zipObj:
    # Compress a folder
    

    Where new_archive.zip is the name of the new zip file, and w indicates that the file is opened in write mode.

  3. Compressing a Folder

    After creating a zip file, we can use the write() method to add files or folders to the zip file. In this case, we need to add a folder to the zip file. We can recursively add all the files within the folder to the zip file.

    import os
    
    # Zip the specified folder
    def zipdir(path, ziph):
    # Loop through all files and subfolders in the folder
    for root, dirs, files in os.walk(path):
    for file in files:
    # Add each file to the zip file
    ziph.write(os.path.join(root, file))
    
    # Save the specified folder as a zip file
    def zip_folder(folder_path, zip_path):
    with zipfile.ZipFile(zip_path, 'w') as zipObj:
    # Add the folder and its contents to the zip file
    zipdir(folder_path, zipObj)
    

    In the above code, the zip_folder() function accepts two parameters: folder_path, which represents the path to the folder to be compressed; and zip_path, which represents the path to the newly created zip file. Inside the function, we first use the os.walk() function to iterate through all the files and subfolders in the folder, and then use the write() method to add each file to the zip file.

    Note: In the above code, the with keyword is used to open the zip file. This ensures that the file object is automatically cleaned up and closed after the file operation is completed.

  4. Test Program

    Use the following code to test the above program:

    if __name__ == '__main__':
    # Compress the 'test_folder' folder into a 'test_folder.zip' file
    zip_folder('test_folder', 'test_folder.zip')
    

    After running the program, a test_folder.zip file will be generated in the current directory.

Complete Code

Below is the complete code for the above program:

import zipfile
import os

# Compress the specified folder
def zipdir(path, ziph):
# Loop through all files and subfolders in the folder
for root, dirs, files in os.walk(path):
for file in files:
# Add each file to the zip file
ziph.write(os.path.join(root, file))

# Save the specified folder as a zip file
def zip_folder(folder_path, zip_path):
with zipfile.ZipFile(zip_path, 'w') as zipObj:
# Add the folder and its contents to the zip file
zipdir(folder_path, zipObj)

if __name__ == '__main__':
# Compress the 'test_folder' folder into the 'test_folder.zip' file.
zip_folder('test_folder', 'test_folder.zip')

Conclusion

Using Python to create zip files can help us more conveniently compress and store folders, especially when processing and transmitting large amounts of data. The method described in this article can be easily extended to compress other file types and also supports decompressing compressed files. I hope this article is helpful!

Leave a Reply

Your email address will not be published. Required fields are marked *