28 Jan 2013

Create a self-extracting archive exe PART 2

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)
Previously in PART 1 I explained how to create an exe file that when run, would extract files and execute a program.

Here I'll explain how to make a self-extracting archive that prompts the user to extract files to a location on their hard disk. The instructions are very similar, there are just a few differences and most importantly the configuration file used is different. 


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 7zSD.sfx from the 7zXXX_extra.7z file.

Make a new folder for your project. By 'project' I mean that if you plan to make a self-extracting archive you normally would already have some files you want to include in the archive. It makes sense to put them in a single folder. Also, you can reuse this folder if you update your files at a later stage. For the purposes of this example we'll call our project folder MyProject, but yours could be called anything.

Copy the 7zSD.sfx into the folder MyProject.

Now we have to make two files. The first is a configuration file. It determines how the self-extracting exe will work. 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!
GUIFlags="8+64+4096"
BeginPrompt=" "
ExtractPathText="Extract to:"
InstallPath="C:\\MyFiles"
FinishMessage="Finished"
;!@InstallEnd@!


Save the file as 7config.txt to the MyProject folder 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 7zSD.sfx + 7config.txt + Install_MyFiles.7z Install_MyFiles.exe
echo.
pause


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

Now inside your MyProject folder you should have the following files:

7zSD.sfx
7config.txt
7makeEXE.cmd


Now all we need are the files you want to include in the installation package itself. These files should be compressed into a single .7z file. Do not make a .zip file, use 7-Zip's native .7z format.

Save the .7z file in the MyProject folder. Name the .7z file as Extract_MyFiles.7z (or whatever you have used in your 7makeEXE.cmd file).


Use
Double click the Extract_MyFiles.exe and you'll see a prompt:


It prompts the user to extract the files to a drive and folder. Look back at the 7config.txt we made earlier, you'll see how you can customise this window. The user also has the freedom to click the yellow browse button and select a destination folder themselves.

Once the user clicks OK the files are extracted and they'll get this prompt:


This is just a message to say it's finished. I recommend this as it's good confirmation for the user that all the files have been extracted. Again this is customisable from the 7config.txt file.


Customisation
In the 7zXXX_extra.7z file you downloaded before, there is a help file 7zSD_EN.chm - this includes an explanation of all the commands used in the 7config.txt file. 


Conclusion
Using this method to create self-extracting archive exe file makes a lot of sense if you create and recreate self-extracting files 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, this is perfect.


Reference
If you want to learn how to make a self-extracting archive that runs an EXE at the end, perhaps Setup.exe to install a program - see my first article on this subject: Create a self-extracting archive exe PART 1

No comments: