Saturday, 10 July 2010

Create a self-extracting archive exe PART 1

Introduction
A self-extracting archive is a zip or similar compressed file that has been turned into an exe (executable). The advantage of this is that the recipient of the file only has to double click the exe and it will do everything automatically. The two actions it would usually perform are:
  1. To run one of the extracted files (to install a program for example)
  2. To extract the files from the compressed archive file (.zip or similar)
If you want to just simply extract files, see PART 2 but in this article we'll explain scenario 1:

Often I need to make self-extracting exe files to send via e-mail. The recipient runs it, the files are extracted and an installation program (an AutoIt script for example) installs the files. A couple of years ago I used WinZip and the WinZip self-extracting archive utility to do this. It worked well but there were two downsides, firstly it was menu driven, I had to click through 'next' screens and select options each time I used it. Secondly, it cost money, not a lot but still...

7-Zip is the answer! It's an alternative to WinZip. You can download 7-Zip from www.7-zip.org, it is an open source program, therefore free to download. But to create a self-extracting exe file using 7-Zip for Windows you have little control. 

After some searching on the web I discovered that with an additional component, a file called 7zS.sfx (sfx stands for self-extracting), I could create self-extracting exe files using 7-Zip. This article explains how to do this! The best thing about the method I explain here is that you can set it up and use it over and over. Instead of going through multiple 'next' options you just run a command file. For anyone working with self-extracting exe files a lot, this is ideal. 


Download
If you've not done it already, download and install 7-Zip for Windows. You can find it here: http://www.7-zip.org/

Also, look on the Downloads page for '7z Library, SFXs for installers, Plugin for FAR Manager' - click to download the 7zXXX_extra.7z file.


Instructions
The 7zXXX_extra.7z is a compressed archive file, like a zip file but in the native 7-Zip format. Extract 7zS.sfx from the 7zXXX_extra.7z file.

Make a new folder, for example SFXfolder.

Copy the 7zS.sfx into the SFXfolder.

Now we have to make two files. The first is a configuration file. It determines how the self-extracting exe will work, what is shown on the screen and the name of the program that will be run automatically after extraction. This configuration file can have any name but to make life easy I recommend you call it '7config.txt'. 

Open Notepad
Type the following:

;!@Install@!UTF-8!
Title="Install Test"
BeginPrompt="Install?"
RunProgram="setup.exe"
;!@InstallEnd@!

Save the file as 7config.txt to the SFXfolder you created before.

To create the self-extracting exe we will need to type a command at the command prompt (CMD). But why do that each time? What we can do is make a command file and just edit/run that file when you want to make your self-extracting exe. You'll see what I mean soon.

Open Notepad
Type the following:

@echo off
copy /b 7zS.sfx + 7config.txt + Install_Test.7z Install_Test.exe
echo.
pause

Save the file as 7makeEXE.cmd to the SFXfolder you created earlier.

Now inside your SFXfolder you should have the following files:
7zS.sfx
7config.txt
7makeEXE.cmd

Now all we need are the files you want to include in the installation package itself. These files, including setup.exe (the file that will be run after extraction) should be compressed into a .7z file. Do not make a .zip file, use 7-Zip's native .7z format. 

Save the .7z file in the SFXfolder. Name the .7z file as Install_Test.7z (or whatever you have used in your 7makeEXE.cmd file).

Double click 7makeEXE.cmd Wait, once it has finished you'll see the Install_Test.exe file in the SFXfolder. It's ready to be used!


Conclusion
Using this method to create self-extracting archive exe file makes a lot of sense if you create and recreate installation packages often. Especially for those deploying files or updating them. Because you only need to recreate your 7z file and double click 7makeEXE.cmd to rebuild the self-extracting exe file. No messing around with lots of Next prompts. Also the 7z format compresses files so small, if you are supplying installation packages over the internet via FTP or by e-mail, this is perfect. I use this every day and I wouldn't go back to WinZip now even if it was free.


Related
To make a self-extracting archive that only extracts files and does not run an EXE file, see:
Create a self-extracting archive exe PART 2

Please also see this article:
http://mgxp.blogspot.com/2010/08/problem-7-zip-sfx-extracted-files.html

Post a Comment