Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

My thanks to all who provided help and assistance with this. I now have a working test solution which I present below as this may be useful for others.

I used Path::Tiny, as originally shown by ++tybalt89, to generate the list of files for archiving. I needed to tweak the suggested code in a few places, as follows:

  • Path::Tiny's visit() method "Executes a callback for each child of a directory.". This meant that the parent directory wasn't added to the array for add_files(): easily fixed by preloading that array with the parent directory.
  • For my purposes, the contents of the directory structure was well known, so no filtering — based on file types, permissions or other characteristics — was required. In other situations, some filtering may be required: see ++tybalt89's and ++Tux' examples[1,2] for possible ways to achieve this.
  • I also needed to change directories to get the exact archive I wanted. This may not be necessary in other circumstances. Note the use of the autodie pragma in a limited, lexical scope to handle problems with chdir and provide useful feedback if they occur.
  • Not an actual tweak required by this code, but use 5.016; was added because it mirrors the version I'm coding to for $work$ (and increases confidence that this won't have issues in my production environment). If you're using this code as a template, and not adding anything fancy, replacing that with a simple use strict; would probably be fine.

Here's the test script. Note that the directory structure under $src_dir is exactly the same as that presented in the OP.

#!/usr/bin/env perl use 5.016; use warnings; use Archive::Tar; use Cwd; use Path::Tiny; my $src_dir = '/Users/ken/tmp/test_arch/src'; my $zip_dir = '/Users/ken/tmp/test_arch/zip'; my $top_dir = 'fred'; my $zip_name = 'fred.tar.gz'; my $zip_path = path($zip_dir, $zip_name); { use autodie; my $cur_dir = getcwd; chdir $src_dir or die; my @tar_files = ($top_dir); path($top_dir)->visit( sub { push @tar_files, "$_" }, { recurse => 1 } ); my $tar = Archive::Tar::->new(); $tar->add_files(@tar_files); $tar->write($zip_path, COMPRESS_GZIP); chdir $cur_dir or die; }

A sample run, as well as various checks, are in the spoiler:

— Ken

In reply to Re: Creating a tarball [SOLUTION] by kcott
in thread Creating a tarball by kcott

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 or How to display code and escape characters are good places to start.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2022-05-17 04:21 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (65 votes). Check out past polls.