I'm trying to uncompress what's inside of a var into the same var or onto another. I created a .gz file using gzip -k filename. It generated a filename.gz while keeping the original. I uploaded this gz file to a server. The following line downloads the gz file to a var. Yes, I could use package require http, etc...
set testvar [exec wget -q -O - url-to-gz-file]; string length $testvar
The string length $testvar returns a number. Now, I need to deflate/decompress what's inside of $testvar to the same var or to another var like testvar2 and this var has to contain the contents of the original file. I mean, the contents of the file before being compressed. The original file is just a text file.
I probably need to instruct that testvar is binary.
Is this possible? I do not want to download the .gz file first to the hard drive and process it after. It all needs to be done "in memory".
Thanks.
The simplest way is to just pipe
wgetintogunzip:This requires that the uncompressed contents be encoded in your system's default character encoding; if it's binary data or some other text encoding, you'll have problems (Your
teststrvariable probably isn't holding valid gzip data either because of the character encoding translation).In those cases, tcl comes with the
zlibcommand for working with zlib and gzip compression. Thezlib gunzip STRINGsubcommand, in particular, would be useful here. You have to useopenfor that, notexec, in order to get binary data that can be passed to thezlib gunzip. It would look something like:You can use
zlib pushto add a layer to thewgetchannel so that usingreadon it automatically decompresses the data too.