zipfs(3tcl)

πŸ’– zipfs β€” the 3tcl manual page, bestie edition πŸ’–


✨ NAME ✨

zipfs β€” mount and work with ZIP files within Tcl. yes really. an entire virtual filesystem, inside a zip file, inside Tcl. the range? unmatched. 🫢


🌈 SYNOPSIS 🌈

zipfs canonical ?mountpoint? filename
zipfs exists filename
zipfs find directoryName
zipfs info filename
zipfs list ?(-glob|-regexp)? ?pattern?
zipfs lmkimg outfile inlist ?password? ?infile?
zipfs lmkzip outfile inlist ?password?
zipfs mkimg outfile indir ?strip? ?password? ?infile?
zipfs mkkey password
zipfs mkzip outfile indir ?strip? ?password?
zipfs mount ?zipfile? ?mountpoint? ?password?
zipfs mountdata data mountpoint
zipfs root
zipfs unmount mountpoint

🌸 DESCRIPTION 🌸

okay so zipfs gives Tcl the power to mount the contents of a ZIP archive file as a virtual file system. it's giving "the files are real to Tcl and that's all that matters." πŸ’…

now, Tcl's ZIP archive support is limited to basic features and options, and i need you to hear me on this. supported storage methods include only STORE and DEFLATE, with optional simple encryption. and that simple encryption is "sufficient to prevent casual inspection of their contents" but honestly? it is not able to prevent access by even a moderately determined attacker. so like. do not put your secrets in there. periodt. πŸ’€

also not supported: strong encryption, multi-part archives, platform metadata, zip64 formats, and other compression methods like bzip2. none of it. nope.

files within mounted archives can be written to, but you cannot create new files or directories. and the modifications to files? they're limited to the mounted archive in memory and are not persisted to disk. so when it's gone, it's gone. 😭

one more thing: paths in mounted archives are case-sensitive on all platforms. all of them. no exceptions. 🌈


πŸ¦‹ zipfs canonical ?mountpoint? filename

this takes the name of a file, filename, and produces where it would be mapped into a zipfs mount as its result. if you pass it, mountpoint says within which mount the mapping will be done. if you leave it out, the main root of the zipfs system is used. slay.

πŸŽ€ zipfs exists filename

returns 1 if the given filename exists in the mounted zipfs, and 0 if it does not. simple. clean. a vibe check for files. ✨

πŸ’œ zipfs find directoryName

returns the list of paths under directory directoryName β€” and bestie, directoryName need not be within a zipfs mounted archive. the paths are prefixed with directoryName. this command is also used by the zipfs mkzip and zipfs mkimg commands, iykyk. 🧚

🌟 zipfs info file

returns information about the given file in the mounted zipfs. the information consists of:

  1. the name of the ZIP archive file that contains the file,
  2. the size of the file after decompressions,
  3. the compressed size of the file, and
  4. the offset of the compressed data in the ZIP archive file.

and here's the special case, don't skip it: querying the mount point gives the start of the zip data as the offset in (4), which can be used to truncate the zip information from an executable. but querying an ancestor of a mount point will raise an error. so watch out. 🚨

πŸ”₯ zipfs list ?(-glob|-regexp)? ?pattern?

if pattern is not specified, the command returns a list of files across all zipfs mounted archives. if pattern is specified, only those paths matching the pattern are returned.

by default, or with the -glob option, the pattern is treated as a glob pattern and matching is done as described for the string match command. alternatively, the -regexp option may be used to specify the matching pattern as a regular expression.

the file names are returned in arbitrary order (so don't come for me about the ordering, it's not that kind of list πŸ’…). also note that path separators are treated as ordinary characters in the matching, so forward slashes should be used as path separators in the pattern. and the returned paths only include those actually in the archive β€” it does not include intermediate directories in mount paths.

πŸ‘‘ zipfs mount

πŸ‘‘ zipfs mount mountpoint

πŸ‘‘ zipfs mount zipfile mountpoint ?password?

