Click here to Skip to main content
11,582,850 members (71,430 online)
Click here to Skip to main content

Creating a Self Extracting Executable

, 20 Aug 2002 CPOL 239.5K 4.9K 101
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

James Spibey
Web Developer
United Kingdom United Kingdom
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.

You may also be interested in...

Comments and Discussions

 
Questionnot support unicode Pin
jacky_zz20-Jul-12 3:21
memberjacky_zz20-Jul-12 3:21 
SuggestionEat your own dog food! Pin
CodingBruce15-May-12 7:45
memberCodingBruce15-May-12 7:45 
GeneralExcellen Pin
sisixiao28-Feb-11 20:37
membersisixiao28-Feb-11 20:37 
GeneralUsing enviroment variables to extract directory Pin
OGangrel5-Sep-08 5:14
memberOGangrel5-Sep-08 5:14 
QuestionMaintaining directory paths Pin
Klasu9-Apr-08 2:29
memberKlasu9-Apr-08 2:29 
GeneralHai How to setup our required icon Pin
Member 406749224-Mar-08 1:20
memberMember 406749224-Mar-08 1:20 
Generalnot working in vista Pin
neha.agarwal2713-Nov-07 23:22
memberneha.agarwal2713-Nov-07 23:22 
GeneralRe: not working in vista Pin
Member 406749224-Mar-08 1:23
memberMember 406749224-Mar-08 1:23 
GeneralNice work Pin
jtholt30-Jan-07 8:12
memberjtholt30-Jan-07 8:12 
GeneralRe: Nice work Pin
James Spibey30-Jan-07 9:07
memberJames Spibey30-Jan-07 9:07 
GeneralRe: Nice work Pin
jtholt31-Jan-07 7:47
memberjtholt31-Jan-07 7:47 
GeneralDecompress all files Pin
hector santos10-Nov-06 4:22
memberhector santos10-Nov-06 4:22 
QuestionEver tested with Unicode ? Pin
Kristian Kratzenstein1-Jan-05 8:14
memberKristian Kratzenstein1-Jan-05 8:14 
AnswerRe: Ever tested with Unicode ? Pin
Kristian Kratzenstein1-Jan-05 8:55
memberKristian Kratzenstein1-Jan-05 8:55 
GeneralCompile error in VS.net "Release" Pin
Bo Christensen14-Oct-04 0:42
memberBo Christensen14-Oct-04 0:42 
GeneralExcellent !!! Pin
villager12-Oct-04 20:45
membervillager12-Oct-04 20:45 
Generaldigital signature problem. Pin
douglashogan25-Apr-04 14:17
sussdouglashogan25-Apr-04 14:17 
Hi James,
your class depends on the file not being changed after you have written the data out. If I now digitally sign the file with a digital certificate, this adds data to the end of the file. Because your whole implementation depends on reading signatures and TOCs etc from the end of the file, errors are generated when we try to extract the files. I think this is a major problem as more and more software is getting distributed as digitally signed self extracting executables. I will modify your code to make it work for me without the end of file dependency, but it is something to keep in mind.

thanks
Douglas
GeneralRe: digital signature problem. Pin
dead_alive13-Jun-04 15:08
memberdead_alive13-Jun-04 15:08 
GeneralRe: digital signature problem. Pin
Soft Eval7-May-07 12:03
memberSoft Eval7-May-07 12:03 
GeneralMAX_FILE -- Suggestion Pin
Sekhar11-Feb-04 20:39
memberSekhar11-Feb-04 20:39 
GeneralRe: MAX_FILE -- Suggestion Pin
James Spibey11-Feb-04 21:02
memberJames Spibey11-Feb-04 21:02 
GeneralRe: MAX_FILE -- Suggestion Pin
Sekhar11-Feb-04 21:14
memberSekhar11-Feb-04 21:14 
GeneralRe: MAX_FILE -- Suggestion Pin
jauming20-Feb-12 5:52
memberjauming20-Feb-12 5:52 
GeneralMaintaining file path information Pin
Mr.X20-Nov-03 5:47
memberMr.X20-Nov-03 5:47 
GeneralRe: Maintaining file path information Pin
James Spibey20-Nov-03 7:42
memberJames Spibey20-Nov-03 7:42 
GeneralRe: Maintaining file path information Pin
Mr.X20-Nov-03 10:39
memberMr.X20-Nov-03 10:39 
GeneralRe: Maintaining file path information Pin
jauming19-Feb-12 22:06
memberjauming19-Feb-12 22:06 
Questionhow to add the zlib folder Pin
Ricky Koh3-Sep-03 0:27
memberRicky Koh3-Sep-03 0:27 
QuestionExecute without Extract? Pin
Rogerio Silva5-May-03 17:14
memberRogerio Silva5-May-03 17:14 
AnswerRe: Execute without Extract? Pin
James Spibey6-May-03 5:37
memberJames Spibey6-May-03 5:37 
GeneralRe: Execute without Extract? Pin
Rogerio Silva13-Aug-03 8:53
memberRogerio Silva13-Aug-03 8:53 
GeneralRe: Execute without Extract? Pin
James Spibey13-Aug-03 9:38
memberJames Spibey13-Aug-03 9:38 
GeneralRe: Execute without Extract? Pin
Rogerio Silva16-Jun-04 11:29
memberRogerio Silva16-Jun-04 11:29 
GeneralRe: Execute without Extract? Pin
Anonymous23-Dec-04 3:10
sussAnonymous23-Dec-04 3:10 
GeneralRe: Execute without Extract? Pin
Mars10514-May-07 23:12
memberMars10514-May-07 23:12 
GeneralExecute unpacked file... Pin
Luke_en21-Feb-03 9:04
memberLuke_en21-Feb-03 9:04 
GeneralNice Class James Pin
Brian Delahunty21-Aug-02 12:02
editorBrian Delahunty21-Aug-02 12:02 
GeneralRe: Nice Class James Pin
James Spibey21-Aug-02 21:04
editorJames Spibey21-Aug-02 21:04 
GeneralRe: Nice Class James Pin
Brian Delahunty22-Aug-02 9:10
editorBrian Delahunty22-Aug-02 9:10 
GeneralRe: Interesting but... Pin
James Spibey21-Aug-02 23:22
editorJames Spibey21-Aug-02 23:22 
Generalinternal error at another machine Pin
Anonymous20-Aug-02 19:13
sussAnonymous20-Aug-02 19:13 
GeneralRe: internal error at another machine Pin
James Spibey20-Aug-02 21:47
editorJames Spibey20-Aug-02 21:47 
GeneralVery Good!!! - One bug fix. Pin
Matthew R. Miller17-Aug-02 11:52
memberMatthew R. Miller17-Aug-02 11:52 
GeneralCancel Problem Pin
Isaiah19-Jun-02 12:59
memberIsaiah19-Jun-02 12:59 
QuestionExtract on startup? Pin
Dustin28-May-02 12:22
memberDustin28-May-02 12:22 
AnswerRe: Extract on startup? Pin
James Spibey28-May-02 22:17
subeditorJames Spibey28-May-02 22:17 
GeneralRe: Extract on startup? Pin
Dustin29-May-02 19:32
memberDustin29-May-02 19:32 
GeneralRe: Extract on startup? Pin
James Spibey29-May-02 21:10
subeditorJames Spibey29-May-02 21:10 
GeneralRe: Extract on startup? Pin
Dustin29-May-02 21:26
memberDustin29-May-02 21:26 
Generalerrors Pin
shotgun23-Mar-02 2:46
membershotgun23-Mar-02 2:46 

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.

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