joe
joe

Reputation: 35087

How to find gzip file is empty compressed file

How to find gzip file is empty using perl

a.txt.gz when i uncompress its empty

how to find the compress gz are empty ?

Upvotes: 2

Views: 2594

Answers (3)

David Raab
David Raab

Reputation: 4488

You can't directly get the size of the uncompressed file, but you can use seek for it. Create an object from the file and try to seek to the first byte. If you can seek, then your file is at least 1 byte in size, otherwise it is empty.

#!/usr/bin/env perl
use strict;
use warnings;
use IO::Uncompress::Gunzip;
use Fcntl qw(:seek);

my $u = IO::Uncompress::Gunzip->new('readme.gz');
if ( $u->seek(1, SEEK_CUR) ) {
    print "Can seek, file greather than zero\n";
}
else {
    print "Cannot seek, file is zero\n";
}

Upvotes: 4

David W.
David W.

Reputation: 107040

You could use IO::Compress::Gzip which comes with Perl 5.10 and above (or download it via CPAN). And use that to read the file.

However, you could just do a stat on the file and simply see if it contains only 26 bytes since an empty file will consist of just a 26 byte header.

I guess it simply depends what you're attempting to do. Are you merely trying to determine whether a gzipped file is empty or did you plan on reading it and decompress it first? If it's the former, stat would be the easiest. If it's the latter, use the IO::Compress::Gzip module and not a bunch of system commands to call gzip. The IO::Compress::Gzip comes with all Perl distributions 5.10 and greater, so it's the default way of handling Zip.

Upvotes: 2

Fredrik Pihl
Fredrik Pihl

Reputation: 45662

To check the content of a compressed tar-file, use

tar -ztvf file.tgz

To list content of a .gz-file, use

gzip --list file.gz

Wrap that in perl and check the uncompressed field in the output

$ gzip --list fjant.gz 
         compressed        uncompressed  ratio uncompressed_name
                 26                   0   0.0% fjant

Upvotes: 0

Related Questions