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

Reading an archive's "ZIP comment"

30 views
Skip to first unread message

Zbiggy

unread,
Oct 16, 2011, 6:04:24 PM10/16/11
to

I'm wondering: how can this be done in the script? I would use vfs::zip -
but if not possible, can be other way too.

Well, there is a procedure at http://wiki.tcl.tk/11846 - but an attempt to
use this raises an error: "not enough arguments for all format specifiers".

When I tried to use the proposed method sequentially line-by-line then - in
the end it reported:

#v+
% puts $cb(comment)
0
#v-

...which is wrong, of course.

Not being too familiar with ZIP-format details, I'm not sure, what's the
problem.
--
Z.

Donal K. Fellows

unread,
Oct 17, 2011, 6:55:29 AM10/17/11
to
On 16/10/2011 23:04, Zbiggy wrote:
> I'm wondering: how can this be done in the script? I would use vfs::zip -
> but if not possible, can be other way too.
>
> Well, there is a procedure at http://wiki.tcl.tk/11846 - but an attempt to
> use this raises an error: "not enough arguments for all format specifiers".

Try the code at http://wiki.tcl.tk/3307#pagetoc2af086b9 which uses Tcl
8.6 (TclOO and the [zlib] command only though, so could be used with 8.5
with a bit of effort and external packages I suppose). It works with the
sample files I've tried, but they didn't have comments so I've only gone
with what's in the spec...

Donal.

Zbiggy

unread,
Oct 17, 2011, 1:29:06 PM10/17/11
to
In comp.lang.tcl, Donal K. Fellows wrote:

> Try the code at http://wiki.tcl.tk/3307#pagetoc2af086b9 which uses Tcl
> 8.6 (TclOO and the [zlib] command only though, so could be used with 8.5
> with a bit of effort and external packages I suppose). It works with the
> sample files I've tried, but they didn't have comments so I've only gone
> with what's in the spec...

Well, it properly recognized the bytesize - but still cannot read
zipfile-comment:

#v+
% puts Comment:[$z comment]
Comment:
foreach n [$z names] {
puts "$n ([$z info $n disksize] bytes)"
}
asdd (7405 bytes)
%
#v-

[$z comment] seems to be empty (but the ZIP file is commented).
--
Z.

Donal K. Fellows

unread,
Oct 18, 2011, 9:47:39 AM10/18/11
to
On 17/10/2011 18:29, Zbiggy wrote:
> Well, it properly recognized the bytesize - but still cannot read
> zipfile-comment:
[...]
> [$z comment] seems to be empty (but the ZIP file is commented).

I've never had a ZIP file with a comment, so I couldn't test. :-)

Donal.

Zbiggy

unread,
Oct 18, 2011, 10:02:34 AM10/18/11
to
In comp.lang.tcl, Donal K. Fellows wrote:

> I've never had a ZIP file with a comment, so I couldn't test. :-)

Maybe you can create one? Usually: "zip -c archive.zip file file file..."
--
Z.

Donal K. Fellows

unread,
Oct 18, 2011, 3:14:23 PM10/18/11
to
OK, I can read them with my class. :-) I can also read overall comments
(i.e., those created if you use the -z option). Hence I claim that my
code's doing the right thing. Whatever the problem is, it's not my code.

I do note that that class doesn't apply any encoding handling. It
probably ought to, but... well, you've got the source. :-D

Donal.

Zbiggy

unread,
Oct 19, 2011, 5:47:57 PM10/19/11
to
In comp.lang.tcl, Donal K. Fellows wrote:

> OK, I can read them with my class. :-) I can also read overall comments
> (i.e., those created if you use the -z option). Hence I claim that my
> code's doing the right thing. Whatever the problem is, it's not my code.

Well, tried it again; like before, it returns no zipfile-comment. Although
it properly recognizes size and packed file name. Tried it using "Example of
use" from the mentioned page.

Slackware64 13.1, TCL/Tk 8.6b2

If your code really is correct - maybe there's a need for some more details,
how to use it? The example obviously won't return any zipfile-comment.
--
Z.

hae

unread,
Oct 20, 2011, 2:11:35 AM10/20/11
to
Hi Zbiggy,

I used the method ReadDirectory in from the wiki page mentioned above
and created a zip file with winrar.exe that contains a comment. It
works for me.

Here is the code I used:

--------8<-------------8<----------------

set fd [open Readme.zip]

