zip file using perl

P

perlnash

Hi,

I am writing a commercial software in which I need to create a zip file
from a group of files and directories.

I am using this code:-

#!/usr/bin/perl
use warnings;
use strict;

use Archive::Zip qw:)ERROR_CODES :CONSTANTS);
my $noofargs=scalar(@ARGV);
die "format: $0 zipfile.zip file [...]\n"
if ($noofargs < 2);
my $zipfilename = shift(@ARGV);
my $zip = Archive::Zip->new();
foreach my $memberName (map { glob } @ARGV){
if (-d $memberName ){
warn "Can't add tree $memberName\n"
if $zip->addTree( $memberName, $memberName ) != AZ_OK;
}
else{
$zip->addFile( $memberName )
or warn "Can't add file $memberName\n";
}
}
my $status = $zip->writeToFileNamed($zipName);
exit $status;

However, for it, I had to download perl modules Archive::Zip and one
more Compress::lib module from cpn and install them.

I do not want the users of software to have to download and install the
modules not provided by default perl installation. Also I cannot assume
that user has net connectivity. Can someone suggest a way out?
May be I can provide the required perl modules with my script, but then
will the user need to manually install these modules, I am not able to
figure out how to do that. I also want my software to be portable i.e.
to be runnable on windows and linux. Please suggest a solution.

Thanks,
perlnash.
 
T

Tad McClellan

perlnash said:
my $noofargs=scalar(@ARGV);
die "format: $0 zipfile.zip file [...]\n"
if ($noofargs < 2);


There is no need for that temporary variable:

die "format: $0 zipfile.zip file [...]\n"
if (@ARGV < 2);
 
J

John Bokma

Tad McClellan said:
perlnash said:
my $noofargs=scalar(@ARGV);
die "format: $0 zipfile.zip file [...]\n"
if ($noofargs < 2);


There is no need for that temporary variable:

die "format: $0 zipfile.zip file [...]\n"
if (@ARGV < 2);

or:

@ARGV >= 2 or die "usage: $0 zipfile.zip file [...]\n";
 
B

Ben Morrow

Quoth "perlnash said:
Hi,

I am writing a commercial software in which I need to create a zip file
from a group of files and directories.
However, for it, I had to download perl modules Archive::Zip and one
more Compress::lib module from cpn and install them.

I do not want the users of software to have to download and install the
modules not provided by default perl installation. Also I cannot assume
that user has net connectivity. Can someone suggest a way out?
May be I can provide the required perl modules with my script, but then
will the user need to manually install these modules, I am not able to
figure out how to do that. I also want my software to be portable i.e.
to be runnable on windows and linux. Please suggest a solution.

Look at PAR (http://par.perl.org).

Ben
 
P

perlnash

John said:
Tad McClellan said:
perlnash said:
my $noofargs=scalar(@ARGV);
die "format: $0 zipfile.zip file [...]\n"
if ($noofargs < 2);


There is no need for that temporary variable:

die "format: $0 zipfile.zip file [...]\n"
if (@ARGV < 2);

or:

@ARGV >= 2 or die "usage: $0 zipfile.zip file [...]\n";




Thanks but my question is different, in short, I want to know if I
download a module/source code of a module from the net(cpan), and I
have a script which uses this module, is there some way I can use the
module (provide module with the script) so that users of my
software do not have to take the pains of downloading the required
modules from net and installing them.

-perlnash
 
P

perlnash

Thanks Ben, but I guess PAR packages perl files and is used to create
an executable zip file, while I want to package normal files(not
necessarily perl files) and I do not want my zip file to be executable.
But I guess things should be similar.

Another thing, if I am using a certain module, if I replace the line
use Archive::Zip by the source code of the Archive::Zip module, will it
work (similar to replacement of header files by the code of header
files by C preprocessor) ? Will I need to install the module in that
case? Actually I am novice to perl modules and till now I have not used
perl modules, so I need help.

Thanks,
perlnash
 
B

Ben Morrow

Quoth "perlnash said:
Thanks Ben, but I guess PAR packages perl files and is used to create
an executable zip file, while I want to package normal files(not
necessarily perl files)

You can put ordinary files into PAR's zips and then get them out later.
It's a bit fiddly though: I don't remember the details.
Another thing, if I am using a certain module, if I replace the line
use Archive::Zip by the source code of the Archive::Zip module, will it
work (similar to replacement of header files by the code of header
files by C preprocessor) ?

It may, or it may not. Something like

BEGIN {
# source of module here
$INC{'Foo/Bar.pm'} = $0;
import Foo::Bar # whatever you would have put on the use line
}

should work for pure Perl modules, but have you looked closely at par?
It has an option to produce a single script which includes all needed
modules, and it's more likely to get it right than doing it by hand.

Ben
 
T

Tad McClellan

perlnash said:
Subject: zip file using perl


I feel quite confident that your problem is not about zip file using perl.

I expect that you are already zipping files just fine.

Right?

I think your problem is: distribute program that uses module without
making the user install the module.

Yes?

Thanks Ben, but I guess PAR packages perl files and is used to create
an executable zip file,


Exactly so.

The executable zip file will be your Perl program and the
Archive::Zip (or any other) module.

while I want to package normal files(not
necessarily perl files) and I do not want my zip file to be executable.


PAR makes an executable (that is zipped).

You want to zip *data* in your Perl program.

There are 2 zips here. The one that PAR makes for you so your users
can run the program without having to install the module, and the
data that you are compressing.

But I guess things should be similar.


"code" and "data" are not at all similar.

if I am using a certain module, if I replace the line
use Archive::Zip by the source code of the Archive::Zip module, will it
work


Maybe and maybe not.

Will I need to install the module in that
case?


Do you need to install the module when you've (attempted to) copy
the module's source into your Perl program?

No. Maybe.

Actually I am novice to perl modules and till now I have not used
perl modules, so I need help.


You should start at the beginning rather than the middle.

You will need more than a cursory understanding of modules to
solve the problem you describe.

So, the first thing to do is to read up on modules.

Have you done that yet?

perldoc perlmod
 
B

Big and Blue

perlnash said:
I am writing a commercial software in which I need to create a zip file
from a group of files and directories.

So you are expecting to pay a commercial rate for the answer to your
problem?
#!/usr/bin/perl
use warnings;
use strict;

use Archive::Zip qw:)ERROR_CODES :CONSTANTS); ....

However, for it, I had to download perl modules Archive::Zip and one
more Compress::lib module from cpn and install them.

Which resulted in a set of installed files? So why not send them along
with your script. Then all you need to do is add a suitable

use lib .....;

statement before the use Archive::Zip above and things should work.

I assume that you know where these files have been installed so would
know what to put in place of ...... If not, you could work it out at
relative to $0.
 
J

John Bokma

perlnash said:
Hi,

I am writing a commercial software in which I need to create a zip
file from a group of files and directories.
[..]

However, for it, I had to download perl modules Archive::Zip and one
more Compress::lib module from cpn and install them.

use PAR to bundle everything into one file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,818
Messages
2,569,732
Members
45,691
Latest member
Dick331194

Latest Threads

Top