Over a million developers have joined DZone.

Use NAnt and WinRar to Create a Self Extracting Archive

· Big Data Zone

Learn how you can maximize big data in the cloud with Apache Hadoop. Download this eBook now. Brought to you in partnership with Hortonworks.

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.

Hortonworks DataFlow is an integrated platform that makes data ingestion fast, easy, and secure. Download the white paper now.  Brought to you in partnership with Hortonworks

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}