Create ZIP in Python

ZIP archives compress and bundle multiple files or folders into a unified container. ZIP archive not only encapsulates the files and folders but also retains their metadata. They primarily reduce file size for storage/transmission and add security through encryption. In addition to conventional file compression tools, desktop and web applications integrate automated compression and extraction functionalities to streamline tasks such as uploading, downloading, sharing, and encrypting files. This article presents a simplified way of compressing files or folders and creating ZIP archives in Python. Also, it covers how to create encrypted and password protected ZIP archives in Python. So let’s delve into it.

Python ZIP Library

Aspose.ZIP for Python is a powerful and versatile library that provides developers with a comprehensive set of tools to work with ZIP files in Python applications. With its intuitive API and wide range of features, Aspose.ZIP for Python makes it easy to create, extract, and manage ZIP archives efficiently. You can install the library from PyPI using the following command.

> pip install aspose-zip

Create a ZIP Archive in Python

The following are the steps to compress a file by adding it into a ZIP archive in Python.

  • First, create a new ZIP archive using Archive class.
  • Then, add file to ZIP archive using Archive.create_entry() method.
  • Finally, save the ZIP archive using Archive.save() method.

The following code sample shows how to add a file into a ZIP archive in Python.

Add Multiple Files into ZIP Archive in Python

In the previous section, we demonstrated how to insert a single file into the ZIP archive. However, if you have more than one file, you can repeatedly call Archive.create_entry() method.

The following code sample shows how to insert multiple files into a ZIP archive in Python.

Add Folders to a ZIP Archive in Python

Not only the files but you can also compress folders into the ZIP archives. For this, you do not need to add each file in the folder to the archive one by one. Simply call Archive.create_entries() method, pass the folder’s name, and that is it. For your understanding, below are the steps of archiving a complete folder.

  • First, create a new archive using Archive class.
  • Then, add folder to ZIP archive using Archive.create_entries(folder_name) method.
  • Finally, save the ZIP archive using Archive.save() method.

The following code sample shows how to add a folder to ZIP in Python.

Create a Password Protected ZIP in Python

In various cases, such as while sharing the archive on the internet, you have to protect the ZIP file with a password. Creating a password-protected ZIP archive using Aspose.ZIP for Python is a straightforward process that offers enhanced security for your compressed files. So let’s see how to protect a ZIP archive with a password in Python.

  • First, create an object of ArchiveEntrySettings class and initialize it with the desired password.
  • Then, create an object of Archive class and initialize it with ArchiveEntrySettings object you have created above.
  • After that, add files and folders to the ZIP archive.
  • Finally, save the ZIP archive using Archive.save() method.

The following code sample shows how to create a password-protect ZIP archive in Python.

Encrypt ZIP Archive with AES Encryption

In the previous example, we applied traditional settings to protect a ZIP archive with a password. However, Aspose.ZIP for Python allows you to use AES encryption to encrypt your ZIP archives. You can use the following AES encryption methods:

  • AES128
  • AES192
  • AES256

The process of applying AES encryption is the same as you have seen in the previous section. The only difference is, that you will now use AesEcryptionSettings instead of TraditionalEncryptionSettings when defining the archive settings. The following code sample demonstrates how to apply AES encryption to the ZIP archives in Python.

Set Parallel Compression Mode

Aspose.ZIP for Python also lets you customize the ZIP archiving process using parallel compression settings. This configuration tells the API when to apply parallel compression. The following parallel compression modes are available.

  • Never - Do not compress in parallel.
  • Always - Do compress in parallel (beware out of memory).
  • Auto - Decide whether to use parallel compression or not upon the entries. This option may compress some entries in parallel only.

The following are the steps to apply parallel compression while creating a ZIP archive in Python.

  • First, create a new archive using Archive class.
  • Then, add files or folders to the ZIP archive (as shown in the previous sections).
  • Create an instance of ParallelOptions class and set parallel_compress_in_memory property to the desired ParallelCompressionMode value.
  • Create an instance of ArchiveSaveOptions class.
  • Assign ParallelOptions object to ArchiveSaveOptions.parallel_options property.
  • Finally, save the ZIP archive using Archive.save() method.

The below code sample shows how to apply parallel compression while zipping files in Python.

Get a Free License

You can get a free temporary license and compress files to a ZIP archive without any evaluation limitations.

Python ZIP Library - Resources

You can explore more about our Python ZIP library using the following resources:

Make ZIP Archives Online

Try our free file zipping application, which is powered by Aspose.ZIP for Python.

Conclusion

In conclusion, Aspose.ZIP for Python offers a robust and versatile solution for creating and managing ZIP archives effortlessly. With its comprehensive set of features, developers can seamlessly integrate ZIP functionality into their Python applications. Whether it’s compressing files, adding encryption, or organizing complex directory structures, Aspose.ZIP provides a reliable toolkit to streamline these tasks.

This article provided you with a complete guide on how to create ZIP archives in Python. In addition, we have covered how to archive multiple files and folders, create password-protected ZIP archives, apply AES encryption, and configure parallel ZIP compression methods. You can easily integrate the library into your Python applications and use the provided code samples.

See also