Use NAnt and WinRar to Create a Self Extracting Archive

DZone 's Guide to

Use NAnt and WinRar to Create a Self Extracting Archive

· Big Data Zone ·
Free Resource

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.


WinRar SFX options file sample:

; The path to the setup executable
; Extract the files to a temporary directory
; Use semi-silent mode
; Overwrite any existing files
; 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)
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">
commandline="${winrarSwitches} -z&quot;${sfxOptionsFileFullPath}&quot; -iicon&quot;${sfxIconFileFullPath}&quot; &quot;${archiveName}&quot; &quot;${archiveDirectoryFullPath}\${archiveFileMask}&quot;"
failonerror="true" />

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.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}