Self-Extracting File Framework






4.55/5 (5 votes)
Jun 26, 2000

112797

1550
An article about creating Self-Extracting files with integrated compression
Introduction
This article is about making self-extracting files (lets call them "sfx"), or more precisely, making the framework for creating sfx.
For a start, let's look at the physical anatomy of a sfx:
sfxLoader.exe |
LOADERHEADER |
StartModule |
User Data |
As you can see, there is an executable (sfxLoader.exe) with some appended data.
sfxLoader
This is the soul of a sfx, a regular Win32 portable executable file that has the responsibility of managing a compressed start module.
For making this I established some goals:
Small footprint: The goal was to make the small possible sfxLoader, and the end the result was a sfxLoader of about 20 KB. This is in part because of some techniques demonstrated in other articles, namely:
- Reduce the Size of Your Final EXE File by Rehan Nadeem,
- Aggressive Optimizations for Visual C++ by Todd C. Wilson.
I have also made a stub. If you want more information on this, start your reading with the file LoaderPriv.h
.
Compression: The compression has left in charge with the well-known zlib.
StartModule
A StartModule is a piece of software that can be either an executable (EXE) or a dynamic link library (DLL). The StartModule has the responsibility of managing the User Data, for example this can be a setup program.
User Data
Here you can put any data that makes sense to the StartModule, since the StartModule will process this data.
LOADERHEADER
This structure contains, among other things, the size and offset of the start module. You should look at the source code for more details.
The Framework
The framework for creating sfx consists mostly in the class CSFXFileCreator
.
The steps for creating a sfx are:
- Create your own class derived from
CSFXFileCreator
and override theAppendStartModule
andAppendUserData
. - Call
CSFXFileCreator::Create
. - Call
CSFXFileCreator::Close
.
You can find the interface of this in the header sfx.h
. Also, there are some helper functions and
classes in the header sfx_helpers.h
. For a real example, take a look at the sample project Compress.
Installing and Building
- Download and extract the code from this article to 'c:\sfx'
- Download and extract the ZLib to the directory 'c:\sfx\ZLib'
- Open and Build the following projects from the Loader workspace ('c:\sfx\Loader\Loader.dsw'):
- Loader
- StartModule
- Compress
NOTE1: You will get some 'LINK : warning LNK4078: multiple ".data" ....', just ignore them.
NOTE2: If you want to build a EXE start module you have to define '_STARTMODULE_IS_EXE' in the Loader.h file, and rebuild all projects in workspace. - Run the 'c:\sfx\bin\Compress.exe' and you will get a sample sfx file called MySFXFile.exe in the directory 'c:\sfx\bin'
License
- You must mention my name in your application documentation/help box, something like, Portions © 2000 by Rui Godinho Lopes <ruiglopes@yahoo.com>.
- I would appreciate an email letting me know that you are using this code.
- I cannot be held responsible of any kind of data lost caused directly or indirectly by the use of this code. Use it at your own risk.
- You cannot sell this code. This is free software.
- If you use this code in a commercial application, you have to give me a copy of your product.
Don't forget, if you have constructive thoughts just email me! Rui Godinho Lopes