Use NAnt and WinRar to Create a Self Extracting Archive

In a recent project, NAnt has been an integral part of creating an installer for a .NET web application with a database back-end. The final stage in creating the ready to deploy executable is packaging the files in a self-extracting archive that extracts and runs the setup. With the help of Winrar and NAnt, that task can be automated.Let's get started.

Requirements:

WinRar SFX options file sample:

; The path to the setup executable
Setup=Setup.msi
; Extract the files to a temporary directory
TempMode
; Use semi-silent mode
Silent=2
; Overwrite any existing files
Overwrite=1
; The title of the SFX archive
Title=Sample Setup
; The text to show initially when the user clicks on the SFX archive (will only matter if using Silent=0)
Text
{
The installer will extract and run the setup
}

NAnt build file sample:

<project name="SampleSFX" default="createSFX" basedir=".">
<description>createSFX task for creating a sample self-extracting installer</description>

<!-- The name of the archive to be created -->
<property name="archiveName" value="SampleSFX" overwrite="false" />

<!-- The target directory from where the build will be invoked -->
<property name="targetDirectory" value="." overwrite="false" />

<!-- The relative path (from the targetDirectory) to directory containing the files to be archived -->
<property name="archiveDirectory" value="<RelativePathToTheDirectoryToArchive>" overwrite="false">

<!-- The mask of the file extensions to be archived -->
<property name="archiveFileMask" value="*.*" overwrite="false" />

<!-- The path to the winrar executable -->
<property name="winrarPath" value="c:\program files\winrar\winrar.exe" overwrite="false" />

<!-- The winrar command line switches for creating the archive -->
<property name="winrarSwitches" value="a -ep -ep1 -r -sfxdefault.sfx" overwrite="false" />

<!-- The name of the sfx options file to use while creating the archive -->
<property name="sfxOptionsFile" value="sfxoptions.txt" overwrite="false" />

<!-- The relative path (from the targetDirectory) to icon file to be used for the created archive -->
<property name="sfxIconFile" value="images\setupIcon.ico" overwrite="false" />

<property name="archiveDirectoryFullPath" value="${path::combine(path::get-full-path(targetDirectory), archiveDirectory)}" overwrite="true" />
<property name="sfxOptionsFileFullPath" value="${path::combine(path::get-full-path(targetDirectory), sfxOptionsFile)}" overwrite="true" />
<property name="sfxIconFileFullPath" value="${path::combine(path::get-full-path(targetDirectory), sfxIconFile)}" overwrite="true" />

<target name="createSFX" description="Creates the self-extracing installer archive">
<exec
basedir="."
program="${winrarPath}"
commandline="${winrarSwitches} -z&quot;${sfxOptionsFileFullPath}&quot; -iicon&quot;${sfxIconFileFullPath}&quot; &quot;${archiveName}&quot; &quot;${archiveDirectoryFullPath}\${archiveFileMask}&quot;"
workingdir="."
failonerror="true" />
</target>
</project>

To run this NAnt task:

  1. Open a command prompt in the targetDirectory you specified in the above build file.
  2. Run the build file with: nant -buildfile:<nameOfTheAboveBuildFile.build>

This will create a single executable file. When the user runs the executable, the setup files will be extracted and the "Setup.msi" (or whatever you specified in the sfx options file) will be executed.

0

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Victor Sergienko replied on Wed, 2008/04/02 - 12:44pm

If one created setup.exe with MSBuild, as a .NET bootstrapper, this won't work.

setup.exe of MSBuild exits BEFORE msiexec opens MSI file, so rarsfx deletes MSI before msiexe installs it.

I had to stick to WinZip self-extractor for .NET bootstrapper.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.