compress files in RAR using perl

Hi - how to write a perl code that compress the file into RAR.
For now I am doing this manually but I am thinking of automating the process
so when I pass a file to the perl code it should compress it into RAR, how can I achieve that.

Thanks,
shragiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FishMongerCommented:
Have you checked out the related modules on cpan?
cpan RAR search
0
shragiAuthor Commented:
I tried the below script

#!/usr/bin/perl

use 5.006;
use strict;
use warnings;
use Pod::Usage;
use Getopt::Long;
use Cwd;
use DateTime;
use DBD::mysql;
use DBI;
use Archive::Rar;

my $helpme = 0;
my $man = 0;
my $inputFileName = 'C:\\script\\test.txt';
my $outputFileName = 'C:\\script\\test.rar';

GetOptions('help' => \$helpme, 'man' => \$man, 'infile=s' => \$inputFileName, 'outfile=s' => \$outputFileName) or pod2usage(2);

pod2usage(1) if $helpme;
pod2usage(-verbose => 2) if $man;

die "No input file name specified!" if !$inputFileName;
die "No output file name specified!" if !$outputFileName;

my $rar = Archive::Rar->new(-archive => $archive_filename);


close INFILE;
close OUTFILE;

Open in new window


but it did not work,
I have a text file and i want to RAR that text file with the same name.
So what is the mistake in my program.
I want that RAR as my outfile so that I can send it to other program.

Thanks,
0
wilcoxonCommented:
What error did you get or in what way did it not work?

It looks like the code you posted is not what you ran as that will not compile.  The option has $outputFileName but the rar command uses $archive_filename.  If this is the exact code you tried, change $archive_filename to $outputFileName.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

wilcoxonCommented:
Looking at the documentation for Archive::Rar, you need a few other changes.  I also edited it down to what should be the minimal list of modules used.

#!/usr/bin/perl
use strict;
use warnings;
use Pod::Usage;
use Getopt::Long;
use Archive::Rar;

my $helpme = 0;
my $man = 0;
my $inputFileName = 'C:\\script\\test.txt';
my $outputFileName = 'C:\\script\\test.rar';

GetOptions('help' => \$helpme, 'man' => \$man, 'infile=s' => \$inputFileName, 'outfile=s' => \$outputFileName) or pod2usage(2);

pod2usage(1) if $helpme;
pod2usage(-verbose => 2) if $man;

die "No input file name specified!" unless $inputFileName;
die "No output file name specified!" unless $outputFileName;

my $rar = Archive::Rar->new(-archive => $outputFileName);
$rar->Add(-files => [$inputFileName]);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shragiAuthor Commented:
Got an error saying
Can't locate Archive/Rar.pm in @INC........
....
BEGIN failed--compilation aborted at line 6

so what is the lib file that i need ?
0
FishMongerCommented:
Did you receive that same error in the script you posted?  It's using the same use statement, so you should have received that same error and should have said so.

You need to install the module.
cpan install Archive::Rar
0
shragiAuthor Commented:
yes got the same error before along with other errors, but now i have only that error.
I tried cpan install Archive::Rar
and got the below error

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Windows\System32>cpan install Archive::Rar
CPAN: LWP::UserAgent loaded ok (v6.03)
Fetching with LWP:
http://cpan.strawberryperl.com/authors/01mailrc.txt.gz
LWP failed with code[500] message[Can't connect to cpan.strawberryperl.com:80 (B
ad hostname)]
Warning: no success downloading 'C:\Dwimperl\cpan\sources\authors\01mailrc.txt.g
z.tmp5464'. Giving up on it.
Warning: no success downloading 'C:\Dwimperl\cpan\sources\authors\01mailrc.txt.g
z.tmp5464'. Giving up on it.

As a last resort we now switch to the external ftp command 'C:\WINDOWS\system32\
ftp.EXE'
to get 'C:\Dwimperl\cpan\sources\authors\01mailrc.txt.gz.tmp5464'.

Doing so often leads to problems that are hard to diagnose.