the zipfs mount command mounts ZIP archives as Tcl virtual file systems and returns information about current mounts. three forms, three moods:

  • no arguments β†’ the command returns a dictionary mapping mount points to the path of the corresponding ZIP archive.
  • single argument form β†’ the command returns the file path of the ZIP archive mounted at the specified mount point.
  • third form β†’ the command mounts the ZIP archive zipfile as a Tcl virtual filesystem at mountpoint.

after that third form executes, files contained in zipfile will appear to Tcl to be regular files at the mount point. if mountpoint is specified as an empty string, it is defaulted to the [zipfs root]. the command returns the normalized mount point path.

if not under the zipfs file system root, mountpoint is normalized with respect to it. for example, a mount point passed as either mt or /mt would be normalized to //zipfs:/mt (given that zipfs root returns //zipfs:/). an error is raised if the mount point includes a drive or UNC volume.

NB: because the current working directory is a concept maintained by the operating system, using cd into a mounted archive will only work in the current process, and then not entirely consistently (e.g., if a shared library uses direct access to the OS rather than through Tcl's filesystem API, it will not see the current directory as being inside the mount and will not be able to access the files inside the mount). so like. be aware. πŸ™ˆ

🌈 zipfs mountdata data mountpoint

mounts the ZIP archive content data as a Tcl virtual filesystem at mountpoint. that's the whole thing. she's straightforward and we respect her. πŸ’–

⭐ zipfs root

returns a constant string which indicates the mount point for zipfs volumes for the current platform. user should not rely on the mount point being the same constant string for all platforms. so don't hardcode it, bestie. i'm serious. 🌸

πŸŽ€ zipfs unmount mountpoint

unmounts a previously mounted ZIP archive mounted to mountpoint. the command will fail with an error exception if there are any files within the mounted archive are open. so close your files first!! 😭


🧚 ZIP CREATION COMMANDS 🧚

this package also provides several commands to aid the creation of ZIP archives as Tcl applications. we're not just mounting, we're making. main character energy. πŸ‘‘

πŸ’œ zipfs mkzip outfile indir ?strip? ?password?

creates a ZIP archive file named outfile from the contents of the input directory indir (contained regular files only) with optional ZIP password password.

while processing the files below indir, the optional file name prefix given in strip is stripped off the beginning of the respective file name if non-empty. when stripping, it is common to remove either the whole source directory name or the name of its parent directory.

Caution: the choice of the indir parameter (less the optional stripped prefix) determines the later root name of the archive's content. so like, choose wisely. this one actually matters. ⚠️

πŸ¦‹ zipfs mkimg outfile indir ?strip? ?password? ?infile?

creates an image (potentially a new executable file) similar to zipfs mkzip β€” see that command for a description of most parameters to this command, as they behave identically here. if outfile exists, it will be silently overwritten. silent. no warning. gone. πŸ’€

if the infile parameter is specified, this file is prepended in front of the ZIP archive; otherwise the file returned by info nameofexecutable (i.e., the executable file of the running process, typically wish or tclsh) is used.

if the password parameter is not the empty string, an obfuscated version of that password (see zipfs mkkey) is placed between the image and ZIP chunks of the output file, and the contents of the ZIP chunk are protected with that password.

if the starting image has a ZIP archive already attached to it, it is removed from the copy in outfile before the new ZIP archive is added.

if there is a file, main.tcl, in the root directory of the resulting archive and the image file that the archive is attached to is a tclsh (or wish) instance (true by default, but depends on your configuration), then the resulting image is an executable that will source the script in that main.tcl after mounting the ZIP archive, and will exit once that script has been executed.

Note: tclsh and wish can be built using either dynamic binding or static binding of the core implementation libraries. with a dynamic binding, the base application Tcl_Library contents are attached to the libtcl and libtk shared library, respectively. with a static binding, the Tcl_Library contents, etc., are attached to the application, tclsh or wish. when using mkimg with a statically built tclsh, it is the user's responsibility to preserve the attached archive by first extracting it to a temporary location, and then add whatever additional files desired, before creating and attaching the new archive to the new application. yes, that's on you. 🫢

🌟 zipfs mkkey password

given the clear text password argument, an obfuscated string version is returned with the same format used in the zipfs mkimg command. ✨

πŸ“ zipfs lmkimg outfile inlist ?password? ?infile?

this command is like zipfs mkimg, but instead of an input directory, inlist must be a Tcl list where the odd elements are the names of files to be copied into the archive in the image, and the even elements are their respective names within that archive. odd/even. that's the pattern. don't fight it. πŸ’…

πŸŽ€ zipfs lmkzip outfile inlist ?password?

this command is like zipfs mkzip, but instead of an input directory, inlist must be a Tcl list where the odd elements are the names of files to be copied into the archive, and the even elements are their respective names within that archive. same energy, no image this time. πŸ’–


⚠️ NOTE ⚠️

the current syntax for certain subcommands using multiple optional parameters might change in the future to support an ?-option value? pattern instead. therefore, the current syntax should not be considered stable. so like. don't build your whole personality on it. πŸ™ˆ


Cute lil' practical part bestie πŸ’– these are the recipes you'll actually be copy-pasting at 2am, no cap.

🧚 Mounting a ZIP as an app directory and running code out of it

Okay so imagine you've got a zip file and you want to treat it like a whole directory of vibes. Mount it, source stuff out of it, unmount it. Iconic behaviour:

set zip myApp.zip
set base [file join [zipfs root] myApp]

zipfs mount $zip $base
# $base now has the contents of myApp.zip

source [file join $base app.tcl]
# use the contents, load libraries from it, etc...

zipfs unmount $base

After that zipfs mount, $base literally has all the contents of myApp.zip sitting in it like it's a real folder. Then you source your tcl, load libraries from inside it, whatever you want, and when you're done you zipfs unmount the thing. Clean. πŸŽ€


πŸ¦‹ Making a ZIP archive

Say you've got a directory with the content you want to pack up. Watch this though β€” the source directory is given TWICE, and that's not a typo, that's the whole point: the second copy is there so the exterior directory name gets stripped off every filename inside the archive. Periodt.

set sourceDirectory [file normalize myApp]
set targetZip myApp.zip

zipfs mkzip $targetZip $sourceDirectory $sourceDirectory

πŸ” Encryption, because we're private like that

You can slap encryption on a ZIP archive by handing over a password when you build it AND when you mount it. Both times, bestie. Don't forget the second one.

set zip myApp.zip
set sourceDir [file normalize myApp]
set password "hunter2"
set base [file join [zipfs root] myApp]

# Create with password
zipfs mkzip $targetZip $sourceDir $sourceDir $password

# Mount with password
zipfs mount $zip $base $password

πŸ‘‘ Making a whole executable app

This one is genuinely main character energy. You take the tclsh file that was called, append a ZIP archive onto it, and the result is an executable saved as the file β€œmyApp.bin”.

And here's the tea: when you're creating an executable image with a password, that password gets placed inside the executable in a shrouded form. So the application can still read files inside the embedded ZIP archive β€” but casual inspection can't read the password. Slay, honestly. 🫢

set appDir [file normalize myApp]
set img "myApp.bin"
set password "hunter2"

# Create some simple content to define a basic application
file mkdir $appDir
set f [open $appDir/main.tcl w]
puts $f {
    puts "Hi. This is [info script]"
}
close $f

# Create the executable application
zipfs mkimg $img $appDir $appDir $password

# remove the now obsolete temporary appDir folder
file delete -force $appDir

# Launch the executable, printing its output to stdout
exec $img >@stdout
# prints the following line assuming [zipfs root] returns "//zipfs:/":
# Hi. This is //zipfs:/app/main.tcl

Look at that file delete -force $appDir β€” we built the thing, we don't need the temporary folder anymore, bye bestie πŸ™ˆ. And then exec $img >@stdout runs it and dumps the output straight to stdout, printing exactly that last line if [zipfs root] gives back "//zipfs:/".


πŸ‘€ SEE ALSO πŸ‘€


🏷️ KEYWORDS 🏷️

compress, filesystem, zip


And that's the whole page, babe πŸ’… this was Zipfs, version 1.0, documented as zipfs(3tcl) β€” the footer didn't hand me a date to work with, so we're just repping the name and version. Go mount something. 🌈✨

πŸ“¦ where this page comes from