How to automate WinRAR to make a single file installer from setup.exe and MSI file

Published:

Introduction

If you want to create a single file installer using Visual Studio, you can either use InstallShield LE, or repackage your setup.exe and MSI file into a self extracting file.  This article explains how to do the latter, with WinRAR, producing a professional looking package.

First of all, you may be tempted to just provide the MSI file for the installation.  You should only do this if you expect your end users to ensure all .NET runtime requirements are present on the install target machine.  This might be adequate in some corporate environments, or for your friends, or for a group of developers, but otherwise Setup.exe bootstrapper should be included with your MSI file.

Creating the install file manually

First of all, we’ll look at the steps involved to create the setup interactively.

Select the Setup.exe and your MSI file and right click.  Select the “Add to Archive…” context menu choice.  Now you can edit the archive name, and check the “Create SFX” checkbox.
General TabClick the “Advanced Options” tab.
Advanced tabClick the “SFX Options” button.  This opens a dialog that allows you to change many of the default behaviours of the generated self extract files.
SFX General
On the General Tab, check “Create in the current folder” and add the option to run Setup.exe after extraction.  This will launch your installer when the user runs the self extractor.
SFX Text and Icon tabOn the Text and Icon tab, you can edit the title of the self extract window and add an icon.  The text appears in the title bar when the extract is running.  The icon is used to represent the self extract file itself in explorer.  It makes a nice professional touch to use your icon instead of the default WinRAR icon.
SFX Update tabOn the Update tab, ensure that “Overwrite all files” is checked.  
SFX Modes tabOn the Modes tab, check the “Hide all” option in silent mode.  This will make sure that the extract occurs without any additional intervention from the user.

Click “Ok” on the bottom of the SFX options dialog, and “Ok” again for the “Add” dialog.  If there are no errors, WinRAR should now create your self extract file.

Well, this is all well and good, but with all these steps, it’s going to be impossible to keep doing this for every new release of your program.  You’ve created an automated build process for your installer—you don’t want to fiddle around with this GUI every time you rebuild.  Fortunately, WinRAR lets us have the exact same functionality through the command line.

Creating the install file from the command line

First let’s create two text files, script.txt and list.txt.  You can put these anywhere that is convenient.  I put them both in the output directory for my installer build process, so that folder contains:
Setup.exe
MSI file
List.txt
Script.txt

The VB.NET code I show later will refer to these files.

List.txt is a file containing the full filenames of the files to include in the self extractor.  Put one file line or use wildcards.

C:\temp\New folder\setup.exe
                      C:\temp\New folder\Setup.msi

Open in new window


Script.txt contains value pairs that configure some of the SFX options.

Title=My Application
                      Silent=1
                      Overwrite=1
                      Setup=setup.exe

Open in new window


Note that the icon will be specified in the command line of the call to WinRAR.

This call will be constructed like this:

"<path to winrar>WinRAR.exe" a "<sfxfilepath>" @"<listfilepath>" –ep –sfx –z"<scriptpath>" -iicon"<pathtoicon>"

Open in new window


In VB.NET I use code similar to this to automate this step:

Private Sub BuildSFX(ByVal OutputPath As String, ByVal OutputFilename As String)
                          Dim ExePath As String = "C:\Program Files\WinRAR\WinRAR.exe"
                          Dim PathString As String = IO.Path.Combine(OutputPath, OutputFilename)
                          Dim ScriptString As String = IO.Path.Combine(OutputPath, "script.txt")
                          Dim ListString As String = IO.Path.Combine(OutputPath, "list.txt")
                      
                          Dim Options As String = String.Format("a  ""{0}"" @""{1}"" -ep -sfx -z""{2}""", PathString, ListString, ScriptString)
                          Options &= " -iicon""C:\Temp\Icons\AppIcon.ico"""
                      
                          Process.Start(ExePath, Options)
                      End Sub

Open in new window


Conclusion

WinRAR gives you the ability to take an existing setup and deployment project that creates a setup.exe and an MSI file, and release a single file installer based on a self-extracting executable.  The creation of such an SFX project can be automated from the command line.  Of course other options are available, both in the GUI and from the command line.  Look to the WinRAR help for information on commands and switches.

I hope you find this useful.  Please take the time to browse my other articles, and please remember to vote if you find this helpful.
2
26,051 Views

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.