Introducing Tcl 8.7 Part 12: Creating ZIP archives

Published

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 a pre-alpha binary for your platform. Alternatively, you can build it yourself from the core-8-branch branch in the Tcl fossil repository.

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.zip blog
% glob *.zip
blog.zip

The above command will create the blog.zip 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 archive.zip [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 blog.zip [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.zip 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 blog.zip {index.md index.md ttpl.md renamed.md}

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

% zipfs lmkzip blog.zip {index.md index.md ttpl.md dir/renamed.md}

will place ttpl.md as file renamed.md 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.

References

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

  2. zipfs man page