Solved

How to zip and tar files in perl

Posted on 2004-09-13
8
944 Views
Last Modified: 2006-11-17
Hi i have a list of files beginning with a and ending with underscore

like a*_.exe

i need to tar or zip them from within a perl script...(not at the command line)

how to do this on windows...

i want to unzip/untar these on a unix platform and extract them to a folder on unix machine ...how do i do this...

thanks
0
Comment
Question by:Vlearns
8 Comments
 
LVL 6

Expert Comment

by:holli
ID: 12044846
Do you want to use gzip and tar - programs or do you prefer to use
Archive::Zip and Archive::Tar - Modules?
0
 
LVL 6

Expert Comment

by:holli
ID: 12044926
anyway, this is how to archive the files:

use strict;

use Archive::Tar;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );

my $tar = Archive::Tar->new();
$tar->add_files(glob ("c:/temp/*.exe"));
$tar->write("file.tar");

my $zip = Archive::Zip->new();
my $member = $zip->addFile("file.tar");
die 'write error' unless $zip->writeToFileNamed( 'file.tar.gz' ) == AZ_OK;
0
 
LVL 6

Accepted Solution

by:
holli earned 250 total points
ID: 12045031
and how to extract:

use strict;

use Archive::Tar;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );

my $zip = Archive::Zip->new();

die 'read error' unless $zip->read( 'file.tar.gz' ) == AZ_OK;
$zip->extractMember('file.tar');

my $tar = Archive::Tar->new ("file.tar");
$tar->extract( $tar->list_files() );
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 18

Assisted Solution

by:kandura
kandura earned 200 total points
ID: 12045335
Archive::Tar can also compress files itself (using Compress::Zlib / IO::Zlib, whichever is part of your perl installation), so there's no need to do the Archive::Zip stuff.

use strict;
use Archive::Tar;
my $tar = Archive::Tar->new();
$tar->add_files(<a*_.exe>);
$tar->write("file.tar.gz", 1, 9); ### filename, compressed, maximum_compression
0
 
LVL 6

Expert Comment

by:holli
ID: 12045772
rtfm holli, rtfm ;-)
0
 
LVL 18

Expert Comment

by:kandura
ID: 12046705
8^D
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 50 total points
ID: 12048809
If you don't want to use any modules (I would recommend that you do use a module), you can do:

system "zip files.zip a*_.exe" and die "Can not zip files $!\n";
0
 

Author Comment

by:Vlearns
ID: 12061258
Thanks all

vlearns
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…

856 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