Reputation: 53
I am having a folder with many zip files. I need to get any of these zip file, extract it and get a particular file from the ziped file. I am using IO::Uncompress::Unzip module for this. I have my perl script as below:
#!usr/bin/perl
use IO::Uncompress::Unzip qw(unzip $UnzipError);
use strict;
my $propath ="/home/test/prroot/Projects/";
my $proj = "/home/Ras/projectroot/projects.txt";
my @projlist = `cat $proj`;
foreach my $pr(@projlist){
chomp($pr);
my $projname = $pr;
my $projtemp = $pr;
$projname =~ s/ /\_/g;
my $replace1 = "%28"; #escaping special characters from project name
my $replace2 = "%29";
my $replace3 = "%26";
my $replace4 = "%2C";
$projname =~ tr/ /_/;
$projname =~ s/\(/$replace1/g;
$projname =~ s/\)/$replace2/g;
$projname =~ s/\&/$replace3/g;
$projname =~ s/\,/$replace4/g;
chomp($projname);
my $dir = $propath.$projname;
chomp($dir);
my @res = glob "$dir/*.zip";
my @res1 = split '/',$res[$#res];
my $out = chdir $dir;
my $input = "$res1[$#res1]";
my $output = "/home/Ras/projectroot/xmlres/result$projname.xml";
my $status = unzip $input => $output, Name => "data";
}
The file /home/Ras/projectroot/projects.txt includes more than 100 project names. For each project name there is a folder under the path /home/test/prroot/Projects/. Inside the projectname folder there are zip files. I need to read particular file named data from the zip file. I am able to read the output for 13 files as /home/Ras/projectroot/xmlres/result$projname.xml. But after that no result. Please help me on this.
Upvotes: 0
Views: 338
Reputation: 53
Unzip was giving data error. I did a modification in my script and it helped.
my $status = `unzip -p $input "data" > $output`
By adding above line replacing unzip $input => $output, Name => "data" helped in solving the issue. Without going for extraction of zip file, it is just reading the data content and i am able to meet the requirement. Thanks for all your help
Upvotes: 0
Reputation: 3735
First thing to check is that the script is attempting to unzip all the files you expect.
Then see if unzip is complaining.
Try this
print "About to unzip '$input' to '$output'\n" ;
my $status = unzip $input => $output, Name => "data"
or die "unzip from '$input' to '$output' failed: $UnzipError\n";
Upvotes: 0
Reputation: 490
'As I don't know how your data look like, I provide a - slightly - improved version of your script here (adding the error handling).
#!usr/bin/perl
use IO::Uncompress::Unzip qw(unzip $UnzipError);
use File::Find;
use strict;use warnings;
my $propath ="/home/test/prroot/Projects/";
my $proj = "/home/Ras/projectroot/projects.txt";
my $output_dir = "/home/Ras/projectroot/xmlres/";
my @projlist = `cat $proj`;
my $projname;
foreach my $pr (@projlist){
chomp($pr);
$projname = $pr;
$projname =~ s/ /\_/g;
$projname =~ tr/ /_/;
$projname =~ s/\(/%28/g;
$projname =~ s/\)/%29/g;
$projname =~ s/\&/%26/g;
$projname =~ s/\,/%2C/g;
print "$dir\n";
find({ wanted => \&find_zip}, $dir);
}
sub find_zip {
my $filename = $File::Find::name;
print "$filename\n";
if ($filename =~ /zip$/ ) {
unless( unzip $filename => $output_dir.'result'.$projname.'.xml', Name => "data" ) {
warn "$filename: $UnzipError";
}
}
}
Upvotes: 0