Solved

Help with Archive::Zip

Posted on 2006-07-22
5
600 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now