9

I'm playing around with 7-Zip to create a self-extracting archive (SFX) executable. I followed the documentation, but I wasn't able to get any close. I'm using the 7-Zip 32 bit and the 7-Zip extra 32 bit.

Here is where I'm stuck first to create an SFX file you need to call the following script:

copy /b 7zS.sfx + config.txt + archive.7z archive.exe

This will generate an archive.exe that will have the config.txt file to run the appropriate program. Here is how the config.txt file look like:

;!@Install@!UTF-8!
Title="7-Zip 4.00"
BeginPrompt="Do you want to install the 7-Zip 4.00?"
RunProgram="setup.exe"
;!@InstallEnd@!`

This will generate archive.exe.

Now further down in the documentation it tells you how to add a file. I need to compress the setup.exe file so I use the following script.

7z a -sfx a.exe setup.exe

I'm confused about this example. Do I use archive.exe instead of a.exe? And if I do that, I get an error saying archive.exe is not a supported archive type.

If I kept the name a.exe and setup.exe to the a and also added the archive.exe file into it using the previous script, and all it did was extracting the file into the local folder instead of executing it.

Why is this happening?

  • Near duplicate of https://stackoverflow.com/q/27904532/1688738. Which has the same answer from @jcrawfor74 too. – Hugh W Oct 03 '22 at 12:13

5 Answers5

14

I have created step by step instructions on how to do this as I also was very confused about how to get this working.

How to make a self extracting archive that runs your setup.exe with 7zip -sfx switch

Step 1 - Setup your installation folder

To make this easy create a folder c:\Install. This is where we will copy all the required files.

Step 2 - 7Zip your installers

  1. Go to the folder that has your .msi and your setup.exe
  2. Select both the .msi and the setup.exe
  3. Right-Click and choose 7Zip --> "Add to Archive"
  4. Name your archive "Installer.7z" (or a name of your choice)
  5. Click Ok
  6. You should now have "Installer.7z".
  7. Copy this .7z file to your c:\Install directory

Step 3 - Get the 7z-Extra sfx extension module

You need to download the 7z-Extra.

  1. Follow this link to go to download 7Zip
  2. You need to download the 9.20 version (as @ May-2015) as the beta does not contain the correct files.
  3. A direct download link.
  4. Extract the 7zip extra files
  5. Copy the file "7zS.sfx" to c:\Install

Step 4 - Setup your config.txt

I would recommend using NotePad++ to edit this text file as you will need to encode in UTF-8, the following instructions are using notepad++.

  1. Using windows explorer go to c:\Install
  2. right-click and choose "New Text File" and name it config.txt
  3. right-click and choose "Edit with NotePad++
  4. Click the "Encoding Menu" and choose "Encode in UTF-8"
  5. Enter something like this:
    ;!@Install@!UTF-8!
    Title="SOFTWARE v1.0.0.0"
    BeginPrompt="Do you want to install SOFTWARE v1.0.0.0?"
    RunProgram="setup.exe"
    ;!@InstallEnd@!
    

Edit this replacing [SOFTWARE v1.0.0.0] with your product name. Notes on the parameters and options for the setup file are here

CheckPoint

You should now have a folder "c:\Install" with the following 3 files:
  1. Installer.7z
  2. 7zS.sfx
  3. config.txt

Step 5 - Create the archive

These instructions I found on the web but nowhere did it explain any of the 4 steps above.
  1. Open a cmd window, Window + R --> cmd --> press enter
  2. In the command window type the following
cd \
cd Install
copy /b 7zS.sfx + config.txt + Installer.7z MyInstaller.exe
  1. Look in c:\Install and you will now see you have a MyInstaller.exe
  2. You are finished

Run the installer

Double click on MyInstaller.exe and it will prompt with your message. Click OK and the setup.exe will run.

P.S. Note on Automation

Now that you have this working in your c:\Install directory I would create an "Install.bat" file and put the copy script in it.
copy /b 7zS.sfx + config.txt + Installer.7z MyInstaller.exe

Now you can just edit and run the Install.bat every time you need to rebuild a new version of you deployment package.

nalse
  • 3
  • 2
jcrawfor74
  • 3,317
  • 2
    In current versions of 7-Zip, the required .sfx file is not part of 7z-Extra download, it is part of the LZMA SDK. The SDK download includes documentation for creating these installers (in DOC/installer.txt) as well as an example config file and batch file with the commands required to do so (in bin/installer). This answer is super helpful, but I would also recommend reading through the documentation as it includes other important details not included here – mark.monteiro Apr 28 '21 at 15:45
6

It's much easier to use the 7-Zip GUI to create a self-extracting archive. Choose the SFX option when creating a 7z archive.

See this wikiHow article for detailed steps.

Screenshot (with freehand circle) of 7-Zip SFX option

  • 3
    This, however, doesn’t provide the features the OP needs. You cannot use different SFX modules and you cannot configure them. – Daniel B Jun 18 '15 at 11:51
6

That's almost right.

In the step:

copy /b 7zS.sfx + config.txt + archive.7z archive.exe

the archive.7z file is the 7-zip archive that contains all your files. This should be the last step.

Before this step, archive.7z should be created, containing setup.exe and any other files it needs. Create this using:

7z a archive.7z setup.exe
prunge
  • 858
1

It's easy with 7zip SFX-Packager (it is open source .bat file) and easy way to just "Drag & drop" folders onto it, and it creates a portable/self-extracting package.

T.Todua
  • 3,938
1

The first script you mentioned creates a self-extracting archive (SFX) executable called archive.exe. This archive contains two files:

  • config.txt: This file contains the configuration settings for the SFX archive.
  • archive.7z: This file is the compressed archive of the files you want to distribute.

The second script you mentioned compresses the setup.exe file into an SFX executable called a.exe. This archive contains only one file:

  • setup.exe: This is the compressed setup file.

You are getting an error when you try to use archive.exe as the input file for the second script because archive.exe is not a compressed file. It is an executable file that contains the SFX code.

To fix this, you need to compress the archive.exe file into an SFX archive. You can do this by using the following command:

7z a -sfx archive.exe archive.exe

This will create a new SFX archive called archive.exe that contains the original archive.exe file.

Once you have created the new SFX archive, you can use it to distribute the archive.exe file. When users double-click on the SFX archive, the archive.exe file will be extracted and executed.

Here are some additional tips for creating SFX archives with 7-Zip:

  • You can use the -sfxmodule option to specify a different SFX module to use for the archive. This can be useful if you want to use a different compression format or if you want to add additional features to the archive.
  • You can use the -sfxoptions option to set additional options for the SFX archive. For example, you can set the icon that will be displayed when the archive is run, or you can specify a password to protect the archive.