Solved

Help with Archive::Zip

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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

728 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