Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

using Archive::Tar

by ansh batra (Friar)
on Oct 04, 2011 at 09:38 UTC ( #929483=perlquestion: print w/replies, xml ) Need Help??
ansh batra has asked for the wisdom of the Perl Monks concerning the following question:

hi monks i am facing problem in using Archive::Tar
Archive::Tar->create_archive('Bundle-FinalTest.tar.gz', COMPRESS_GZIP, +'Bundle::Finaltest);
this only compress the folder Bundle::FinalTest but i want that it should contain all the subfolders and files of Bundle::FinalTest. please help!

Replies are listed 'Best First'.
Re: using Archive::Tar
by keszler (Priest) on Oct 04, 2011 at 11:06 UTC

    The third argument to create_archive is defined as @filelist, with the explanation:

    The remaining arguments list the files to be included in the tar file. These files must all exist. Any files which don't exist or can't be read are silently ignored.

    Note that no mention is made of directories, just files. create_archive does not recurse through directories; you must specify each file to be archived.

Re: using Archive::Tar
by pvaldes (Chaplain) on Oct 04, 2011 at 13:40 UTC
    use strict; use warnings; use autodie; use Archive::Tar; my @filelist = (); opendir(my $dir, '/path/to/my/dir/'); while(readdir $dir) { chomp; if (!~ /(^\.\.|\.)$/){ push @filelist, $_; } } my $pack = Archive::Tar->new; $pack->create_archive('Bundle-FinalTest.tgz', COMPRESS_GZIP, @filelist +); closedir $dir;

    UPDATE: A couple of changes, but Not properly working still...

    a lot of "not such file: filename" error messages, I don't know why

    Alternatively, you can use the old cheap way, that is guaranteed to act as you expect:

    `tar -zcvvf Bundle-FinalTest.tgz /path/to/Bundle/FinalTest/dir/`;

      Combine with File::Find:

      use strict; use warnings; use autodie; use Archive::Tar; use File::Find; my @fl; find (sub { -d $_ or push @fl, $File::Find::name }, "Bundle::Finaltest +"); Archive::Tar->create_archive ("Bundle-FinalTest.tar.gz", COMPRESS_GZIP +, @fl);

      Enjoy, Have FUN! H.Merijn
        thanks H.Merijin i have used your code
        what shall i do when i want to compress a FILE to .gz only? please reply asap!!!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://929483]
Approved by Corion
[davido]: jdporter : Is there a specific issue, or are we all just getting up there?

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2018-06-21 16:40 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.