Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Working with Tcl zlib command

74 views
Skip to first unread message

WJG

unread,
Jul 22, 2018, 5:26:51 AM7/22/18
to
Hi Everyone,

I'm trying to get a grips on the the zlib command which is proving baffling. Does anyone have some simple procs which could be shared to zip and unzip files and directories?

Hitherto I've used 'exec unzip' to access data but the memory loss is too excessive.

Thanks,

Will

Rich

unread,
Jul 22, 2018, 10:48:26 AM7/22/18
to
WJG <wjgid...@googlemail.com> wrote:
> Hi Everyone,
>
> I'm trying to get a grips on the the zlib command which is proving
> baffling. Does anyone have some simple procs which could be shared
> to zip and unzip files and directories?

Well, you appear to be conflating two different things in your
paragraph above.

zlib is a compression algorithm. Simplified, it takes a block of data,
compresses it, and returns a (hopefully) smaller block as a result. Or
for decompress it takes a block of data, decompresses it, and returns
(generally) a larger block as a result.

Zip and unzip are archivers that create/extract archive files, that
happen to utilize the zlib algorithm for compressing stored archive
members.

So Tcl's zlib command is not a "zip" or "unzip" tool by itself, which
is what your paragraph above appears to imply.

> Hitherto I've used 'exec unzip' to access data but the memory loss is
> too excessive.

You might start looking here: http://wiki.tcl.tk/15158

WJG

unread,
Jul 22, 2018, 2:27:14 PM7/22/18
to
Hi Rich,

Yep. Aware of everthing you mention. Might use a different strategy.

Will

Jim

unread,
Jul 24, 2018, 12:36:57 PM7/24/18
to
Yes, here is a tcl proc called "gzip". It takes a filename as input and
compresses it. The output is a .gz file.

usage:
gzip <filename>
output:
<filename>.gz

proc gzip {args} {

foreach fileName $args {
set file_extension [file extension $fileName]
if { [string equal $file_extension ".gz"] } {
break
} else {
if { [catch {open ${fileName} rb} fin] } {
puts "(E): gzip, Error opening \[$fileName\] (w/read
access): catchme=\[$fin\]."
return
}
if { [catch {open ${fileName}.gz wb} fout] } {
puts "(E): gzip, Error opening \[${fileName}.gz\] (w/write
access): catchme=\[$fout\]."
return
}
set zs [zlib stream gzip]
while {![eof $fin]} {
puts -nonewline $fout [$zs add [read $fin 4096]]
}
$zs finalize
puts -nonewline $fout [$zs get]
close $fin
close $fout
file delete $fileName
}
}
}

Jim

unread,
Jul 24, 2018, 12:53:11 PM7/24/18
to
On 7/22/2018 2:26 AM, WJG wrote:
Here is an example of reading a compressed file (.gz) line-by-line --
just like you would read an uncompressed file. We simply attach
'gunzip' to the open channel (inFile) if the extention is .gz and place
the channel in 'binary mode' using 'fconfigure'.


1 #######################
2 # OPEN, input file (READ-ONLY)
3 #######################
4 set inFile [open $my_file r]
5 # attach uncompress (gunzip) method if input file is in *.gz format:
6 if { [string equal [file extension $my_file] ".gz"] } {
7 fconfigure $inFile -translation binary
8 zlib push gunzip $inFile
9 }
10
11 while { [gets $inFile line] >= 0 } {
12 <whatever you want here>
13 }
14
15 close $inFile
16

Line 4: Open a file for read access.
Line 6-9: Check file extension and add "zlib push gunzip" to the open
channel if extension is ".gz"
Line 11-13 Read the file line-by-line just like you would if it was not
compressed.
Line 12: Insert your code here. Example: puts $line

Jim

unread,
Jul 24, 2018, 1:10:55 PM7/24/18
to
On 7/22/2018 2:26 AM, WJG wrote:
Please use tcl 8.7a1 or greater.
Prior versions of tcl have bugs (one example shown below using 8.6.6
but other versions have bugs too).

To test your tcl version, grab the latest testcase from most recent
release (8.7a1 today?) and source the testcase.

zlib testcase:
./test/zlib.test

My results:
source <install>/test/zlib.test
zlib.test: Total 65 Passed 65 Skipped 0 Failed 0

% puts $tcl_version
8.7
% puts $tcl_patchLevel
8.7a1

As compared to (tcl 8.6.6.) where 4 tests fail:
zlib.test: Total 65 Passed 61 Skipped 0 Failed 4
% puts $tcl_patchLevel
8.6.6

More Details
Refer to:
Subject: zlib push creates corrupt file gz file when source size is
equal or greater than 8092
Link:
https://groups.google.com/forum/?utm_source=digest&utm_medium=email#!msg/comp.lang.tcl/cwRSgyJ0dOU/OZxS54BDBQAJ;context-place=forum/comp.lang.tcl
0 new messages