proc ReadDirectory {} {
variable fd

set off -22
while 1 {
seek $fd $off end
binary scan [read $fd 4] i sig
if {$sig == 0x06054b50} {
seek $fd $off end
break
}
incr off -1
}
binary scan [read $fd 22] issssiis sig disk cddisk nrecd nrec \
dirsize diroff clen
if {$clen > 0} {
set comment [read $fd $clen]
} else {
set comment ""
}
if {$disk != 0} {
error "multi-file zip not supported"
}
seek $fd $diroff
for {set i 0} {$i < $nrec} {incr i} {
binary scan [read $fd 46] issssssiiisssssii \
sig ver mver flag method time date crc csz usz n m k d ia
ea \
off
if {$sig != 0x02014b50} {
error "bad directory entry"
}
set name [read $fd $n]
set extra [read $fd $m]
if {$k == 0} {
set c ""
} else {
set c [read $fd $k]
}
set directory($name) [dict create timestamp [list $date $time]
\
size $csz disksize $usz offset $off method $method \
extra $extra comment $c]
}

puts comment='$comment'
parray directory
}

--------8<-------------8<----------------

Here is my test output:

(tclzip) 54 % source tclzip.tcl
(tclzip) 55 % ReadDirectory
comment='This is a zip comment!'
directory(Readme.txt) = timestamp {16212 16389} size 10 disksize 10
offset 0 method 0 extra {} comment {}
(tclzip) 56 %

Donal K. Fellows

unread,
Oct 20, 2011, 4:08:15 AM10/20/11
to
On 19/10/2011 22:47, Zbiggy wrote:
> Well, tried it again; like before, it returns no zipfile-comment. Although
> it properly recognizes size and packed file name. Tried it using "Example of
> use" from the mentioned page.

There can be comments on both the overall archive and on the individual
files. (Under the assumption that $z holds the name of an instance of a
Zip object,) [$z comment] returns the whole archive comment, and [$z
comment $file] returns the comment associated with a particular file; I
tried to make the usage of the API as obvious as possible.

I tested it with ZIP files created with both my system 'zip' command
(which is how I tested the comments) and with Java's 'jar' tool. These
are independent implementations, so I assume that if I can read both of
their output, my code is correct. :-) Note however that Java's 'jar'
tool does not produce comments at all, so I couldn't check that
particular combination.

Perhaps your sample zip file just doesn't have any comments? Or maybe
they're packed somehow into the 'extra' info field, which I don't parse
at all. (The ZIP spec is really quite complicated when you get into the
details, and not everything is actually documented apparently.)

Donal.

Zbiggy

unread,
Oct 20, 2011, 6:51:10 AM10/20/11
to
In comp.lang.tcl, hae wrote:

> I used the method ReadDirectory in from the wiki page mentioned above
> and created a zip file with winrar.exe that contains a comment. It
> works for me.

I made another attempt using this code - here's the result:

#v+
% ReadDirectory
comment=''
directory(asdd) = timestamp {15996 31078} size 3891 disksize 7405 offset 0
method 8 extra {UT˙ Mux
čd} comment {To jest test}
#v-

Then we can see still an empty comment - but, from the other side, it showed
comment in a dictionary "comment" contents, and some strange things in
"extra" field contents.

No idea, maybe my ZIP utility is flawed? It's Zip 3.0 (July 5th 2008). But,
from the other side: since Midnight Commander - and other tools - are able
to recognize the contents of zip-packages prepared using this utility - and
the comment, of course - I don't think, it's really flawed.

Still I can't understand the origin of the problem.
--
Z.

Donal K. Fellows

unread,
Oct 20, 2011, 8:31:27 AM10/20/11
to
On 20/10/2011 11:51, Zbiggy wrote:
> I made another attempt using this code - here's the result:
>
> #v+
> % ReadDirectory
> comment=''
> directory(asdd) = timestamp {15996 31078} size 3891 disksize 7405 offset 0
> method 8 extra {...} comment {To jest test}
> #v-
>
> Then we can see still an empty comment - but, from the other side, it showed
> comment in a dictionary "comment" contents, and some strange things in
> "extra" field contents.

Ah! You've misunderstood the ZIP format. There are *two* *independent*
places for comments: the overall archive, and attached to each file in
the archive. In the case you're dealing with, the overall comment is not
there but there is a comment on the 'asdd' entry; with my code, doing
[$z comment asdd] will return it. Alternatively, pick the value out of
the dictionary with [dict get]; that's all that's going on.

It all seems a bit odd if you're only putting one file inside the ZIP, I
know, but it makes a lot more sense with multi-file archives.

Donal.

Zbiggy

unread,
Oct 21, 2011, 4:40:14 AM10/21/11
to
In comp.lang.tcl, Donal K. Fellows wrote:

> There can be comments on both the overall archive and on the individual
> files.

Ah, now I see, that I totally confused "-c" and "-z" options of the ZIP
utility; actually, I was always commenting entire archive only, as it was
quite enough for me.

Thanks, not tried it again yet (in a few minutes) - but I'm pretty sure,
that my troubles are over. :D
--
Z.
0 new messages