S
scottmf
I am trying to write a modified file open function that can take an
ascii text file, a .gz file or a .zip file and create a filehandle for
reading or writing using the appropriate module. I have a routine
working for ascii text or .gz files but I cannot figure out how to get
it working for zip files. The goal is to replace the standard open
command in several of my scripts with my modified open, but to not have
to change any other code and be able to read from or write to any file
type simply based on the filename I use.
Here is what I have:
my $test_fh_in;
my $test_fh_out;
mod_open($test_fh_in, "test_in.zip");
mod_open($test_fh_out, ">test_out.zip");
while(<$test_fh_in>){
print "$_";
print $test_fh_out "$_";
}
sub mod_open{
## Libraries needed to to zipped file reading/writing
use File::Basename;
use IO::File ;
use IO::Zlib ;
use Archive::Zip;
my $FH;
my $file_name = $_[1];
my $read; ## boolean indicating whether to open the file for
read or write 1 = read, 0 = write
## Need to look into appending as well.....
if($file_name =~ /^\>/){ ## Check to see if the file is to be
opened for reading or writing
$read = 0;
$file_name =~ s/^\>//; ## drop the read/write identifier from the
string so it is a valid filename
}
else{
$read = 1;
}
## define a list of zip suffixes
my @suffixlist = (
".gz",
".gzip",
".zip"
);
# check for filename extension - if .gz use the the Zlib, else do
not...
my ($file_base, $file_path, $file_type) =
fileparse($file_name,@suffixlist);
if($read){ ## if the file is supposed to be opened for reading
then do so
if ( $file_type eq "" ) {
print "The input file $file_name is standard ASCII -
uncompressed\n" ;
$FH = IO::File->new($file_name, "r") ;
}
elsif($file_type =~ /gz(ip)?/) {
print "The input file $file_name is compressed using gzip\n" ;
$FH = IO::Zlib->new($file_name, "rb") ;
}
elsif($file_type =~ /zip/){
print "The input file $file_name is compressed using winzip\n" ;
my $zp = Archive::Zip->new($file_name); ## open the zip file
my $numMembers = $zp->numberOfMembers(); ## find out what files
are in the zip file
if($numMembers>1){
die "This routine only supports zip archives with one file\n";
}
## need to get a filehandle for the compressed file
}
}
elsif(!$read){ ## if the file is supposed to be opened for writing
the do so
if ( $file_type eq "" ) {
print "The output file $file_name is standard ASCII -
uncompressed\n" ;
$FH = IO::File->new($file_name, "w") ;
}
elsif($file_type =~ /gz(ip)?/) {
print "The output file $file_name is compressed\n" ;
$FH = IO::Zlib->new($file_name, "wb") ;
}
elsif($file_type =~ /zip/){
print "The output file $in_file is compressed using winzip\n" ;
my $zp = Archive::Zip->new($file_name); ## create the zip file
## need to create a filehandle to a compressed file
}
}
## make sure the file got opened or created
if (!defined $FH) {
die "Cannot open file: $file_name $!\n";
}
## now pass the file handle back to the user for reading or writing
$_[0] = $FH;
}
ascii text file, a .gz file or a .zip file and create a filehandle for
reading or writing using the appropriate module. I have a routine
working for ascii text or .gz files but I cannot figure out how to get
it working for zip files. The goal is to replace the standard open
command in several of my scripts with my modified open, but to not have
to change any other code and be able to read from or write to any file
type simply based on the filename I use.
Here is what I have:
my $test_fh_in;
my $test_fh_out;
mod_open($test_fh_in, "test_in.zip");
mod_open($test_fh_out, ">test_out.zip");
while(<$test_fh_in>){
print "$_";
print $test_fh_out "$_";
}
sub mod_open{
## Libraries needed to to zipped file reading/writing
use File::Basename;
use IO::File ;
use IO::Zlib ;
use Archive::Zip;
my $FH;
my $file_name = $_[1];
my $read; ## boolean indicating whether to open the file for
read or write 1 = read, 0 = write
## Need to look into appending as well.....
if($file_name =~ /^\>/){ ## Check to see if the file is to be
opened for reading or writing
$read = 0;
$file_name =~ s/^\>//; ## drop the read/write identifier from the
string so it is a valid filename
}
else{
$read = 1;
}
## define a list of zip suffixes
my @suffixlist = (
".gz",
".gzip",
".zip"
);
# check for filename extension - if .gz use the the Zlib, else do
not...
my ($file_base, $file_path, $file_type) =
fileparse($file_name,@suffixlist);
if($read){ ## if the file is supposed to be opened for reading
then do so
if ( $file_type eq "" ) {
print "The input file $file_name is standard ASCII -
uncompressed\n" ;
$FH = IO::File->new($file_name, "r") ;
}
elsif($file_type =~ /gz(ip)?/) {
print "The input file $file_name is compressed using gzip\n" ;
$FH = IO::Zlib->new($file_name, "rb") ;
}
elsif($file_type =~ /zip/){
print "The input file $file_name is compressed using winzip\n" ;
my $zp = Archive::Zip->new($file_name); ## open the zip file
my $numMembers = $zp->numberOfMembers(); ## find out what files
are in the zip file
if($numMembers>1){
die "This routine only supports zip archives with one file\n";
}
## need to get a filehandle for the compressed file
}
}
elsif(!$read){ ## if the file is supposed to be opened for writing
the do so
if ( $file_type eq "" ) {
print "The output file $file_name is standard ASCII -
uncompressed\n" ;
$FH = IO::File->new($file_name, "w") ;
}
elsif($file_type =~ /gz(ip)?/) {
print "The output file $file_name is compressed\n" ;
$FH = IO::Zlib->new($file_name, "wb") ;
}
elsif($file_type =~ /zip/){
print "The output file $in_file is compressed using winzip\n" ;
my $zp = Archive::Zip->new($file_name); ## create the zip file
## need to create a filehandle to a compressed file
}
}
## make sure the file got opened or created
if (!defined $FH) {
die "Cannot open file: $file_name $!\n";
}
## now pass the file handle back to the user for reading or writing
$_[0] = $FH;
}