| View previous topic :: View next topic |
| Author |
Message |
Ofloo Owner
Joined: 13 May 2003 Posts: 953 Location: Belguim
|
Posted: Mon Aug 15, 2011 7:54 am Post subject: zlib tclsh8.6 |
|
|
how do i decompress gzip file from url | Code: |
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 |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Mon Aug 15, 2011 9:25 am Post subject: |
|
|
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: | 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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
Ofloo Owner
Joined: 13 May 2003 Posts: 953 Location: Belguim
|
Posted: Mon Aug 15, 2011 10:20 am Post subject: |
|
|
| Quote: | $ ./zlib_test.tcl
An error occured while fetching http://: corrupted input data
$ gunzip /tmp/file.gz
$ ls /tmp/file
/tmp/file
$ |
| Code: | #!/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: | #!/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 |
|
| Back to top |
|
 |
|