Solved

Help with Archive::Zip

Posted on 2006-07-22
5
620 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Six Sigma Control Plans

632 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