This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

zlib tclsh8.6

Help for those learning Tcl or writing their own scripts.
Post Reply
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

zlib tclsh8.6

Post by Ofloo »

how do i decompress gzip file from url

Code: Select all


package require http
package require zlib

set url "http://"

set tok [http::geturl $url -binary 1]
set dat [http::data $tok]
http::cleanup $tok

catch {[set dcd [zlib decompress $dat]]}
this errors
XplaiN but think of me as stupid
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Well, posting the actual error message is usually helpful..

That said, you've enclosed your "set" command within brackets, and then pass the output to "catch" for evaluation. Unless you were intending to execute the whole text on the webpage as a single tcl-command, this is probably your problem. Even if this is what you intended, be adviced that the text received is used explicitly as the command; you will not be able to pass arguments this way.

I'd also recommend using the error/result-variable option with catch, so that you can actually see the error you're trying to catch...

All in all, I'd probably re-write that piece something like this:

Code: Select all

package require http
package require zlib

set url "http://"
set tok [http::geturl $url -binary 1]
set dat [http::data $tok]
http::cleanup $tok

if {[catch {zlib decompress $dat} dcd]} {
  #Something went wrong, the error message is available in dcd
  putlog "An error occured while fetching $url: $dcd"
} else {
  #Everything went smoothly.. do whatever you need with the data in dcd

  #Should you desire to execute the content of $dcd as a valid tcl command line, uncommend the following line:
  #catch {eval $dcd} result


}
Be adviced, I've intentionally left out the set command within the catch argument, as it is not needed.
NML_375
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

$ ./zlib_test.tcl
An error occured while fetching http://: corrupted input data
$ gunzip /tmp/file.gz
$ ls /tmp/file
/tmp/file
$

Code: Select all

#!/usr/local/bin/tclsh8.6
package require http
package require zlib

set url {http://}
set tok [http::geturl $url -binary 1]
set dat [http::data $tok]
http::cleanup $tok

if {![catch {open /tmp/file.gz w} w]} {
  fconfigure $w -translation binary -encoding binary
  puts -nonewline $w $dat
  close $w
}

if {[catch {zlib decompress $dat} dcd]} {
  puts "An error occured while fetching $url: $dcd"
}
EDIT

an other thing i noticed is that when you use tcl 8.6 that you're not supposed to add package require zlib

Code: Select all

#!/usr/local/bin/tclsh8.6
package require http

set url {http://narf.ofloo.net/file.gz}
set tok [http::geturl $url -binary 1]
set dat [http::data $tok]
http::cleanup $tok

if {![catch {open /tmp/file.gz w} w]} {
  fconfigure $w -translation binary -encoding binary
  puts -nonewline $w $dat
  close $w
}
otherwise it will not have the option zlib gunzip however the result is exactly the same though. And an other surprise it did a decompression. However this is a test file, .. and the link i'm getting doesn't contain the file name which results back into the same error.
XplaiN but think of me as stupid
Post Reply