#!/bin/sh #! -*- perl -*- eval 'exec $PERLLOCATION/bin/perl -x $0 ${1+"$@"} ;' if 0; $fileName = $ARGV[0]; $magicNumber_GZIP = pack("C*", 0x1f, 0x8b, 0x08, 0x08); # Just peek at the first 4 characters in the file and # close it again. open(INPUTFILE, $filename) or die "Couldn't open file $filename: $!"; binmode(INPUTFILE); read(INPUTFILE, $magicNumber, 4); close(INPUTFILE); if ($magicNumber eq $magicNumber_GZIP) { # file starts w/gzip magic number my $pid; if (not defined($pid = open(INPUTFILE, "-|"))) { die "can't fork: $!"; } if ($pid) { # parent process - do nothing } else { # child process exec( "gzip", "-f", "-c", "-d", $filename); } } else { open(INPUTFILE, $filename) or die "couldn't open $filename: $!"; } while () { my $line = $_; # Here, we could just process the file line by line, but # for this example, we'll just push it onto an array. push @contents, $line; } close(INPUTFILE);