Create Executable Self-extracting Archive in Python

Archiving and compressing files is a common task and it often involves creating ZIP files to efficiently bundle multiple files and folders into a single package. While ZIP files are widely used for this purpose, sometimes you may need to create self-extracting archives (SFX or SEA) for the convenience of your users. Self-extracting archives are executable files that can automatically extract their contents without requiring a separate unzip utility. In this blog post, we’ll explore how to create self-extracting archives programmatically in Python.

Python Library to Create Self-extracting Archives

Aspose.ZIP for Python is a powerful and easy-to-use Python library for working with ZIP archives. It provides various features for creating, manipulating, and extracting ZIP files. One of its notable features is the ability to create self-extracting archives, which can simplify the distribution of files to end users.

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

> pip install aspose-zip

Create a Self-extracting Archive in Python

The following are the steps to create an executable self-extracting archive in Python.

  • First, create an object of Archive class.
  • Then, add files to the archive using Archive.create_entry(string, string) method.
  • Create an object of SelfExtractorOptions class to specify options for self-extracting archive.
  • Set options such as title, icon, etc.
  • Create an object of ArchiveSaveOptions class and set self_extractor_options property to the SelfExtractorOptions object we have just created.
  • Finally, save the self-extracting archive file using Archive.save(string, ArchiveSaveOptions) method.

The following code sample shows how to create an executable self-extracting archive in Python.

Executing Self-extracting Archives using Command Line

Since we have created an executable self-extracting archive, you can execute the archive to extract its content. If you want to use the command line, you can extract the archive with the following command.

C:>archive.exe -autoExtract 

Here, -autoExtract is the primary option that lets you extract the archive. In case the archive is password-protected, you can specify the password using -password option, as shown in the following command.

C:>archive.exe -autoExtract -password:T0p$ecret

Python ZIP Library - Get a Free License

You can create executable self-extracting archives in Python without evaluation limitations by getting a free temporary license.

Learning Resources

Learn more about our Python ZIP library using the resources given below:

Online ZIP Tools

Get access to our free online tools to work with ZIP archives. These tools are powered by Aspose.ZIP for Python and do not require you to create an account.

Conclusion

Creating self-extracting archives can simplify the distribution of files to end users by eliminating the need for a separate unzip utility. Aspose.ZIP for Python makes it easy to create self-extracting archives with just a few lines of code. In this blog post, we’ve walked through the process of creating a self-extracting archive in Python. You can easily integrate the library into your Python applications and simplify the process of working with archives.

See Also