Creating a SFX (Self-extracting archive) with 7-Zip as part of your build process.

I’ve used 7-Zip for two years by now. In the beginning I was bit sceptic about it because of the poor-quality icons in their toolbar (they are not very appealing). Now, I cannot live without it. It even have become part of the tool ecosystem I use at work. Currently, I am using it to package an installer. Let’s see how it works and how this could be valuable to you.

In first place, let’s get the concepts before getting our hands dirty. To build our self-extracting installer, we need three elements:

  1. A SFX header, that is, a piece of binary code which implements the extraction algorithm.
  2. A special config.txt file.
  3. An archive with our software!

SFX header

Download 7z Library (7z-extras) and extract in 7-zip’s folder. This archive contains the SFX for Windows installers.

The SFX header is what contains the binary code needed to build the executable. If you need to change the language, the icon or other “resources”, you might try using Resource Hacker.

The payload for this SFX module is around 150KB. Not bad at all :)

Creating “config.txt”

To edit config.txt file you would better use an advanced text editor like Notepad++. Although this is not documented so well, you need to use Unicode encoding and Unix new line separator ('n', unlike Windows’ "rn").

According to 7-zip’s help, every key-value pair must follow this format:
key="value"

Now let’s see an example config.txt:
;!@Install@!UTF-8!
Title="Title of the Window"
BeginPrompt="Do you want to install this application?"
Progress="yes"
ExecuteFile="setup.exe"
ExecuteParameters="/frompackage"
;!@InstallEnd@!

Tips:

  • If you want to avoid the BeginPrompt message box, just don’t include that line in the result config.txt.
  • Progress is “yes” or “no”. This shows or hides a progress bar while extracting the file.
  • ExecuteFile is the name of the installer (tipically setup.exe). If you just want to call an executable file not included in the SFX, use RunProgram. For instance: “RegSvr32 component.ocx”.
  • ExecuteParameters is what you expect. Most of the time you won’t need it.

7z archive

Well, this step is straightforward. Just check 7-zip help if you need something more advanced, but this should be enough:

PathTo:7z.exe -a archive.7z *.exe *.dll *.xml (whatever files you need to include in your release.)

build.bat file

Once we have the SFX header, our config.txt file and our compressed files, we can build our self-extracting archive. In a console, run the following command:
copy /b PathTo:7zS.sfx + config.txt + archive.7z MyProductInstaller.exe

Finally…

Most IDEs let you run a command if the build have been successful. Just call our build.bat and there you are!

Anuncios
Creating a SFX (Self-extracting archive) with 7-Zip as part of your build process.