Using Archive::Zip extractTree();

Using Archive::Zip extractTree();

am 02.01.2007 22:33:56 von eldwin

Hello All,

I'm new to perl and I'm attempting to write a script that does the
following:

1) Open the directory and copy the .zip1 file name to a variable
2) create a new directory (using the .zip1 file name as the name of the
new directory.)
3) Unzip the .zip1 file to it's newly created directory.
4) Edit the files that were unzipped to the directory.
5) Zip the edited files to a new .zip1 file and put them in a FINISHED
directory.

Here's what I have so far.

#!/usr/bin/perl -w

use strict;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );

my $zip = Archive::Zip->new();
my $workdir; #Working directory where zip1 are unpackaged
my $tempdir;
my $status;

print "\n\nScanning directory for .zip1 files...\n\n";

opendir(DIR,".");

while ($workdir=readdir(DIR))
{
if($workdir =~ /zip1/)
{
mkdir $workdir."_UNPACKED";
$tempdir = $workdir."_UNPACKED";
print "Creating Working Directory: $tempdir\n";
$status = $zip->read($workdir);
die "Read of $workdir failed\n" if $status != AZ_OK;
$zip->extractTree($workdir, $tempdir);
}

}

closedir(DIR);
print "\n";
exit;

The directories are created, but the .zip1 files are not unzipped to
the created directories ($tempdir).
Any advice will be appreciated. Thanks.

--Eldwin

Re: Using Archive::Zip extractTree();

am 02.01.2007 23:40:08 von Jim Gibson

In article <1167773636.230434.19600@h40g2000cwb.googlegroups.com>,
eldwin wrote:

> Hello All,
>
> I'm new to perl and I'm attempting to write a script that does the
> following:
>
> 1) Open the directory and copy the .zip1 file name to a variable
> 2) create a new directory (using the .zip1 file name as the name of the
> new directory.)
> 3) Unzip the .zip1 file to it's newly created directory.
> 4) Edit the files that were unzipped to the directory.
> 5) Zip the edited files to a new .zip1 file and put them in a FINISHED
> directory.
>
> Here's what I have so far.
>
> #!/usr/bin/perl -w
>
> use strict;
> use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
>
> my $zip = Archive::Zip->new();
> my $workdir; #Working directory where zip1 are unpackaged
> my $tempdir;
> my $status;
>
> print "\n\nScanning directory for .zip1 files...\n\n";
>
> opendir(DIR,".");
>
> while ($workdir=readdir(DIR))
> {
> if($workdir =~ /zip1/)
> {
> mkdir $workdir."_UNPACKED";
> $tempdir = $workdir."_UNPACKED";
> print "Creating Working Directory: $tempdir\n";
> $status = $zip->read($workdir);
> die "Read of $workdir failed\n" if $status != AZ_OK;
> $zip->extractTree($workdir, $tempdir);

Read the documentation for extractTree. The first argument is not the
name of the zip file, it is the root name of all of the files in the
zip file to be extracted. If none of the files stored in the zip file
have the same root name as the name of the zip file itself (a likely
occurrence), no files will be extracted. Try cd'ing to the destination
directory and using extractTree with no arguments. Try using '' as the
first argument. If those don't work, try accessing the names of the
zipped files (with members() perhaps) and extracting them individually.
I have not used Archive::Zip and am just suggesting possibilities from
the documentation.

> }
>
> }
>
> closedir(DIR);
> print "\n";
> exit;
>
> The directories are created, but the .zip1 files are not unzipped to
> the created directories ($tempdir).
> Any advice will be appreciated. Thanks.

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Re: Using Archive::Zip extractTree();

am 03.01.2007 00:30:06 von eldwin

> Read the documentation for extractTree. The first argument is not the
> name of the zip file, it is the root name of all of the files in the
> zip file to be extracted. If none of the files stored in the zip file
> have the same root name as the name of the zip file itself (a likely
> occurrence), no files will be extracted. Try cd'ing to the destination
> directory and using extractTree with no arguments. Try using '' as the
> first argument. If those don't work, try accessing the names of the
> zipped files (with members() perhaps) and extracting them individually.
> I have not used Archive::Zip and am just suggesting possibilities from
> the documentation.
>

Thanks for the reply. I totally misunderstood the documentation. I've
fixed the script and it now unzips to the proper directories.

This is the change I made:

$zip->extractTree(' ' , ' ', $tempdir);

Thanks again,

--Eldwin