If you're the victim of such problems, please consider unsetting the
ftp config variable with

    o conf ftp ""
    o conf commit

Fetching with LWP:
http://cpan.strawberryperl.com/authors/01mailrc.txt.gz
LWP failed with code[500] message[Can't connect to cpan.strawberryperl.com:80 (B
ad hostname)]
Warning: no success downloading 'C:\Dwimperl\cpan\sources\authors\01mailrc.txt.g
z.tmp5464'. Giving up on it.
Warning: no success downloading 'C:\Dwimperl\cpan\sources\authors\01mailrc.txt.g
z.tmp5464'. Giving up on it.

As a last resort we now switch to the external ftp command 'C:\WINDOWS\system32\
ftp.EXE'
to get 'C:\Dwimperl\cpan\sources\authors\01mailrc.txt.gz.tmp5464'.

Doing so often leads to problems that are hard to diagnose.

If you're the victim of such problems, please consider unsetting the
ftp config variable with

    o conf ftp ""
    o conf commit

CPAN: YAML loaded ok (v0.77)
Please check, if the URLs I found in your configuration file
(http://cpan.strawberryperl.com/) are valid. The urllist can be edited.
E.g. with 'o conf urllist push ftp://myurl/'

Could not fetch authors/01mailrc.txt.gz


C:\Windows\System32>
0
FishMongerCommented:
Looks like you're having a connection problem with your configured cpan mirrors.

I just installed it a little bit ago and had the same problem connecting to http://cpan.strawberryperl.com, but my next configured mirror http://www.cpan.org was successful.

c:\test>cpan Archive::Rar
CPAN: CPAN::SQLite loaded ok (v0.204)
Database was generated on Wed, 29 Jul 2015 21:58:31 GMT

Running install for module 'Archive::Rar'
CPAN: LWP::UserAgent loaded ok (v6.13)
CPAN: Time::HiRes loaded ok (v1.9726)
Fetching with LWP:
http://cpan.strawberryperl.com/authors/id/S/SM/SMUELLER/Archive-Rar-2.02.tar.gz
LWP failed with code[500] message[Can't connect to cpan.strawberryperl.com:80]
Warning: no success downloading 'C:\STRAWB~1\cpan\sources\authors\id\S\SM\SMUELLER\Archive-Rar-2.02.tar.gz.tmp5484'. Giving up on it.
Fetching with LWP:
http://www.cpan.org/authors/id/S/SM/SMUELLER/Archive-Rar-2.02.tar.gz
CPAN: YAML::XS loaded ok (v0.59)

Open in new window


Try selecting/adding a different mirror.

I had a number of failed tests which ultimately caused the install to fail.  I have not done any troubleshooting, so yours may or may not fail.
0
shragiAuthor Commented:
I don't know how to add another mirror.
0
FishMongerCommented:
I just checked the CPAN Dependencies and it appears that all 5.22.x version show failed for Win32, but all 5.21.x versions show successful.  You may need to install one of those versions.  Start with the highest one 5.21.9 and if it failed, work your way down.
0
FishMongerCommented:
Run the cpan shell and enter this command:
o conf init

Then step through the config questions.

Example:
c:\test>cpan

cpan shell -- CPAN exploration and modules installation (v2.05)
Enter 'h' for help.


cpan>

cpan> o conf init

CPAN.pm requires configuration, but most of it can be done automatically.
If you answer 'no' below, you will enter an interactive dialog for each
configuration option instead.

Would you like to configure as much as possible automatically? [yes] no

Open in new window


Make sure you answer no to that first question.
0
shragiAuthor Commented:
figured it out and ran the cpan install Archive::Rar
but i still get the same error

Can't locate Archive/Rar.pm in @INC........
....
BEGIN failed--compilation aborted at line 6
0
wilcoxonCommented:
That error will only occur if Archive::Rar is not installed (properly).  If you do perl -e "use Archive::Rar" from the command line, do you get the same error?  If so, something definitely went wrong with the install.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.