Gzip reader

Hi ,
I have gzip file.
Is it possible to script a code which can read from compressed byte offset to uncompressed byte length?

Regards,
Chetan.C

You mean, translate an offset in the gzip file to an offset in the unzipped file?

It may not always make sense to do so. These things aren't necessarily related so directly. Longer sequences than bytes may be necessary to encode things.

One thing you could do, I suppose, is something like

dd if=file.gz bs=numofbytes count=1 | gunzip 2>/dev/null | wc -c

That would break the zip file early, and show how many bytes it managed to decode before it broke. The 2> /dev/null is necessary because gzip would certainly complain.

Hi,
Thanks for ther response.
Would like to add more information to the question.

The gzip file that im creating is by zipping individual stream of data through datastage.
I am calculating the compressed byte length from another copy of the file which is not zipped by the below code.

while IFS="^" read FIELD1 FIELD2
do
        echo "$FIELD2"
        echo "$FIELD1" | gzip | wc -c
done<"$file"

Field2 is the filename of the record.

I'm expecting that compressed size of 1st record should be the byte offset of second record in the zipped file.

Am i right in thinking so?

Thanks.

Sorry, no.

gzip doesn't work that way. You can't really seek inside a gz file, for starters, because you can't use a partial file -- any of the bytes are meaningless without the others. And one byte can represent many bytes -- kind of the whole point of using it. So there's no longer any direct, easy, accurate correlation, and not even any benefit in finding out the rough location.

Hi,

Firstly apologies for that double post!.
Thanks for the responses.Will ensure it does not happen.

Thanks,
Chetan.C