For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > Re: checking gzip files









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: checking gzip files
Adriano Ferreira

2006-01-10, 4:02 am

On 12/30/05, S Khadar <ibiokid@gmail.com> wrote:
> #!/usr/bin/perl
> use Shell;

....
> $dmchk=3Dzless( "$dir/$_/foo.gz");


As an aside note, C<perldoc Shell> advises against this style [ use
Shell <nothing> ; ]. Prefer this:

use Shell qw(zless);

so that you know that you are not calling some program by mistake/typo.

From the man page of gzip, the command

zcat $gzfile | wc -c

is recommended to get the uncompressed file size.

You can use it in backticks, like `zcat $gzfile | wc -l` and then
check the returned number.
But this is rather expensive for large files, since you just want to
know if it has zero bytes or not. Ah, you can use 'zcat' where you are
using 'zless' with the same effect but without a pipe to the unix
command 'less'.

A pure Perl solution would be to use Compress::Zlib (which you
probably has already - for example if you use CPAN) and use a function
like

use Compress::Zlib;

sub zz {
my $f =3D shift;
my $gz =3D gzopen($f, 'r') or die; # error handling left as an exercize
return ! $gz->gzread(my $buf, 1); # just one or zero bytes read and dum=
ped
# only return
matters - true for empty, false otherwise
}

my $f =3D 'a.gz';
print zz($f) ? 'zero bytes' : 'non-empty';

In this case, no need anymore for "use Shell", unless you use some
other external utility.

Regards,
Adriano.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com