Is it possible to execute a file after extraction from a 7-Zip self-extracting archive (SFX) archive?

41

15

I'm try to make a compressed deploy-able application.

Currently I'm taking a set of files that I've published from my IDE (Visual Studio 2008 - WPF published application) and compressing them in a 7-Zip SFX archive.

My users have asked if they can "one click install" from the 7-Zip exe. There is a parameter list when I'm creating the archive. Is there a way to set some sort of combination of parameters to invoke the installer executable that is extracted?

Unfortunately the 7-Zip documentation doesn't seem to cover this scenario. I've seen several paid applications that do something similar to this, but I'm trying to keep the tool that I'm writing free of licensed code so that we can use it internally.

Mike G

Posted 2009-09-17T17:18:40.890

Reputation: 513

Could you point to some paid one's ? – abhishek77in – 2015-09-21T03:43:24.220

Answers

35

You will need to download the LMZA SDK from 7-Zip. The "installer.txt" file contains documentation:

7zSD.sfx is SFX module for installers. 7zSD.sfx uses msvcrt.dll.

SFX modules for installers allow to create installation program. Such module extracts archive to temp folder and then runs specified program and removes temp files after program finishing. Self-extract archive for installers must be created as joining 3 files: SFX_Module, Installer_Config, 7z_Archive. Installer_Config is optional file. You can use the following command to create installer self-extract archive:

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

You need to create a config.txt file.

Config file contains commands for Installer. File begins from string ;!@Install@!UTF-8! and ends with ;!@InstallEnd@!. File must be written in UTF-8 encoding. File contains string pairs:

ID_String="Value"

Title Title for messages

BeginPrompt Begin Prompt message

Progress Value can be "yes" or "no". Default value is "yes".

RunProgram Command for executing. Default value is "setup.exe". Substring %%T will be replaced with path to temporary folder, where files were extracted

Directory Directory prefix for "RunProgram". Default value is ".\"

ExecuteFile Name of file for executing

ExecuteParameters Parameters for "ExecuteFile"

For example:

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

Molly7244

Posted 2009-09-17T17:18:40.890

Reputation:

5

Note that the "SFX modules were moved to LZMA SDK package" (available on 7-zip.org) as of version 9.35 beta.

– Lukas – 2015-12-03T02:28:10.590

1yes. I have tried it. A bit uncomfortable coz everything is command line, but you will find good documentation for it. And I get the best compression available for free. – Ganesh R. – 2009-09-17T17:47:40.567

@MikeG -- That link is much more thorough. It explained how to make the decompression portion completely transparent. Very nice -- Thanks! – namuol – 2013-01-03T11:07:58.150

23

The agreed solution is correct, but it actually doesn't help in explaining what you actually need to do!. I read this and many other solutions and I could not find full and complete simple instructions on how to do this.

I you are confused like I was, please read my blog post which provides step by step instructions on how to achieve what Molly7244 has posted.

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

It was brought to my attention that links can become dead so, here are the steps, from my blog

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 from the 7z SDK

You need to download the 7z SDK

  1. Follow this link to go to download 7Zip
  2. You need to download the 19.00 version (as @ Dec 2019) as the beta does not contain the correct files.
  3. A SDK download link.
  4. Extract the 7zip SDK files.
  5. Copy the file "7zSD.sfx" from the bin directory of the SDK 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 7zSD.sfx + config.txt + Installer.7z MyInstaller.exe
    
  3. Look in c:\Install and you will now see you have a MyInstaller.exe

  4. 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 7zSD.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.

jcrawfor74

Posted 2009-09-17T17:18:40.890

Reputation: 2 547

3This should be the accepted answer! – noelicus – 2016-04-06T11:58:36.480

Is it possible to pass a command line arg to the ExecuteParameters item in config.txt? For example: C:\Install\MyInstaller.exe some_file.txt would execute setup.exe some_file.txt. – dthor – 2016-05-10T20:07:00.403

Yes it is. As matthew-lundberg said in his answer, "Arguments passed to the SFX executable are passed along to the Install.cmd script"

– dthor – 2016-05-10T20:13:20.793

8

You should also look at NSIS. With it, you can create an installer that is compressed with the same method that 7-Zip uses (LZMA), so it should be roughly the same size, and you have full control over the installation process.

Matthew Talbert

Posted 2009-09-17T17:18:40.890

Reputation: 1 131

2

Building on jcrawfor74's answer, here is some additional info:

Instead of 7zS.sfx, you can also use the file 7zS2.sfx. This is a much smaller version (28 kB vs. 140 kB) but it doesn't support the config.txt file. So you can just leave it out in the copy command. If you just want to create tiny configuration scripts, this lets you build smaller EXE files.

The smaller SFX module doesn't ask any questions. It extracts the files to a temporary folder as the other module, and then finds executable candidates. According to the source code (also available on the downloads page), in C\Util\SfxSetup\SfxSetup.c, it weighs files differently based on their name and extension and then picks the highest scoring file.

Both SFX modules automatically run as Administrator, at least in Windows 10, and this cannot be configured. While this behaviour might be desirable for system-wide setups, this may not always be the case. Also, this decision is only based on the file version data because there is no manifest included that explicitly tells Windows what to do. Windows compatibility looks at the file and finds strings like "Setup" that lead to the elevation prompt.

If you need an SFX module that doesn't ask for Admin permissions, you could rebuild the SFX module from source, or simpler, edit its file version info with a resource editor like this one. Just remove the word "Setup" from the FileDescription field, in the case of the mentioned Resource Hacker compile the file, and save the EXE file. You could also perform this step on the 7zS.sfx or 7zS2.sfx file directly and keep it somewhere so you can use it to build non-elevated SFX packages. And you can use this to fully customise the file version info to put your product name or anything in it that describes the payload.

ygoe

Posted 2009-09-17T17:18:40.890

Reputation: 1 597