Introducing Tcl 8.7 Part 12: Creating ZIP archives


This is the twelfth in a series of posts about new features in the upcoming version 8.7 of Tcl. The prior post in the series covered mounting of ZIP archives as Tcl virtual file systems, their introspection and using file I/O commands to read and write to files within the archive. This short post concerns itself with utility commands for creating ZIP file archives.

To take Tcl 8.7 for a spin, you can download the source distribution. Binary distributions for Windows are available from magicsplat and BAWT.

Creating ZIP archives from a directory tree

The zipfs mkzip command creates ZIP archives. Creating one from a directory structure on disk is very simple.

% zipfs mkzip blog
% glob *.zip

The above command will create the archive containing the blog directory along with all its contents. The root path set in the ZIP archive will be blog so that ZIP extractors will (by default) create the directory blog on the target system relative to the extractor's current directory and place the directory contents below that.

This can sometimes be inappropriate. For example, consider the following command:

% zipfs mkzip [file join [pwd] blog]

The above will place the absolute path of the blog directory as the root path in the archive. As a consequence, extractors will by default recreate that path on the target system which may not be desired or even feasible; for example, consider that on Windows systems the drive specified in the path may not even exist on the target system.

Thus the zipfs mkzip command allows an additional argument to be specified that is treated as a prefix to be stripped from the input directory path before storing as the root of the archive.

% zipfs mkzip [file join [pwd] blog] [pwd]

This now results in the ZIP archive root being set to just blog as in the first example above.

Finally, a password may be provided for encrypting the ZIP file content. Be it noted that ZIP file encryption is extremely weak and serves mostly as an obfuscation technique rather than providing real privacy.

NOTE: at the time of writing this password functionality appears to be broken in the 8.7 development code base.

% zipfs mkzip blog mysecret

Creating ZIP archives from a file list

Instead of basing the ZIP file structure on an existing directory tree, you can create the archive from a list of arbitrary source files. These can be stored with different names in the archive. The zipfs lmkzip command provides this capability. Instead of the input directory path it takes a list of alternative source file and corresponding target file (as named in the archive) paths.

% zipfs lmkzip {}

The above creates an archive containing and with the latter renamed as The specified paths may not refer to directories but may refer to files whose paths include directories. For example,

% zipfs lmkzip { dir/}

will place as file under the dir directory in the created archive.

Like mkzip, lmkzip also takes an optional password if archive encryption is desired.

Looking to the future...

The enhancements to Tcl for supporting ZIP archives also enabled the deployment of Tcl applications as single file executables without needing any additional external tooling. This will be topic of the next post in this series.


  1. TIP 430: Add basic ZIP archive support to Tcl

  2. zipfs man page