From: Paul Duncan Date: 2003-04-15T02:28:27+09:00 Subject: Re: Code for random access to compressed IO? --6c2NcOVqGQ03X4Wi Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable * coma_killen@fastmail.fm (coma_killen@fastmail.fm) wrote: > Hi, >=20 > Anyone seen some code for compressed IO streams that allow=20 > random access (arbitrary seek)? Along the lines of=20 > razip.sf.net? Zlib itself doesn't allow for arbitrary seek. SEEK_END (aka seeking relative to the end of the stream) isn't supported, and seeking forward is basically implemented by reading forward in a tight for() loop. Here's what zlib.h has to say about gzseek(): ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file, z_off_t offset, int whence)); /*=20 Sets the starting position for the next gzread or gzwrite on the given compressed file. The offset represents a number of bytes in the uncompressed data stream. The whence parameter is defined as in lseek(2); the value SEEK_END is not supported. If the file is opened for reading, this function is emulated but can be extremely slow. If the file is opened for writing, only forward seeks are supported; gzseek then compresses a sequence of zeroes up to the new starting position. gzseek returns the resulting offset location as measured in bytes from the beginning of the uncompressed stream, or -1 in case of error, in particular if the file is opened for writing and the new starting position would be before the current position. */ In order to implement truly random access, you'd need to read till the end of the file, get the length, then rewind the file. Then you could emulate SEEK_END by rewinding the file, and gzseek()ing forward file_len - seek_amt. This works, but it's slooooooow. =20 > It seems to me that ruby-zlib does not provide this. It > has GzipReader and Writer but they don't give random access? >=20 > Any info appreciated, >=20 > Karsten --=20 Paul Duncan pabs in #gah (OPN IRC) http://www.pablotron.org/ OpenPGP Key ID: 0x82C29562 --6c2NcOVqGQ03X4Wi Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (GNU/Linux) iD8DBQE+mu+xzdlT34LClWIRApoaAJ0W9/761l2wSz5P/l50uR/RxlR1xQCg1qbZ uuiku+0ALP/5k7n18WtnBCs= =k2Fl -----END PGP SIGNATURE----- --6c2NcOVqGQ03X4Wi--