Click here to Skip to main content
Licence CPOL
First Posted 21 Nov 1999
Views 207,604
Bookmarked 93 times

Creating a Self Extracting Executable

By | 20 Aug 2002 | Article
A class that allows you to create self extracting executables for use in distribution or setup programs

Self Extractor Builder

Introduction

This class allows you to create self extracting executables for use in distribution or setup programs. Many people have helped me along the way to produce this class but here are a couple I would like to thank.

  • Levente Farkas - For the suggestion of how the class might be implemented
  • Roger Allen - For further advice on the reading and writing of the data
  • Jamie Thornback - For help with the callback procedures
  • Tim Johnson - For his CShellFileOp class which is used briefly in this class

A new addition to this class is compression courtesy of Zlib and the following contributors: -

  • Luca Piergentili - For his suggestions and source code contributions for the compression features.
  • Mark Nelson - For his Zlib wrapper class which I mercylessly butchered into my own code.

Description of the Self-Extracting (SFX) executable

The SFX file which this class creates consists of an extraction executable with the data files appended to the end of it. The data on the end of the file does not affect the executable image and so the file executes as if the data wasn't even there. So to extract the data, the executable must first detach the data from itself and then create the approriate files. The way I have chosen to do this is to write a 'Table of Contents' (TOC) after the data which can be read by the extractor to find out where the various files are stored in the data segment.

File Layout

The layout of the TOC is as follows:-

Starting from the end of the archive and working backwards :

Header Info

  • 10 bytes - Signature (Identifier for SFX archive)
  • 4 bytes - Version number of SFX archive
  • 4 bytes - Number of files in archive

Table of Contents

This section contains one record in the following format for each file

  • 4 bytes - Length of filename
  • variable length - Filename
  • 4 bytes - Length of File (compressed)
  • 4 bytes - Length of File (uncompressed)
  • 4 bytes - Offset in archive to data

Data Segment

Each file is the compressed in memory using zlib and then written in the order of the TOC. After this is the extractor executable.

How To Use it

Having said all that, you don't need to know any of that stuff above to use it. All you need to do is create an instance of CSelfExtractor and then call AddFile() to add in all the files that you want to include. Next call Create() to create the archive.

The demo project consists of two projects - 'Extractor' which is the executable which extracts the archive and Self Extractor which is the program for building Self Extracting archives. Self Extractor allows you to specify an external extractor program to use for the archive or alternatively you can use the extractor which has been compiled into the program inside the resources. Read the source code to find out more.

The Zlib source code is subject to the licence documented here. The demos make use of classes written by other people at both codeguru.com and here at codeproject.com so any bugs in those should be directed at their respective authors.

Updates

21st August 2002 - Updated code with fixes suggested by readers relating to file permissions and CFileDialog. Also updated Zlib to v1.14 which fixes an important security problem.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

James Spibey

Web Developer

United Kingdom United Kingdom

Member

James is currently working as a Software Engineer providing large scale Warehouse Management Systems and Airport Baggage Handling Systems. He is a Windows specialist but nowadays spends about 65% of his time fighting with VI in a vain attempt to get his UNIX C code to compile. He has been programming in C/C++ for 6 years and Visual C++/MFC for 4 years.
 
In his spare time James plays a variety of musical instruments including guitar and piano with varying degrees of success. He has been told he spends too much time and money in the pub but doesn't everyone have their own stool at the bar?
 
James is originally from Nottingham (no Robin Hood jokes please) but is now based in sunny Manchester, UK.
 
The attached photo shows James in his favourite position, drinking beer with a hand growing out of his neck.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
SuggestionEat your own dog food! PinmemberCodingBruce7:45 15 May '12  
GeneralExcellen Pinmembersisixiao20:37 28 Feb '11  
GeneralUsing enviroment variables to extract directory PinmemberOGangrel5:14 5 Sep '08  
QuestionMaintaining directory paths PinmemberKlasu2:29 9 Apr '08  
GeneralHai How to setup our required icon PinmemberMember 40674921:20 24 Mar '08  
Generalnot working in vista Pinmemberneha.agarwal2723:22 13 Nov '07  
GeneralRe: not working in vista PinmemberMember 40674921:23 24 Mar '08  
GeneralNice work Pinmemberjtholt8:12 30 Jan '07  
GeneralRe: Nice work PinmemberJames Spibey9:07 30 Jan '07  
GeneralRe: Nice work Pinmemberjtholt7:47 31 Jan '07  
GeneralDecompress all files Pinmemberhector santos4:22 10 Nov '06  
I am trying out his utility and one thing I don't understand.
 
I have a SETUP.EXE program that changes for external files in the same location which the SETUP.EXE started. So if I used WINZIPSE for example
to compress and create a SFX with 4 files:
 
FILE1.INI
FILE2.DLL
FILE3.EXE
SETUP.EXE
 
to create the SFX
 
SETUP-SFX.EXE
 
when this starts, then it will decompress all these files into the TEMP Folder, run SETUP.EXE and SETUP.EXE will see the 3 additional files.
 
With this UTILITY, it seems to only decompress the SETUP.EXE. Not the othe r files so SETUP.EXE never sees the 3 FILE*.* files.
 
What I am missing? Or this utility not one I can use for what I want?
 
Thanks

 
---
HLS

QuestionEver tested with Unicode ? PinmemberKristian Kratzenstein8:14 1 Jan '05  
AnswerRe: Ever tested with Unicode ? PinmemberKristian Kratzenstein8:55 1 Jan '05  
GeneralCompile error in VS.net "Release" PinmemberBo Christensen0:42 14 Oct '04  
GeneralExcellent !!! Pinmembervillager20:45 12 Oct '04  
Generaldigital signature problem. Pinsussdouglashogan14:17 25 Apr '04  
GeneralRe: digital signature problem. Pinmemberdead_alive15:08 13 Jun '04  
GeneralRe: digital signature problem. PinmemberSoft Eval12:03 7 May '07  
GeneralMAX_FILE -- Suggestion PinmemberSekhar20:39 11 Feb '04  
GeneralRe: MAX_FILE -- Suggestion PinmemberJames Spibey21:02 11 Feb '04  
GeneralRe: MAX_FILE -- Suggestion PinmemberSekhar21:14 11 Feb '04  
GeneralRe: MAX_FILE -- Suggestion [modified] Pinmemberjauming5:52 20 Feb '12  
GeneralMaintaining file path information PinmemberMr.X5:47 20 Nov '03  
GeneralRe: Maintaining file path information PinmemberJames Spibey7:42 20 Nov '03  
GeneralRe: Maintaining file path information PinmemberMr.X10:39 20 Nov '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 21 Aug 2002
Article Copyright 1999 by James Spibey
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid