Create 7z Archive in Python

File compression is a crucial aspect of data management and sharing in today’s digital world. The 7z format is known for its high compression ratio and strong encryption capabilities. If you’re looking to create 7z archives programmatically in Python, let me introduce you to a powerful library that can simplify the 7z archiving task. So in this blog post, we will explore how to create 7z archives in Python effortlessly.

Python Library to Create 7z Archive

Aspose.ZIP for Python is a powerful library that allows developers to work with various archive formats, including ZIP, 7z, TAR, GZIP, and more. It offers a wide range of features for creating, manipulating, and extracting archive files, making it a valuable tool for data compression tasks. We will use it to create the 7z archives in this article.

You can either download the library or install it from PyPI.

> pip install aspose-zip

Create 7z Archive in Python

You can compress either a single file or multiple files to a 7z archive, depending upon the scenario. So let’s have a look at both cases one by one.

Create 7z Archive with Single File

The following are the steps to create a 7z archive for a single file.

  • Create an instance of the SevenZipArchive class.
  • Then, add file into the archive using SevenZipArchive.create_entry() method.
  • Finally, create and save 7z archive using SevenZipArchive.save() method.

The following code sample shows how to create a 7z archive with a single file in Python.

Compress Multiple Files in a 7z Archive

In case you would have multiple files to compress, you will call SevenZipArchive.create_entry() method for each file. The following code sample shows how to compress multiple files to 7z archive in Python.

Create an AES Encrypted 7z Archive in Python

In certain cases, you need to encrypt the 7z archives and protect them with a password. To encrypt 7z archives, Aspose.ZIP supports AES encryption. The following are the steps to create and encrypt 7z archives in Python.

  • First, create an object of SevenZipEntrySettings class and initialize it with the password using SevenZipAESEncryptionSettings class.
  • Then, create an instance of the SevenZipArchive class and initialize it with SevenZipEntrySettings object you have just created.
  • Then, add files to the archive.
  • Finally, create and save 7z archive using SevenZipArchive.save() method.

The following code sample shows how to create an encrypted 7z archive in Python.

Get a Free License

You can get a free temporary license and compress files to 7z archives without any evaluation limitations.

Python 7z Library - Resources

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

Make 7z Archives Online

Try our free app to create 7z files online, which is powered by Aspose.ZIP for Python.

Conclusion

In this blog post, we’ve provided a straightforward way of creating 7z archives in Python. By following the steps outlined here, you can harness the potential of the Python 7z library to streamline your file compression tasks, saving both time and storage space. You can easily integrate Aspose.ZIP for Python in your application and compress files to 7z effortlessly.

See also