Updated 2012-10-29 04:24:16 by RLE

A tarball is a file that contains an archive of a directory or file(s) created by the command tar (which stands for tape archive). It's commonly used in Unix-like systems and they are often compressed with gzip or bzip2.

More on the origins of the term at [1]

Tcl supports tar filesystems with the vfs::tar module of the tclvfs.

Question: What would a Tcl script look like to access a tar file using tclvfs and accounting for the possible compression of the file?

18Aug2004 PS If you want to abuse the tar code to extract a tar file using pure Tcl, you can use the tar code from tarvfs.tcl and get working tar decoder with relative ease. You need to include all the code starting from aproximately halfway through the tarvfs.tcl file (version 0.9) in your script. The decoder starts with a comment block aptly named 'tar decoder:'.

Untarring is very simple:
 proc untar { tarfile path } {
    # Untar data from tarfile to path
    
    set fd [tar::open $tarfile]
    puts "Untar"

    foreach {file sbx} [array get ::tar::$fd.toc] {
        array set sb $sbx
        switch $sb(type) {
            "directory" {                
                file mkdir [file join $path $file]
            }
            "file" {
                puts [file join $path $file]
                set dir [file dirname [file join $path $file]]
                if { ![file exists $dir] } {
                    file mkdir $dir
                }
                set out [open [file join $path $file] w]
                fconfigure $out -translation binary
                seek $fd $sb(start)
                tar::Data $fd sb data
                puts -nonewline $out $data
                close $out
            }
        }
        unset sb
    }
    tar::_close $fd    
 }

Related: tgz tbz bzip2