Skip Menu |

This queue is for tickets about the Compress-Zlib CPAN distribution.

Report information
The Basics
Id: 63756
Status: resolved
Priority: 0/
Queue: Compress-Zlib

People
Owner: Nobody in particular
Requestors: matt [...] lackof.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: have Compress::Zlib use normal filehandles and functions
Date: Wed, 08 Dec 2010 14:44:23 -0800
To: bug-Compress-Zlib [...] rt.cpan.org
From: Matt Taggart <matt [...] lackof.org>
I have a feature request for the Compress::Zlib module. Hopefully it's a good one but if my lack of perl guruness means I am just not using it right, feel free to let me know :) I wanted to add support to an existing perl script to have it be able to open gzipped files as well as normal files. The script already used a FILEHANDLE and did things like seek(FILEHANDLE, $startpos, 0) and while (<FILEHANDLE>). I wanted to be able to just check if the file ended in .gz and then if it did open it in a special way but not have to modify the rest of the script. At first I tried this open(FILEHANDLE, "-|", "zcat", $filename); seek(FILEHANDLE, $startpos, 0); but the latter failed, I guess seek doesn't work well on pipes? So then I started to look at using Compress::Zlib, but that meant having to use gzopen, gzseek, gzreadline, etc. Could Compress:Zlib somehow use normal filehandles? You'd still have to do a special open, but after that it would be nice to use normal methods. Maybe the close would need to be special too (or maybe that could be smart?). (I don't think it matters but I am using perl 5.10.0 and debian's libio-compress-zlib-perl version 2.012-1) Thanks, -- Matt Taggart matt@lackof.org
Hi Matt, Compress::Zlib uses a module called IO::Uncompress::Gunzip behind the scenes to do all the hard work (it also uses IO::Compress::Gzip for compression, but that's not relevant here). IO::Uncompress::Gunzip is the module you are looking for - the object that IO::Uncompress::Gunzip creates is a special filehandle that is mostly compatible with all the standard file IO meothds, so you can use read, getc etc. The big exception is the seek command - when reading a compressed file you can only seek forwards in the file. If your application only ever deals with compressed files that end with .gz, you can use a standard open for non-compressed files and IO::Uncompress::Gunzip for the compressed ones. Alternatively, if the "Transparent" option is spefified when openeing a file with IO::Uncompress::Gunzip, it will figure out if the file compresssed or not and return a filehandle that just works with both compressed & uncompressed files. Drop me a line if you need any more assistance. Paul