Create self-extracting (SFX) archive
Aspose.ZIP allows you to create a self-extracting (SFX) archive. This is a special kind of file that is both a valid ZIP archive and an executable program. It has .exe extension. You can extract a self-extracting file by double-clicking it.
Creation self-extracting archive
To create a self-extracting archive, instantiate SelfExtractorOptions and pass it to ArchiveSaveOptions when saving.
1 using (FileStream zipFile = File.Open("archive.exe", FileMode.Create))
2 {
3 using (var archive = new Archive())
4 {
5 archive.CreateEntry("entry.bin", "data.bin");
6 var sfxOptions = new SelfExtractorOptions()
7 {
8 ExtractorTitle = "Extractor",
9 CloseWindowOnExtraction = true,
10 TitleIcon = "C:\\pictorgam.ico"
11 };
12 archive.Save(zipFile, new ArchiveSaveOptions() { SelfExtractorOptions = sfxOptions });
13 }
14 }Running self-extracting archive
The archive you produce is executable and requires .NET Framework 2.0 or higher to run. Such frameworks are included with Windows Vista and later versions. You can execute it as a regular program by double-clicking, or run it via the command-line interface.
If you want extraction to start automatically, specify the -autoExtract command-line option. For example:
1 C:\>archive.exe -autoExtract -password:T0p$ecretCommand line options for self-extracting archive
| Option | Meaning | Sample |
|---|---|---|
| -autoExtract | Primary option — without it, decompression does not start. Extraction starts automatically, and other options may be applied. | sfx.exe -autoExtract |
| -autoClose | Window closes when the extraction is complete. This option only works if the -autoExtract option is present. | sfx.exe -autoExtract -autoClose |
| -forceOverwrite | Overwrites all existing files without prompt if there are any. This option only works if the -autoExtract option present. | sfx.exe -autoExtract -forceOverwrite |
| -password: | Provides a password to encrypted entries. This option only works if the -autoExtract option is present. If you have spaces within the password quote it. | sfx.exe -autoExtract -password:T0p$ecret |
| -destination: | Extracts files to the specified directory. This option only works if the -autoExtract option is present. If your path contains spaces, enclose it in quotes. | sfx.exe -autoExtract -destination:"C:\My Documents" |
You can use these options when composing batch scripts on Windows.