Given a directory structure which looks something like this (which I've been using for testing):

fred fred/asd fred/derf fred/qwe fred/zxc fred/derf/fgh fred/derf/rty fred/derf/fgh/vbn

I'm looking for a way to implement the following command in Perl:

$ tar zcvf fred.tar.gz fred

I thought the builtin Archive::Tar module was possibly the way to go. I can get this to achieve what I want but it's horribly clunky:

use Archive::Tar; my $tar = Archive::Tar::->new(); $tar->add_files(glob "fred fred/* fred/*/* fred/*/*/*"); $tar->write("fred.tar.gz", COMPRESS_GZIP);

This has additional problems in that the directory structure could be deeper and hidden files (starting with '.') are not captured by any of those glob patterns.

I tried using "fred" and "fred/" as the sole arguments to add_files() but they only pick up the top-level directory, not the rest of the directory structure. I can't see an add_directory() (or similarly named) method. I also tried with the create_archive() class method but had a similar lack of success.

I've also had a hunt around various core and CPAN modules in the Archive:: and IO:: namespaces: none seemed to do what I want.

I could write a recursive routine to capture the entire directory into an array (for add_files(@filenamelist)). That would be my fallback plan but I was hoping there might be a more elegant (less code) way of doing this.

Any ideas on how I might achieve what I want with either Archive::Tar, or some other module, would be appreciated.

— Ken


In reply to Creating a tarball by kcott

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.