Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Creating a tarball

by tybalt89 (Prior)
on Feb 07, 2019 at 06:57 UTC ( #1229519=note: print w/replies, xml ) Need Help??


in reply to Creating a tarball

Untested:

use Path::Tiny; path('fred')->visit( sub { -f || -d and $tar->add_files($_) }, {recurse => 1} );

EDIT: Fuller version (still untested):

use Archive::Tar; my $tar = Archive::Tar::->new(); my @filenamelist; use Path::Tiny; path('fred')->visit( sub { -f || -d and push @filenamelist, "$_" }, {recurse => 1} ); $tar->add_files(@filenamelist); $tar->write("fred.tar.gz", COMPRESS_GZIP);

SECOND EDIT: Because I like Path::Tiny better than File::Find, although it has its quirks. For example, in the callback, $_ is a Path::Tiny object instead of a plain string, hence the "$_" which "stringifies" it.

Replies are listed 'Best First'.
Re^2: Creating a tarball
by Tux (Canon) on Feb 07, 2019 at 07:30 UTC

    I'd guard that against unreadable files:

    sub { -f || -d and push @filenamelist, "$_" }, => sub { -f || -d and -r and push @filenamelist, "$_" },

    Enjoy, Have FUN! H.Merijn

      Which leads to the question: Should these files be silently ignored, or an error or warning be generated?

      (And code gets more and more complicated :)

      G'day Tux,

      You raise a good point and, in general, my code is littered with such checks. However, in this instance, the directory structure is generated afresh immediately before the tarball creation, and the spec requires all files in the directory structure to be readable. So, while that is something I would normally do, it's not needed in this particular situation. Thanks for mentioning it anyway.

      — Ken

Re^2: Creating a tarball [Path::Tiny]
by kcott (Bishop) on Feb 07, 2019 at 12:08 UTC

    G'day tybalt89,

    Thanks. That looks like a nice, succinct solution.

    I'm extending legacy code, which already uses Path::Tiny, so that removes any additional, dependency-related issues (e.g. Makefile.PL). I'll test this out tomorrow.

    By the way, thanks for the additional information, e.g. object stringification.

    Update: Added "[Path::Tiny]" to the title.

    — Ken

Re^2: Creating a tarball
by Aldebaran (Curate) on Feb 10, 2019 at 08:35 UTC
    Fuller version (still untested)

    I was happy to see your post using Path::Tiny's visit method. This works well for me:

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1229519]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2022-05-16 05:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (62 votes). Check out past polls.

    Notices?