Link to home
Start Free TrialLog in
Avatar of shragi
shragiFlag for India

asked on

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,
Avatar of FishMonger
FishMonger
Flag of United States of America image

Have you checked out the related modules on cpan?
cpan RAR search
Avatar of shragi

ASKER

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,
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.
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shragi

ASKER

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 ?
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
Avatar of shragi

ASKER

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>
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.
Avatar of shragi

ASKER

I don't know how to add another mirror.
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.
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.
Avatar of shragi

ASKER

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
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.