Robert Citek wrote:

> 
> Hello all,
> 
> Questions:
> How can I tell if a file is compressed (gzip or compress)?
> Can I use seek/tell on a pipe or fifo?
> If not, are there work-arounds?
> 

here is a OS-independent way to check if a file is gzip or not:

#!/usr/bin/perl -w
use strict;

if(is_zip('file.whatever')){
        print "is gzip\n";
}else{
        print "is not gzip\n";
}

sub is_zip{

        my $zip_file = shift;
        my $id1 = undef;
        my $id2 = undef;

        open(GZIP,$zip_file) || die("Unable to open $zip_file: $!");

        sysread(GZIP,$id1,1);
        sysread(GZIP,$id2,1);

        close(GZIP);

        return unpack("C",$id1) == 31 && unpack("C",$id2) == 139;
}

__END__

the idea is simple, you check for the GZIP magic header. the GZIP header 
happens to be the first 2 bytes of the gzip file and always have the value 
of 31 and 139. every gzip file starts like that. check the GZIP 
specification for more. once you know the GZIP specification, you can do a 
lot more with the GZIP file.

no, you can't really seek/tell pipe or fifo. pipe and fifo are IPC where 
seek/tell makes no sense. you can't seek/tell something you don't know. you 
never know what's happening at the other end.

david

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to