Solved

Help with Archive::Zip

Posted on 2006-07-22
5
612 Views
Last Modified: 2012-05-05
I have a series of files on a Unix server that I want to turn into a zip file and dump into a new folder.  
I can't get the zip file to fall into the proper folder.
Right now, it gets saved the same folder as the cgi script.

#!/usr/bin/perl

print "Content-type: text/plain\n\n";

use Archive::Zip qw( :ERROR_CODES :CONSTANTS ) || die "Can't find Module";

$zip_dir='/path/where/the/files/are/located';
$zip_file_name='myZipFile.zip';

##  Read names of all files in the directory
@files=("$zip_dir/file1.txt","$zip_dir/file2.txt");

my $zip = Archive::Zip->new();
my $member = $zip->addDirectory( '/path/where/i/want/the/zip/file/to/land');

foreach (@files){ ##  Add each file in that directory to the .zip archive.
print "$_\n";
  $member = $zip->addFile($_);
}

die 'Cannot create $zip_file_name: $!\n' if $zip->writeToFileNamed($zip_file_name) != AZ_OK;

0
Comment
Question by:marcparillo
  • 2
  • 2
5 Comments
 
LVL 28

Accepted Solution

by:
FishMonger earned 250 total points
ID: 17161508
#!/usr/bin/perl

print "Content-type: text/plain\n\n";

use Archive::Zip qw( :ERROR_CODES :CONSTANTS ) || die "Can't find Module";

my $dest_dir = '/path/where/i/want/the/zip/file/to/land';
$zip_dir='/path/where/the/files/are/located';
$zip_file_name='myZipFile.zip';

##  Read names of all files in the directory
@files=("$zip_dir/file1.txt","$zip_dir/file2.txt");

my $zip = Archive::Zip->new();

foreach (@files){ ##  Add each file in that directory to the .zip archive.
print "$_\n";
  $member = $zip->addFile($_);
}

die 'Cannot create $zip_file_name: $!\n' if $zip->writeToFileNamed("$dest_dir/$zip_file_name") != AZ_OK;
0
 
LVL 3

Author Comment

by:marcparillo
ID: 17161839
Thanks -- one other question:

In this section of the code, is it necessary to use $member?

foreach (@files){ ##  Add each file in that directory to the .zip archive.
print "$_\n";
  $member = $zip->addFile($_);
}


Could it also be written:

foreach (@files){ ##  Add each file in that directory to the .zip archive.
 $zip->addFile($_);
}
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 17161873
I've only used the module once and it's been awhile, so I'm not exactly sure of all of the rmifications of dropping $member from the assignment.  I do know that it's allowed, you'll loose access to the methods associated with it.  You should read over the documentation regarding Member Operations (Member Class Methods).   http://www.annocpan.org/~ADAMK/Archive-Zip-1.17_02/lib/Archive/Zip.pod
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 17163063
I would guess that, as your using it, $member is assigned the return value of the operation following it (whether it's "$zip->addDirectory( '/path/where/i/want/the/zip/file/to/land')" or "$zip->addFile($_)"), thus allowing you to check that the operation was a success.  Since you're not checking its value, you could probably leave it out.

Try adding print "$member\n"; after each assignment to see what it holds after each operation, and decide whether you need it or not...
0
 
LVL 3

Author Comment

by:marcparillo
ID: 17163464
Thanks for the help!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question