Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-23 15:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found