Pages

10 Jul 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

8 comments:

  1. i am able to create SFX exe but when it is double clicked it shows extraction taking place but it does not extract plz help..

    ReplyDelete
  2. Thanks Sr.Mjh, I'm glad I could help :-)

    Satyam, I am not sure, perhaps check the file names you use in the COPY command (inside 7makeEXE.cmd) exist. If the Install_Test.7z doesn't exist then 7makeEXE.cmd will create a self-extracting archive EXE but it will not work because it has nothing in it. Let me know how you get on...

    ReplyDelete
  3. I followed ur instrunction and work fine, but when i try to create a VBscript that will automate the procedure i m facing a problem with the copy command, and the file is corrupted after double click on the SFX file, would you able to help me with the Copy command in vbscript
    thank you

    ReplyDelete
  4. I'm sorry I didn't see your message before Jean Pierre. In any case I haven't worked with VBscript much :-( I use AutoIt mostly. But anyway, perhaps it's a timing issue in your script, maybe it is trying to create the exe before the 7z file has finished compressing? That's just an idea.

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. Can I specify the location, where it extracts files?

    ReplyDelete
    Replies
    1. Finally I got around to writing an article about this! Sorry for the delay but here it is: http://mgxp.blogspot.ch/2013/01/create-self-extracting-archive-exe-part.html

      Delete
  7. Hi Raido, when running an exe on extraction, I've never wanted to change the extraction folder. By default it makes one under the default temp folder on your PC. When your exe closes the temp folder is deleted. Take care in this situation, look at this http://mgxp.blogspot.ch/2010/08/problem-7-zip-sfx-extracted-files.html --- On the other hand, if you want to make a self-extracting archive that does not run an exe but instead just extracts files, yes that's possible. I should probably write another post on that subject though ;-)

    ReplyDelete