Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read a gzipped file in TCL?

Tags:

gzip

tcl

I have a file with .gz extension. When I try to read and print the file with following TCL commands I can't read the file even though I am able to see the contents in the VI editor.

I tried with the following TCL code:

set of [glob *.gz ]
set op [open "$of" r]
set file_data [read $op]
set data [split $file_data "\n"]
foreach line $data {
    puts " $line"
}
like image 243
bharath kumar Avatar asked Apr 30 '14 09:04

bharath kumar


1 Answers

In Tcl 8.6, you have built-in support for this so you can do:

set f [open $filename]
zlib push gunzip $f
set data [read $f]
close $f

The zlib push gunzip just attaches a suitable uncompressing transform to the channel.

In 8.5 and before, you're best to read from a pipeline with zcat or gzcat (depending on platform details:

set f [open "|gzcat $filename"]
set data [read $f]
close $f

The down-side is that that's nowhere near as portable.

like image 146
Donal Fellows Avatar answered Oct 30 '22 17:10

Donal Fellows