Home / Alt manpages / unzipsfx(1)

  • unzipsfx(1)
  • User command
  • linux

Build and Test a Self-Extracting ZIP with unzipsfx

Need to hand someone a file that unpacks itself with no unzip command required on their end? unzipsfx is the stub that makes that possible on Linux. You will turn an ordinary ZIP file into a self-extracting archive, make it executable, repair its internal offsets and test it without unpacking over useful files. The installed package here is unzip version 6.0-28ubuntu4.1; its stub identifies itself as Info-ZIP UnZipSFX 6.00.

Allow about fifteen minutes. You need zip, unzipsfx and a shell. The build commands are ordinary user commands when the files are in your working directory. Do not use sudo for this workflow.

1. Check the installed stub

Confirm which executable you will prepend. This matters because the resulting file contains that executable and is generally tied to its operating system and architecture.

$ command -v unzipsfx
/home/linuxbrew/.linuxbrew/bin/unzipsfx
$ unzipsfx -h
UnZipSFX 6.00 of 20 April 2009, by Info-ZIP (http://www.info-zip.org).

The help text is short because UnZipSFX omits several normal unzip features to keep the stub small. In particular, it has no ordinary archive listing mode. Its -t test option is the practical substitute.

Checkpoint

Continue only if command -v found the program you intend to distribute. If it found nothing, install the package through your normal package-management process and repeat this check.

2. Make a normal ZIP in a clean directory

Put the files you want to distribute in a staging directory. Use a new directory so that a later extraction test cannot overwrite unrelated work.

$ mkdir sfx-staging
$ printf '%s\n' 'Read this first.' > sfx-staging/README.txt
$ printf '%s\n' 'Example configuration.' > sfx-staging/config.example
$ (cd sfx-staging && zip -r ../letters.zip README.txt config.example)
  adding: README.txt (stored 0%)
  adding: config.example (stored 0%)

The ZIP is the source archive. Keep it until you have tested the finished SFX file; it is the simplest recovery path if the concatenation step is mistyped.

3. Prepend the stub and repair the archive

Concatenate the stub first and the ZIP second. The output file is an executable program, not just a renamed ZIP.

$ cat "$(command -v unzipsfx)" letters.zip > letters
$ chmod 755 letters
$ zip -A letters
Zip entry offsets appear off by 91296 bytes - correcting...

zip -A adjusts the ZIP offsets for the bytes added by the stub. Run it against the combined file before distributing it. The exact byte count in its diagnostic depends on the installed stub, so do not script against that number.

Safety warning

The > redirection truncates an existing destination before cat starts. Choose a new name such as letters, or stop first if a file with that name already contains something useful. To undo this build, delete only the newly created SFX file and recreate it from the retained letters.zip; do not delete the source archive as part of cleanup.

Checkpoint

Verify that the file is executable and that the repair command completed:

$ test -x letters && echo 'executable SFX ready'
executable SFX ready
$ file letters
letters: ELF 64-bit LSB pie executable

Your file description may differ with the platform and compiler. It should identify an executable, not a plain text file.

4. Test without extracting files

Run the embedded archive's test mode from the directory containing the SFX file. The first q suppresses routine messages; the second keeps the output to a summary-style result on this version.

$ ./letters -tqq
UnZipSFX 6.00 of 20 April 2009, by Info-ZIP (http://www.info-zip.org).
$ status=$?
$ printf 'test status: %s\n' "$status"
test status: 0

Status zero means the archive passed the test. A non-zero status is a failure, so do not distribute the file or assume extraction will work. Keep the original ZIP and inspect the output from an unsuppressed test with ./letters -t.

Because the stub does not provide -l or -v, testing is also a useful way to confirm that all intended members are present. If you need a human-readable inventory before building, run unzip -l letters.zip on the original ZIP.

5. Extract into a disposable destination

A plain invocation extracts all members into the current directory, recreating paths as needed. Test that behaviour in a new destination. This installed build supports the documented -d option.

$ mkdir extraction-test
$ ./letters -n -q -d extraction-test
$ find extraction-test -type f -printf '%P\n' | sort
README.txt
config.example

The -n modifier means never overwrite an existing file. -q reduces output, and -d extraction-test selects the destination. Unlike an ordinary ZIP listing, the SFX program finds its archive by finding itself, so invoke it as ./letters or with a path that resolves to the file.

To remove this test result, first check the destination path, then remove only that disposable directory with your usual file-management command. If you instead need to refresh an existing destination, choose the overwrite behaviour deliberately: -n protects existing files, while -o overwrites without prompting. Do not use -o in a script unless replacing files is explicitly intended.

6. Extract a selected member safely

Members can be supplied after the options, and shell wildcards should be quoted so the shell does not expand them against files in your current directory.

$ mkdir selected-test
$ ./letters -n -q -d selected-test '*.txt'
$ find selected-test -type f -printf '%P\n'
README.txt

Use -x for exclusions, for example ./letters -n -q -d selected-test -x '*.txt' to skip text files. The patterns match archive member names, including directory separators. Check the resulting file list after every filtered extraction; a pattern that matches nothing is easy to mistake for a successful selective deployment.

7. Know the portability boundary

An UnZipSFX archive is portable only as far as the embedded executable is. An SFX built on one Unix flavour normally needs a compatible system and architecture; it is not a universal ZIP replacement. The same archive can usually still be read with regular unzip, although the prepended bytes may produce a harmless warning. It is technically not a plain ZIP file, and some other archive tools may reject it.

If recipients use different systems, distribute letters.zip as well, or provide an SFX built for each supported target. If a recipient reports that the SFX cannot find itself, have them invoke it from its own directory with ./letters or use an explicit relative or full path. UnZipSFX does not generally search PATH to locate its own file.

Done means

  • Stub identified. unzipsfx was identified and its version was recorded.
  • Source kept. The original letters.zip was retained.
  • Archive built. The stub was prepended, permissions were set, and zip -A repaired offsets.
  • Test passed. ./letters -tqq returned status zero.
  • Extraction proven. Extraction succeeded in a disposable directory without overwriting files.
  • Portability flagged. Recipients were told the executable is platform-specific and that the normal ZIP is the fallback.