Version 15 of Using zipper to create zip files

Updated 2010-05-24 20:21:42 by erich

Kevin Walzer I've been looking for a pure-Tcl solution to create zip archives, and stumbled across JCW's "zipper" package, documented at http://www.equi4.com/critlib/zipper.README . It's part of his critlib package at http://www.equi4.com/critlib/ . I'm a bit surprised that this extension isn't more widely discussed or used, because it appears to offer a foundation for writing a tcllib extension similar to tar.

Here's a sample:

    package require zipper
    zipper::initialize [open try.zip w]
    zipper::addentry dir/file.txt "some data to store"
    close [zipper::finalize]

This will create a zip archive called "try.zip" with "file.txt," whose contents are "some data to store." Mounting the archive with a tool like WinZip or, more appropriately, the tclvfs package (vfs::zip) confirms the contents of the file.

I was curious if one could copy an existing file to a zip file with zipper, and it seems you can. I tried a jpeg, and it worked fine. Use code similar to this:

   package require zipper
   set f  [open somefile.jpg r]
   fconfigure $f -translation binary
   zipper::initialize [open try.zip w]
   zipper::addentry somefile.jpg [read $f]
   close $f
   close [zipper::finalize]

This works by reading the jpeg file as a binary stream, then writing the data to a file of the same name in the zip archive. By contrast, with the tclvfs extension, you can copy a file from the virtual file system (in this case a zip archive) to your local directory using the file copy command.

One thing zipper does not do is append files to an existing zip archive--I have not found a way to do that. ((Using the command "open try.zip a" simply overwrote the contents of the existing zip archive.) If you want to append files you will probably need to do a workaround by writing the contents of an existing zip archive to a temporary directory, adding whatever files you want to that directory via file copy, then writing each file back to a new zip archive with the same name via zipper. This might be slow if you have a lot of files or they are large.

I'm thinking about writing a little GUI utility that will simplify the creation and viewing of zip archives, and the zipper command set will likely be an integral part of it.


MHo, 2007-Oct-09: I noticed a time difference of -2 hours after adding some files with addentry... [file mtime sourcefile] (WinXP, daylight saving time active, germany)... Can someone confirm this behaviour? I can't remember details, but a 1 hour-delta-problem with NTFS sounds somewhat familiar... Meanwhile I found a possible workaround for the diffs in zipper.tcl:

 proc dostime {sec} {
     set f [clock format $sec -format {%Y %m %d %H %M %S} -gmt 1]; ########################### -gmt 1
     regsub -all { 0(\d)} $f { \1} f
     foreach {Y M D h m s} $f break
     set date [expr {(($Y-1980)<<9) | ($M<<5) | $D}]
     set time [expr {($h<<11) | ($m<<5) | ($s>>1)}]
     return [list $date $time]
 }

After removing -gmt 1, the local timezone is respected and most of the time stamps are accurat. But still some files remain which are stored with a 1-hour-difference which is due to daylight saving time manipulations, I guess... hm ... How is the time stamp in the zip archiv interpreted?


RS 2007-10-09: Of course, it would be best if tclvfs supported adding to ZIP files... MHo: Yes, indeed. But as I noticed that tclkit*.exe have the zlib compiled in, zipper.tcl was the easiest way to create zip archives without execing to an external zip.exe. I don't know the details of zlib, though.

erich 2010-05-24: without the zlib package, for those using pre-8.6 tcl, it might be useful to change the addentry proc and use tcllib's crc32.tcl for the crc check so it doesn't spit out a zip archive that gives you an error of a bad checksum when trying to open the archive


Category Compression