Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with Archive::Zip

Posted on 2006-07-22
5
Medium Priority
?
622 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 1000 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

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…
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…
Six Sigma Control Plans

971 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